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

 Accepted Answer

Amit
Amit on 8 Feb 2014
Edited: Amit on 8 Feb 2014
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 Comments

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
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
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.
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
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
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
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
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))"
@Erin: Open a new Question with your specific problem, what code you have written so far, and what specific things you need help with.
@James I did but I haven't been responded to yet :(

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 8 Feb 2014

Commented:

on 22 Sep 2016

Community Treasure Hunt

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

Start Hunting!