CroCo
CroCo

Reputation: 5741

Euler’s Method for solving ODE ( why the error is decreasing )

I have a simple linear system with 2nd order of ODE.

enter image description here

The analytical solution of the system is

enter image description here

Euler’s Method is

enter image description here

Now I would like to solve the system and compare the approximated value with the true value. This is my code in Matlab

clear all; clc;

 t = 0;
dt = 0.2;
tsim = 5.0;
n = round((tsim-t)/dt);
A = [ -3 0; 
      0 -5];
B = [2;3];
XE = [1; 2];
u = 1;

fprintf('Time \t     Euler Value \t True Value  Error \n');

for i = 1:n

    % Analytical Method
    XA = [ exp(-3*t)/3 + 2/3; 
          (7*exp(-5*t))/5 + 3/5];

    % Euler Method
    dx  = A*XE + B*u;
    XE  = XE + dx*dt;
    X1(i,:) = [t, XE'];
    fprintf('%f \t %f \t     %f \t %f\n', t, XE(1), XA(1), (XA(1)-XE(1))/XA(1)*100 );

    t = t + dt;
end

The results are

Time         Euler Value     True Value  Error 
0.000000     0.800000        1.000000    20.000000
0.200000     0.720000        0.849604    15.254624
0.400000     0.688000        0.767065    10.307440
0.600000     0.675200        0.721766    6.451714
0.800000     0.670080        0.696906    3.849297
1.000000     0.668032        0.683262    2.229064
1.200000     0.667213        0.675775    1.266957
1.400000     0.666885        0.671665    0.711675
1.600000     0.666754        0.669410    0.396748
1.800000     0.666702        0.668172    0.220089
2.000000     0.666681        0.667493    0.121690
2.200000     0.666672        0.667120    0.067134
2.400000     0.666669        0.666916    0.036980
2.600000     0.666668        0.666803    0.020348
2.800000     0.666667        0.666742    0.011188
3.000000     0.666667        0.666708    0.006149
3.200000     0.666667        0.666689    0.003378
3.400000     0.666667        0.666679    0.001855
3.600000     0.666667        0.666673    0.001019
3.800000     0.666667        0.666670    0.000559
4.000000     0.666667        0.666669    0.000307
4.200000     0.666667        0.666668    0.000169
4.400000     0.666667        0.666667    0.000092
4.600000     0.666667        0.666667    0.000051
4.800000     0.666667        0.666667    0.000028

My question is why the error is decreasing ?

Upvotes: 1

Views: 331

Answers (1)

BerndGit
BerndGit

Reputation: 1660

I'Ve have made the following correction. If you calculate ´XE = XE + dx*dt;´ then also the time has to be increased by ´t = t + dt;´:

clear all; clc;

 t = 0;
dt = 0.2;
tsim = 5.0;
n = round((tsim-t)/dt);
A = [ -3 0; 
      0 -5];
B = [2;3];
XE = [1; 2];
u = 1;
X1(1,:) = [t, XE'];

fprintf('Time \t     Euler Value \t True Value  Error \n');

for i = 2:n+2

    % Analytical Method
    XA = [ exp(-3*t)/3 + 2/3; 
          (7*exp(-5*t))/5 + 3/5];

    fprintf('%f \t %f \t     %f \t %f\n', t, XE(1), XA(1), (XA(1)-XE(1))/XA(1)*100 );

    % Euler Method
    dx  = A*XE + B*u;
    XE  = XE + dx*dt;
    X1(i,:) = [t, XE'];


    t = t + dt;
end

Now the output is:

Time         Euler Value     True Value  Error 
0.000000     1.000000        1.000000    0.000000
0.200000     0.800000        0.849604    5.838471
0.400000     0.720000        0.767065    6.135693
0.600000     0.688000        0.721766    4.678287
0.800000     0.675200        0.696906    3.114622
1.000000     0.670080        0.683262    1.929326
1.200000     0.668032        0.675775    1.145733
1.400000     0.667213        0.671665    0.662889
1.600000     0.666885        0.669410    0.377167
1.800000     0.666754        0.668172    0.212243
2.000000     0.666702        0.667493    0.118548
2.200000     0.666681        0.667120    0.065876
2.400000     0.666672        0.666916    0.036477
2.600000     0.666669        0.666803    0.020147
2.800000     0.666668        0.666742    0.011108
3.000000     0.666667        0.666708    0.006116
3.200000     0.666667        0.666689    0.003365
3.400000     0.666667        0.666679    0.001850
3.600000     0.666667        0.666673    0.001017
3.800000     0.666667        0.666670    0.000558
4.000000     0.666667        0.666669    0.000307
4.200000     0.666667        0.666668    0.000168
4.400000     0.666667        0.666667    0.000092
4.600000     0.666667        0.666667    0.000051
4.800000     0.666667        0.666667    0.000028
5.000000     0.666667        0.666667    0.000015

The max error is reduced from 20 % to 6 % by using the correct time stamp.

The solution converges to steady state conditions for t -> inf. The algorithm is obviously able to find this end value (where dx -> [0 0]). Just in the transient approach to this equilibrium there are differences between discretisation and true value. You could get a clearer picture on, whats going on if you plot the curves for these solutions (instead of just looking at the numbers).

Upvotes: 2

Related Questions