Euler Method without using ODE solvers such as ode45

I am trying to write a code that will solve a first order differential equation using Euler's method. I do not want to use an ode solver but rather would like to use numerical methods which allow me to calculate slope (k1, k2 values, etc). I am given an equation with two different step values. I am not sure how to begin to write this in MATLAB. I have solved the equation by hand and am now trying to write a code that solves that equation.
The equation to be used is y’ +2y = 2 – e -4t.
y(0) = 1, which has an exact solution:
y(t) = 1 + 0.5 e -4t - 0.5 e -2t .
I did the following to solve numerically: 1+(0.5e^-4t) - (0.5e^-2(0)) y_n+1=1+0.1 and y_n+1=1+1(.001)
The step sizes are 0.1 and 0.001. (so my h in this example). The t values range from 0.1 to 5.0.
Any help is appreciated even if its an example pseudocode. Thank you

 採用された回答

Amit
Amit 2014 年 2 月 8 日
編集済み: Amit 2014 年 2 月 8 日

0 投票

Given the equation:y' + 2y = 2 - 1e-4t The approximation will be: y(t+h) = y(t) +h*(- 2*y(t)+ 2 - e(-4t))
To write this: EDIT
y = 1; % y at t = 0
h = 0.001;
t_final = 0.1;
t = 0;
while (t < t_final)
y = y +h*(- 2*y+ 2 - exp(-4*t));
t = t + h;
end

11 件のコメント

KayLynn
KayLynn 2014 年 2 月 8 日
My mistake the equation should be: y’ +2y = 2 – e^(-4t). Meaning that e is raised to the negative 4t.
y(t) = 1 + 0.5 e^(-4t) - 0.5 e^ (-2t)
Sorry for the confusion
KayLynn
KayLynn 2014 年 2 月 8 日
The following is the code that I am trying to implement. I am unsure of the total steps since t ranges from 0 to 5 in increments of 0.1. ANy help is again appreciated. Thank you %Rearrange your equation to represent the following: y'= 2-e^-4*t-2*y; %define your f(t,y) f(t,y)=2-e^-4*t-2*y; %define initial t and initial y; define step size(h) and number of steps(n) t0=0; y0=1; h=0.1; n= %Write for loop for i=1:numel(n)-1 m=f(t0,y0); y=y0 +h*m; t1=t0+ht0; Print t1,y1; t0=t1; y0=y1; end
Amit
Amit 2014 年 2 月 8 日
I have corrected the code accordingly. Your code will be similar to the one I wrote above.
Can you format your code with code attributes so that I can see it.
KayLynn
KayLynn 2014 年 2 月 8 日
y'= 2-e^-4*t-2*y;
f(t,y)=2-e^-4*t-2*y;
t0=0; y0=1; h=0.1; n=
for i=1:numel(n)-1
m=f(t0,y0);
y=y0 +h*m;
t1=t0+ht0;
Print t1,y1;
t0=t1;
y0=y1;
end
I am not sure the number of total steps(n) since my t values go from 0 to 5.0 with a step size of 0.1
Was using this code I found online: define f(t,y)
input t0 and y0.
input step size, h and the number of steps, n.
for j from 1 to n do m = f (t0, y0) y1 = y0 + h*m t1 = t0 + h Print t1 and y1 t0 = t1 y0 = y1 end
Amit
Amit 2014 年 2 月 8 日
Okay if you want to follow this way, then try this:
f = @(t,y) (2 - exp(-4*t) - 2*y);
h = 0.1; % Define Step Size
t_final = 5;
t = 0:h:t_final;
y = zeros(1,numel(t));
y(1) = 1; % y0
% You know the value a t = 0, thats why you'll state with t = h i.e. i = 2
for i = 2:numel(t)
y(i) = y(i-1) + h*f(t(i-1),y(i-1));
disp([t(i) y(i)]);
end
KayLynn
KayLynn 2014 年 2 月 8 日
Thank you so much. And then for my step size of 0.001 I would just change the h variable in said above code to 0.001
Amit
Amit 2014 年 2 月 8 日
Yup !
John D'Errico
John D'Errico 2014 年 2 月 8 日
How can you be unsure of the total number of steps if you know the stepsize, and you know how far it must go? Surely division is the proper tool here. 5/0.1
Erin W
Erin W 2016 年 9 月 20 日
Amit,
How did you determine the approximation for the equation?
I have a similar problem I am trying to solve only I have y' = ry(1-y/K) where r = 1, K = 10, and yo = 1.
I understand the code that you have written but I'm unsure of how to alter my function.
"Given the equation:y' + 2y = 2 - 1e-4t The approximation will be: y(t+h) = y(t) +h*(- 2*y(t)+ 2 - e(-4t))"
James Tursa
James Tursa 2016 年 9 月 20 日
@Erin: Open a new Question with your specific problem, what code you have written so far, and what specific things you need help with.
Erin W
Erin W 2016 年 9 月 22 日
@James I did but I haven't been responded to yet :(

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2014 年 2 月 8 日

コメント済み:

2016 年 9 月 22 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by