Euler's approximation in MATLAB

699 views Asked by At

I have a project in MATLAB where I am to approximate the solution of a diff equation. To do that, I use ode45 to get the "real solution" and compare it to Euler's approximation that I perform 3 times with halving the step every time. Here are my issues:

ode45 doesn't seem to work on my computer. I get this message:

No help found for ode45.m.

When I type help ode45 in the commando window and this

Error using ode45
Too many input arguments.

Error in Lab2 (line 51)
[f, u] = ode45(@myode, [pi/6 pi/2], 1);

So I switched to ode23 and got a what I thought to be a quite good result. Problem is though that I noticed that the error in the last approximation becomes slightly smaller which shouldn't happens since the error gets only larger by every step... right?

To makes things worse, I tried to run my code with ode23 on the school computer and got different results (different solution curve). I tried then ode45 and got the same results. When I look at the curve and its values it is totally wrong since in my diff equation values should be decreasing instead of increasing like they do when I run them on the school computer.

I don't understand how the same code can produce two different results on different computers.

I don't understand either why ode45 is missing from my computer. I have tried re-installing the new version and it is still the same.

I am totally confused...

Here is my code:

This is in a function file named myode

function dudf = myode(f,u)
k=1/20;
dudf = (-k*u.^3)/sin(f).^3;
end

This is the program

%Euler's method
f_init = pi/6;
f_final = pi/2;
u_init = 1;
k = 1/20;
n = 10; %number of steps
h(1) = (f_final-f_init)/n;
fh(1) = f_init;
uh(1) = u_init;

%calculate euler's approximation after every step
for i = 2:n+1
    fh(i) = fh(i-1)+h;
    uh(i) = uh(i-1)+h*(-k*uh(i-1)^3)/(sin(fh(i-1))^3);
end


%save vaules of ode45 at every step for first appr.
step0=pi/30;
fiend = pi/6 + step0;
odeu1 = [1];
step1 = [pi/6];
for i = 1:length(uh)-1
    [f, u] = ode23(@myode, [pi/6 fiend], 1);
    odeu1 = [odeu1 u(end)];
    step1 = [step1 fislut];
   fiend = fiend + step0;
end

Any help is appreciated!

0

There are 0 answers