Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the numeric error in my implementation of Runge-Kutta not decrease like N^a?
    primarykey
    data
    text
    <p>I'm trying to determine how many steps it takes for the <a href="http://mathworld.wolfram.com/Runge-KuttaMethod.html" rel="nofollow">Runge-Kutta Method</a> ("RK4") to be within 0.01% of the exact solution of an ordinary differential equation. I'm comparing this to the <a href="http://mathworld.wolfram.com/EulerForwardMethod.html" rel="nofollow">Euler method</a>. Both should result in a straight line on a loglog plot. My Euler solution seems to be correct but I am getting a curved solution for RK. They are based on the same code so I am completely confused about the problem. </p> <p>EDIT: Sorry for removing the wikipedia links. It wouldn't let me keep more than one link since I'm a new user. However, both methods are detailed on Wikipedia similarly to my implementation.</p> <p>Should someone wish to tackle my problem, the code is below and graphs are in <a href="http://dl.dropbox.com/u/5962491/td4.4.doc" rel="nofollow">this Word file on dropbox.com</a>. Yes this is a homework problem; I'm posting this because I'd like to understand what is wrong in my thought process.</p> <pre><code>f = @(x,y) x+y; %this is the eqn (the part after the @(t,y) </code></pre> <p>This is my RK4 code:</p> <pre><code>k1=@(x,y) h*f(x,y); k2=@(x,y) h*f(x+1/2*h,y+1/2*k1(x,y)); k3=@(x,y) h*f(x+1/2*h,y+1/2*k2(x,y)); k4=@(x,y) h*f(x+h,y+k3(x,y)); clear y x exact i x(1)=0; y(1)=2; xn=1; x0=0; exact=3*exp(xn)-xn-1; %exact solution at x=1 %# Evaluate RK4 with 1 step for x=0...1 N=1; %# number of steps h=(xn-x0)/N; %# step size i=1; y(i+1)=y(i)+1/6*k1(x(i),y(i))+1/3*k2(x(i),y(i))+1/3*k3(x(i),y(i))+1/6*k4(x(i),y(i)); RK4(N)=y(i+1); %# store result of RK4 in vector RK4(# of steps) E_RK4(N)=-(RK4(N)-exact)/exact*100;%keep track of %error for each N Nsteps_RK4(N)=N; %# repeat for increasing N until error is less than 0.01% while -(RK4(N)-exact)/exact &gt; 0.0001 i=1; N=N+1; h=(xn-x0)/N; for i=1:N y(i+1)=y(i)+1/6*k1(x(i),y(i))+1/3*k2(x(i),y(i))+1/3*k3(x(i),y(i))+1/6*k4(x(i),y(i)); x(i+1)=x(i)+h; end RK4(N)=y(i+1); Nsteps_RK4(N)=N; E_RK4(N)=-(RK4(N)-exact)/exact*100; %# keep track of %error for each N end Nsteps_RK4(end); </code></pre> <p>This is my Euler code: </p> <pre><code>%# Evaluate Euler with 1 step for x=0...1 clear y x i x(1)=0; y(1)=2; N=1; %# number of steps h=(xn-x0)/N; %# step size i=1; y(i+1)= y(i)+h*f(x(i),y(i)); Euler(N)=y(i+1); %# store result of Euler in vector Euler(# of steps) E_Euler(N)=-(Euler(N)-exact)/exact*100;%# keep track of %error for each N Nsteps_Euler(N)=N; %# repeat for increasing N until error is less than 0.01% while -(Euler(N)-exact)/exact &gt; 0.0001 i=1; N=N+1; h=(xn-x0)/N; for i=1:N y(i+1)= y(i)+h*f(x(i),y(i)); x(i+1)=x(i)+h; end Euler(N)=y(i+1); Nsteps_Euler(N)=N; E_Euler(N)=-(Euler(N)-exact)/exact*100; %# keep track of %error for each N end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload