Euler's Method of Numerical Approximations

Click For Summary

Discussion Overview

The discussion revolves around implementing Euler's Method for numerical approximations in MATLAB. Participants explore the algorithm's structure, address coding issues, and discuss the accuracy of results obtained from their implementations.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant shares an algorithm for Euler's Method and their initial MATLAB code implementation.
  • Another participant suggests evaluating the function k = f(t,y) inside the for loop to ensure k is updated with each iteration.
  • There is a clarification about the input method for defining the function f(t,y) and how to use it correctly within the loop.
  • One participant proposes using inline functions to define f(t,y) dynamically based on user input, allowing for correct evaluations in each iteration.
  • Another participant raises concerns about the accuracy of the results compared to those in the reference book, questioning whether the observed errors are due to rounding or the implementation itself.
  • It is noted that the function definition may have been incorrectly ordered, leading to discrepancies in results, which was later corrected.

Areas of Agreement / Disagreement

Participants generally agree on the need to evaluate the function within the loop and the use of inline functions, but there is ongoing uncertainty regarding the acceptable error margins in the results compared to the reference values.

Contextual Notes

Participants express concerns about the accuracy of numerical results, indicating potential limitations in the implementation or inherent errors in the numerical method itself. The discussion does not resolve the extent to which these errors are acceptable.

Who May Find This Useful

Readers interested in numerical methods, MATLAB programming, and those seeking to understand Euler's Method for solving differential equations may find this discussion beneficial.

kishtik
Messages
96
Reaction score
0
Hello,
For Euler's Method of Numerical Approximations, my book (Boyce&DiPrima)
gives this algorithm:

Step 1: define f(t,y)
Step 2: input initial values t0 and y0
Step 3: input step size h and number of steps n
Step 4: output t0 and y0
Step 5: for j from 1 to n do
Step 6: k1 = f(t,y)
y = y + h*k1
t = t + h
Step 7: output t and y
Step 8: end

I tried this on Matlab. My code was:

t=input('Enter t0:');
y=input('Enter y0:');
h=input('Enter step size h:');
n=input('Enter number of steps n:');
k1=input('Define f(t,y):');
result=zeros(n,2);
result(1)=t;
result(1,2)=y;
for j=[2:n+1]
k=k1;
y=y+k*h;
t=t+h;
result(j,1)=t;
result(j,2)=y;
end
disp(' t y');
disp(result);


But I realize that my program calculates k1 only for the initial values... How can I change it so that k1 is actually calculated in every turn of for loop?

Thanks.
 
Last edited:
Physics news on Phys.org
You need to evaluate k = f(t,y) INSIDE the for loop. This way each time you change t and y, you will get a new value for k.

kishtik said:
t=input('Enter t0:');
y=input('Enter y0:');
h=input('Enter step size h:');
n=input('Enter number of steps n:');
k1=input('Define f(t,y):');
result=zeros(n,2);
result(1)=t;
result(1,2)=y;
for j=[2:n+1]
k=k1;

k = f(t,y)
y=y+k*h;
t=t+h;
result(j,1)=t;
result(j,2)=y;
end
disp(' t y');
disp(result);
 
Of course, but the value of k1 is evaluated and assigned at the line
k1=input('Define f(t,y):');
before the loop and the information about the function which should be used to evaluate k's is lost... How can I change it so that it actually evaluates k in the loop using the input k1 as a formula rather than the first of k's?

(Edit: 100th post btw.)
 
kishtik said:
Of course, but the value of k1 is evaluated and assigned at the line
k1=input('Define f(t,y):');
before the loop and the information about the function which should be used to evaluate k's is lost... How can I change it so that it actually evaluates k in the loop using the input k1 as a formula rather than the first of k's?

(Edit: 100th post btw.)

Ok, I thought your input k1 was just the initial value, but you're saying
k1=input('Define f(t,y):');
is supposed to take in a symbolic function f(t,y)?
 
Yes, and that symbolic expression should be used to compute the next k's in every turn of the loop.
 
kishtik said:
Yes, and that symbolic expression should be used to compute the next k's in every turn of the loop.

Ok, I think you want to use inline functions. If you use
k1=input('Define f(t,y):')
and enter something like 't^3+4-5*y' with the ' ', then after that put
f = inline(k1);
and it should define f(t,y) = t^3+4-5*y. Then replace k=k1 with k=f(t,y) insinde your loop. I think that will do what you want.

Code:
t=input('Enter t0:');
y=input('Enter y0:');
h=input('Enter step size h:');
n=input('Enter number of steps n:');
k1=input('Define f(t,y):');
f = inline(k1);
result=zeros(n,2);
result(1)=t;
result(1,2)=y;
for j=[2:n+1]
k=f(t,y);
y=y+k*h;
t=t+h;
result(j,1)=t;
result(j,2)=y;
end
disp(' t y');
disp(result);
 
Thank you! That was just what I was looking for. But now I have another problem...
I modified the code to:

t=input('Enter t0:');
y=input('Enter y0:');
h=input('Enter step size h:');
n=input('Enter number of steps n:');
k1=input('Define f(t,y) in single quotation marks:');
f=inline(k1,'y','t');
result=zeros(n,2);
result(1)=t;
result(1,2)=y;
for j=[2:n+1]
k=f(t,y);
y=y+k*h;
t=t+h;
result(j,1)=t;
result(j,2)=y;
end
disp(' t y');
disp(result);


And tried the example in the book(p.103) with,
t0 = 0,
y0 =1,
h=0.1,
n=4,
f(t,y)=3 + exp(-t) - (1/2)*y,
and got this out:
t _________________y
0________________1.00000000000000
0.10000000000000 1.33678794411714
0.20000000000000 1.65805675248899
0.30000000000000 1.96710763513263
0.40000000000000 2.26609371515479

but the book gave

y0 = 1
y1 = 1.3500
y2 = 1.6730
y3 = 1.9712
y4 = 2.2467

Nearly %2 error for every y. Isn't this too much for an error from rounding or "how my program is constructed" or "how my computer executes arithmetic steps"? The book says "minor variations in the last decimal place" are acceptable, but %2?
 
kishtik said:
k1=input('Define f(t,y) in single quotation marks:');
f=inline(k1,'y','t');
.
.
.
k=f(t,y);

It looks like you have defined f(y,t) and are evaluating f(t,y). So switch one of the two and it should work.
 
It does. :smile:
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 0 ·
Replies
0
Views
2K