Comparing Methods for Initial Value Problem

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Compare
In summary, the conversation discusses solving an initial value problem using the forward Euler method, trapezoid method, and backward Euler method. The conversation also explores how to compare the accuracy of these methods by plotting graphs of the approximations. It is suggested to subtract the exact solution from the approximations to see which method is the most accurate.
  • #36
And what if we write these two function into one?

I mean like that:
Code:
function [ ] = ex(N) 
h=1/N;
A=[-5 -2;-2 -100]; 
%trapezoid_method  
y=[1;1];
t2=[0];
y2=[y];
for (i=1:N)
      y=(eye(2)+h*A+h^2/2*A^2)*y;
      t2=[t2, i*h];
      y2=[y2, y];
end 
%euler_method  
y=[1;1];
t3=[0];
y3=[y];
for (i=1:N)
    y=inv(eye(2)-h*A)*y;
    t3=[t3, i*h];
    y3=[y3, y];
end 
plot(t2, y2(1,:).^2+y2(2,:).^2, 'y', t3, y3(1,:).^2+y3(2,:).^2, 'b'); 
end

So that the variables are known, does the function have to return these? Or how can we fix the error that we get, that the variable are not known? I splitted the two functions into two files, the following:

Code:
function [ ] = euler(N) 
h=1/N;
A=[-5 -2;-2 -100]; 
%trapezoid_method  
%euler_method  
y=[1;1];
t3=[0];
y3=[y];
for (i=1:N)
    y=inv(eye(2)-h*A)*y;
    t3=[t3, i*h];
    y3=[y3, y];
end 
end

Code:
function [ ] = trapezoid(N) 
h=1/N;
A=[-5 -2;-2 -100]; 
%trapezoid_method  
y=[1;1];
t2=[0];
y2=[y];
for (i=1:N)
      y=(eye(2)+h*A+h^2/2*A^2)*y;
      t2=[t2, i*h];
      y2=[y2, y];
end 
end

Then in the command line I wrote the following commands:
octave:1> trapezoid(100)
octave:2> euler(100)
octave:3> plot(t2, y2(1,: ).^2+y2(2,: ).^2, 'y', t3, y3(1,: ).^2+y3(2,: ).^2, 'b');

but I get again "error: 't2' undefined near line 1 column 6". What am I doing wrong? (Thinking)

Here's how it looks like at me:View attachment 9069
 

Attachments

  • oct.jpg
    oct.jpg
    70.6 KB · Views: 70
Mathematics news on Phys.org
  • #37
evinda said:
And what if we write these two function into one?

I mean like that:

So that the variables are known, does the function have to return these? Or how can we fix the error that we get, that the variable are not known?

That should work yes.
Does it? (Thinking)

evinda said:
I splitted the two functions into two files, the following:

Then in the command line I wrote the following commands:
octave:1> trapezoid(100)
octave:2> euler(100)
octave:3> plot(t2, y2(1,: ).^2+y2(2,: ).^2, 'y', t3, y3(1,: ).^2+y3(2,: ).^2, 'b');

but I get again "error: 't2' undefined near line 1 column 6". What am I doing wrong? (Thinking)

Here's how it looks like at me:

Now we do not get the error that there is something wrong with the script any more. Good! (Happy)

The variables still need to be returned though.
We can do that with:
[M] function [t3, y3] = euler(N)
...[/M]
and:
[M]function [t2, y2] = trapezoid(N)
...[/M]
and:
[M]octave:1> [t2, y2] = trapezoid(100)
octave:2> [t3, y3] = euler(100)
octave:3> plot(t2, y2(1,: ).^2+y2(2,: ).^2, 'y', t3, y3(1,: ).^2+y3(2,: ).^2, 'b'); [/M]
(Emo)
 
  • #38
Klaas van Aarsen said:
The variables still need to be returned though.
We can do that with:
[M] function [t3, y3] = euler(N)
...[/M]
and:
[M]function [t2, y2] = trapezoid(N)
...[/M]
and:
[M]octave:1> [t2, y2] = trapezoid(100)
octave:2> [t3, y3] = euler(100)
octave:3> plot(t2, y2(1,: ).^2+y2(2,: ).^2, 'y', t3, y3(1,: ).^2+y3(2,: ).^2, 'b'); [/M]
(Emo)

I tried it like that. The command "[t2, y2] = trapezoid(100)" works. But the command "[t3, y3] = euler(100)" seems not to work, I get the following:View attachment 9070(Thinking)
 

Attachments

  • oct2.jpg
    oct2.jpg
    53.4 KB · Views: 68
  • #39
evinda said:
I tried it like that. The command "[t2, y2] = trapezoid(100)" works. But the command "[t3, y3] = euler(100)" seems not to work, I get the following:

It seems to work for me:

View attachment 9071

(Wasntme)
 

Attachments

  • euler.png
    euler.png
    19.8 KB · Views: 73

Similar threads

Replies
2
Views
995
Replies
3
Views
2K
Replies
2
Views
868
Replies
7
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Back
Top