MHB Comparing Methods for Initial Value Problem

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Compare
Click For Summary
The discussion focuses on solving an initial value problem using various numerical methods: forward Euler, trapezoidal, and backward Euler. The participants analyze the results, noting that the graphs of the approximations for the methods appear similar, yet the forward Euler method produces a non-smooth graph, indicating a lack of accuracy. It is suggested that comparing these methods to the exact solution or a more accurate approximation, such as one from Octave's ODE solver, would provide better insights into their effectiveness. Additionally, the conversation touches on the mathematical properties of the system, including the Lipschitz condition and the implications of smoothness in the graphs. Ultimately, the discussion emphasizes the importance of error analysis in evaluating the performance of numerical methods.
  • #31
I tried:
Code:
t2=[1 2];
y2=[3 4; 5 6];
plot(t2, y2(1,:).^2+y2(2,:).^2, 'y');

That works... (Thinking)

Did you perhaps put those functions in script files?
Or are you running the code on the prompt?

Btw, your current functions are not syntactically correct since 'for' has an 'end', but 'function' does not have an 'end'. (Worried)
 
Mathematics news on Phys.org
  • #32
Klaas van Aarsen said:
I tried:
Code:
t2=[1 2];
y2=[3 4; 5 6];
plot(t2, y2(1,:).^2+y2(2,:).^2, 'y');

That works... (Thinking)

Did you perhaps put those functions in script files?
Or are you running the code on the prompt?

Btw, your current functions are not syntactically correct since 'for' has an 'end', but 'function' does not have an 'end'. (Worried)

I wrote that code in a script .m and when I run it it is asked to enter a value for N. I give for example 100 and then I get the error message.

I did that in https://octave-online.net/

So, when you run that code it is executed normally without any error? (Thinking)
 
  • #33
evinda said:
I wrote that code in a script .m and when I run it it is asked to enter a value for N. I give for example 100 and then I get the error message.

I did that in https://octave-online.net/

So, when you run that code it is executed normally without any error?

If I copy and paste your code in octave-online, I don't get any response. (Worried)

I expected that since those functions are not terminated with the [M]end[/M] that should come after. (Nerd)

What did you put in octave-online exactly?
How did you get the error? (Wondering)
 
  • #34
There are two functions that I want to plot. Here you can see the functions I wrote then I press RUN and enter 100 as the value for N and as you can see I get again an error message. What have I done wrong? (Thinking)

View attachment 9067
 

Attachments

  • run.jpg
    run.jpg
    56.5 KB · Views: 115
  • #35
evinda said:
There are two functions that I want to plot. Here you can see the functions I wrote then I press RUN and enter 100 as the value for N and as you can see I get again an error message. What have I done wrong?

If we put a function into a file named [M]ex.m[/M], then that file must contain exactly a function named [M]ex[/M] and no other statements.
Since that is not the case, we get an error that is unfortunately not very descriptive. (Worried)If we copy and paste your code into the regular command prompt of Octave online, we get:
View attachment 9068

Now we get the error that the variables are not known as I explained. (Nerd)Alternatively, we can create a new file named [M]trapezoid_method.m[/M].
Put the code of the function in there, and only that function.
Click the save button.
Type [M]trapezoid_method(10)[/M] on the command prompt.
Now it will execute the function as desired. (Happy)
 

Attachments

  • Octave.png
    Octave.png
    26.7 KB · Views: 111
  • #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: 108
  • #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: 98
  • #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: 108

Similar threads

Replies
20
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
7K
  • · Replies 11 ·
Replies
11
Views
3K