Backward Eurler and Forward Eurler on matrices in matlab
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to to solve the sysmtem of differential equations
where I'm given initial condition x(0) = X0 and matrix A and use forward Eurler and Backward Eurler to find X.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/823935/image.png)
So I tried writing the code as follows
load ODE_Data %load the matrix A and initial condition X0
dT = 6/500; %sec
t_be = 0:dT:6;
Xbe = zeros(5,length(t_be));
Xbe(:,1) = X0;
for t=2:length(t_be)
Xbe(:,t) = ((1/dT)-A)\(Xbe(:,t-1)/dT);
end
for Backward Eurler using the equation ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/823940/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/823940/image.png)
and
load ODE_Data %load the matrix A and initial condition X0
%Name your time points as t_fe and Name your solution vector as Xfe.
dT = 6/500; %sec
t_fe = 0:dT:6;
Xfe = zeros(5,length(t_fe));
Xfe(:,1) = X0;
for t=2:length(t_fe)
b =Xfe(:,t-1);
c=dT*(A+(1/dT));
Xfe(:,t) = dT*(A+(1/dT))*Xfe(:,t-1);
end
for Forward Eurler using the equation ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/823945/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/823945/image.png)
but the anwsers for both quickly goes to infinity and I'm really lost on why that is.
0 Kommentare
Antworten (1)
Michael Hubatka
am 6 Jan. 2022
First, make sure that
eig(A) < 0
otherwise, the solution should in fact go to infinity.
The real problem in your code is the calculation of
1/dT - A
A + 1/dT
This will modify all entries in A, the correct code is
eye(size(A))./dT - A
A + eye(size(A))./dT
which will modify only the diagonal.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!