matlab - Absolute error of ODE45 and Runge-Kutta methods compared with analytical solution -


I appreciate that someone may help in the following issue: I have the following ODE:

  dr / dt = 4 * exp (0.8 × t) - 0.5 * r, r (0) = 2, t [0,1] (1)  

I give Is solved in different ways (1) through the rage-cut method (4th order) and through the ode45 in Matlab. I compared both results with analytical solution, which is given by:

  r (t) = 4 / 1.3 (exp (0.8 * t) - exp (-0.5 * t) ) +2 * APP (-0.5 * T)  

When I plot the complete error of each method regarding the exact solution, I get the following:

RK-method, my code is:

  h = 1/50; X = 0: h: 1; Y = zero (1, length (x)); Y (1) = 2; F_xy = @ (T, R) 4. * XP (0.8 * T) - 0.5 * R; I for = 1: (length (x) -1) k_1 = F_xy (x (i), y (i)); K_2 = F_xy (x (i) + 0.5 * h, y (i) + 0.5 * h * k_1); K_3 = F_xy ((x (i) + 0.5 * h), (y (i) + 0.5 * h * k_2)); K_4 = F_xy ((x (i) + h), (y (i) + k_3 * h)); Y (i + 1) = y (i) + (1/6) * (k_1 + 2 * k_2 + 2 * k_3 + k_4) * h; % Main Equation End  

Enter image details here

For

and ode45 :

  tspan = 0: 1/50: 1; X0 = 2; F = @ (T, R) 4. Exp (0.8 * T) - 0.5 * R; [Tid, y_oode45] = ode 45 (f, x, x);  

Enter image details here

My The question is, why do I oscillate when I use ode45 ? (I'm referring to full error) both solutions are accurate ( 1e-9 ), but in this case what happens with ode45 ?

When I calculate the full error for the RK-method, why does it look good?

Your RK4 function is taking a definite step, which can be obtained from ode45 It is very small that which you are actually seeing is an error, due to which the steps that make ode45 are often referred to as "dense output" (see) is.

When you specify a TSPAN vector with more than two elements, then certain phase size generates output. This does not mean that they actually use a fixed shape size or they use the step sizes specified in your TSPAN . By using a structure and using it, you can see the actual step size using the size of the size you want and use it:

  sol = ode45 (f, tspan, x0); Diff (sol.x)% actual step size y_ode45 = deval (sol, tspan);  

You will see that after the initial step of 0.02 , because your ODE is simple, for this next steps it will be 0.1 Is focused. Default default tolerances combined with the default maximum step size range (a tenth integration interval) determine this. Let's plot the error on the right steps:

  exactsol = @ (t) (4/1) * (exp (0.8 * t) -exp (-0.5 * t)) + 2 * Exp (-0.5 * t); Abs_err_ode45 = abs (exact (tspan) -y_ode45); Abs_err_ode45_true = abs (exact (sol.x) -sol.y); Abs_err_rk4 = abs (exact (tspan) -y); the figure; Stories ('ODE45', 'ODE45 (right step)', 'RK4', 2)  

In comparison, effectively there is a high order method so that you can expect it). Due to interpolation the error grows between integration points. If you want to limit it, you should adjust tolerance or other options.

If you can do ode45 to 1/50 you can do it (works because your psalm is simple):

  selects the option = odeset ('MaxStep', 1/50, 'InitialStep', 1/50); Sol = Ode 45 (F, Tapan, X0, Opt); Diff (sol.x) y_ode45 = deval (soul, tspan);  

For another experiment, maybe try to increase the integration interval in t = 10 . You will see lots of interesting behaviors in error (relative error plot It's useful to do here). Can you explain this? Can you use the ode45 and odeset to generate the result? It is challenging to add exponential tasks at large intervals with adaptive step methods and ode45 is not the best tool for the job though, but they may need some programming.


Comments