5

I got a Runge-Kutta method here and I solve this system using it.

So here's Runge-Kutta stuff \begin{align} k_1 &= f(t_n, y_n) \\ k_2 &= f(t_n + h/2, y_n + hk_1/2) \\ k_3 &= f(t_n+h, y_n - hk_1 + 2hk_2) \\\hline y_{n+1} &= y_n + h(k_1 + 4k_2 + k_3)/6 \end{align} where $h$ is step

Here's my test system \begin{align} y'_1 &= -5y_1 - 10y_2 + 14e^{-x} \\ y'_2 &= -10y_1 - 5y_2 + 14e^{-x} \end{align} with exact solution $y_1(x) = y_2(x) = e^{-x}$

UPD: The initial condition here is $y_1(0) = y_2(0) = 1$

I need to solve it on $[0;4]$.


Well, I thought I solved it right, because I checked how the exact solution and these approximate solution plots looks like (on the left, on the right I zoomed plot until saw difference) plot and zoom

Also I checked how the plot of the difference between exact solution and approximate solution depending on step (let's call it e/h) looks like.

So $e/h$ it looks like this enter image description here

But when I checked e/h^4 dependence it looked like these enter image description here

I showed it to the teacher and she said that my solution is wrong, it's not suppose to be like these! I show my code to her asked for help but she said that she doesn't understand matlab :c

Have I really done something wrong? And if yes what I've done wrong? And if not how to prove that I'm right?

Here's my code btw

Runge-Kutta method

  function [ res_y ] = RungeKutta(dim, size, grid, step, f1,f2,y1, y2)
k1=zeros(dim);
k2=zeros(dim);
k3=zeros(dim);

h = step;

res_y(1,1) = y1;
res_y(2,1) = y2;

for i=1: size

   k1(1)= f1(grid(i),y1,y2);
   k1(2)= f2(grid(i),y1,y2);

   k2(1)= f1(grid(i)+h/2, y1+h*k1(1)/2, y2+h*k1(2)/2);
   k2(2)= f2(grid(i)+h/2, y1+h*k1(1)/2, y2+h*k1(2)/2);

   k3(1)= f1(grid(i)+h, y1-h*k1(1)+2*h*k2(1), y2-h*k1(2)+2*h*k2(2));
   k3(2)= f2(grid(i)+h, y1-h*k1(1)+2*h*k2(1), y2-h*k1(2)+2*h*k2(2));

   res_y(1,i+1) = y1 + h*(k1(1) + 4*k2(1) + k3(1))/6;
   res_y(2,i+1) = y1 + h*(k1(2) + 4*k2(2) + k3(2))/6;

   y1 = res_y(1,i+1); 
   y2 = res_y(2,i+1);
end

end

Main method

    a = 0; b = 4;
    h = 0.1; % step
    t = a:h:b; %grid
    n = 2; 
    m = size(t,2);
 hold on;
     plot(t, exp(-t),'b-')
     plot(t, exp(-t),'r--')
 hold off;

y1=1; y2 = 1;

f1_ptr = @f1;% out = -5 * y1 - 10 * y2 + (14)*exp(-x);
f2_ptr = @f2;% out = -10 * y1 - 5 * y2 + (14)*exp(-x);

res_y = RungeKutta(n,m-1,t,h,f1_ptr, f2_ptr,1,1);

hold on;
plot(t,res_y);

hold off;

%e/h and e/h^4 plots

fig_a = figure; set(fig_a,'name','e/h','numbertitle','off') hold on;

counter = 0;

for h=0.001:0.01:0.1 y1=1; y2 = 1; t = a:h:b; m = size(t,2); counter = counter + 1;

result_appr = RungeKutta(n,m-1,t,h,f1_ptr, f2_ptr,y1,y2);
result_exact = exp(-t);


result_difference = abs(result_appr(1, :) - result_exact);

e1(counter) = max(result_difference);
e2(counter) = max(result_difference);

hh = h*h*h*h;

ehh1(counter)=e1(counter)/hh;
ehh2(counter)=e2(counter)/hh;  


end;

h=0.001:0.01:0.1; plot(h,e1,'c'); plot(h,e2,'c'); hold off;

fig_b = figure; set(fig_b ,'name','e/h^4','numbertitle','off')

hold on; plot(h,ehh1,'r') plot(h,ehh2,'b') hold off;

f1 function

function [ out ] = f1( x, y1, y2, alpha, beta )
if nargin == 3
    alpha = 5;
    beta = 10;
end

out = -alpha * y1 - beta * y2 + (alpha + beta - 1)*exp(-x);

end

f2 function

function [ out ] = f2( x, y1, y2, alpha, beta )
if nargin == 3
    alpha = 5;
    beta = 10;
end
out = -beta * y1 - alpha * y2 + (alpha + beta - 1)*exp(-x);

end

Lutz Lehmann
  • 131,652
  • Where are the initial conditions? – Amzoti Nov 23 '13 at 14:49
  • @Amzoti oops sorry, I'll update question – Danil Gholtsman Nov 23 '13 at 14:57
  • @Amzoti I updated it! – Danil Gholtsman Nov 23 '13 at 15:07
  • I dont read Matlab code but, in the statement : "res_y(2,i+1) = y1 + h(k1(2) + 4k2(2) + k3(2))/6;" is it "y1" or "y2" ? – Claude Leibovici Nov 23 '13 at 15:22
  • @ClaudeLeibovici it's 'y2' – Danil Gholtsman Nov 23 '13 at 15:31
  • 1
    @DanilGholtsman. When you have such a problem, after a few hours, you are no more able to see anything. In this case, ask a friend to look at it. Trust me, I starting in computing sciences 53 years ago. Does this change your results ? Please reply and post. Glad to have been able to help (hoping that this is the case). – Claude Leibovici Nov 23 '13 at 16:00
  • @ClaudeLeibovici 53 years ago? wow sir, you're awesome! Btw I changed this line res_y(2,i+1) = y2 + h*(k1(2) + 4*k2(2) + k3(2))/6 but it changed nothing. Well, my firends in university always asking me for help. Actually here in my town and in coutry our CS education nowadays it's not good, as you can see :) – Danil Gholtsman Nov 23 '13 at 16:10
  • @DanilGholtsman: Are those supposed to be $e^{-t}$ instead of $e^{-x}$ terms? Also, over the range you defined, wouldn't the numerical solution have issues? It is okay over $[0,1]$, for example. – Amzoti Nov 24 '13 at 05:37
  • @Amzoti well, actually $t$ is array of the points where we execting our function. $e^t$ and $e^x$ are equal thing. What kind of issues? Over [0,1] approximation looks okay too – Danil Gholtsman Nov 24 '13 at 08:55
  • Why does your teacher say that your solution is wrong ? – Tony Piccolo Nov 24 '13 at 10:50
  • @TonyPiccolo because she says that plot of e/h^4 not suppose to look like it looks like here – Danil Gholtsman Nov 24 '13 at 10:53
  • And how should it look ? – Tony Piccolo Nov 24 '13 at 17:21
  • @TonyPiccolo she said that first point are too far from X axis ($>500$) – Danil Gholtsman Nov 24 '13 at 17:30
  • The code seems right. From the type of the plot I could think of a problem depending on the finite precision of the machine arithmetic, but matlab uses double precision by default. In fact the phenomenon happens if one forces lower precision with small steps. You could tell to your teacher ... – Tony Piccolo Nov 24 '13 at 23:58
  • @TonyPiccolo well, I tried once (I told her about it but I wasn't sure, I mean I didn't want to made research so it were sound like an excuse maybe). Colud you please write an answer then? – Danil Gholtsman Nov 25 '13 at 03:51
  • In the meanwhile let me know if you are studying from a textbook. – Tony Piccolo Nov 25 '13 at 10:22
  • @TonyPiccolo no, no textbooks. Just lectures. – Danil Gholtsman Nov 25 '13 at 16:02
  • @TonyPiccolo I guess tommorow I ask other numerical methods teacher to check what I've done wrong – Danil Gholtsman Nov 25 '13 at 16:04
  • 2
    I need to know where the test about $e/h^4$ comes from for a third order Runge-Kutta method: can your teacher give you some written reference ? – Tony Piccolo Nov 25 '13 at 17:27
  • @TonyPiccolo haha, guess what? I showed(and translared) her this post (and the previous one too) and the answers today! And she agreed that I'm not suppose to check $\frac{e}{h^4}$ and etc, so I guess everything is ok now! Thank you :) – Danil Gholtsman Nov 26 '13 at 12:56
  • @TonyPiccolo hi! could you write what you've said as answer. I know it's been a while lol but I realized that nobody still wrote anything and your suggestuion were right thou. – Danil Gholtsman Jul 22 '14 at 03:10

1 Answers1

3

The method is a classical third-order method as computed and presented by M. Wilhelm Kutta in 1901, based on the Simpson quadrature rule.

This means that the global error will scale like $h^3$ and the local truncation error like $h^4$.

Plotting the error as computed against $h$, but in a loglog plot and for a geometric sequence of step sizes, for h = 10.^[-4:0.5:-1], gives a line that has a very nice slope $3$.

enter image description here

Another way to visualize this is to plot the difference of the computed to the exact solution divided by $h^3$. This gives an error profile plot

enter image description here

that nicely and visually converges to the leading coefficient $c(x)$ in $e(x)=c(x)h^3+O(h^4)$. Note that this is the absolute error, and that the solutions fall exponentially to zero, which explains that the error falls in the end. In a plot of the relative error one could expect the curve to continue to grow away from zero.

Lutz Lehmann
  • 131,652
  • Oh, after 7 years I don't even understand what it was about what I was writing in the question. As a student I knew math a bit, but years of web development washed out almost all stuff from uni completely...

    But nice to see that the answer is here :)

    – Danil Gholtsman Feb 17 '21 at 23:12