Euler method and Graph
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I have to solve a an ODE with the Euler method for two separate step sizes and have to place them on same graph. I'm very confused on how to do this... Please help me...
y'=1/y; Initial Condition y(0) = 1;
I have to use Euler method to solve for y(1) for step size deltat = 0.1 and also deltat = 0.01
This is as far as I got and I'm just completely stuck.....
t0=0; t1=1; dt=0.1; %define time range and step size
y0=1; %initial condition
t=t0:dt:t1;
y=zeros(size(t,1), size(t,2))
for i=1: ((t1-t0)/dt)
y(i+1)=y(i)+1/y(i)*dt
end
4 commentaires
Walter Roberson
le 23 Fév 2020
t0=0; t1=1; dt=0.1; %define time range and step size
y0=1; %initial condition
t=t0:dt:t1;
y=zeros(size(t));
num_t = length(t);
for i = 1: num_t - 1
y(i+1)=y(i)+1/y(i)*dt
end
Réponses (1)
Pravin Jagtap
le 25 Fév 2020
Modifié(e) : Pravin Jagtap
le 25 Fév 2020
Hello Nasir,
Refer to the following function which takes ‘dt’ and ‘y0’ as an input argument and returns the ‘y’ as solution for time vector from 0 to 1 with ‘dt’ as timestep (This is just a demonstrative example. You can modify as per your requirements).
function y = EulerMethod(dt, y0)
% Preparing the Time vector from tStart to tEnd
t_Start = 0;
t_End = 1;
t = t_Start:dt:t_End;
% Initialize the solution vector
y = zeros(size(t));
% Impose Initial Condition
y(1) = y0;
% Compute the Solution vector using the Eulers Method
num_t = length(t);
for i = 1: num_t - 1
y(i+1)=y(i)+((1/y(i))*dt);
end
end
Call the above function for different values of ‘dt’ as follows and plot it
% call the function for dt 0.1
y1 = EulerMethod(0.1,1);
% call the function for dt 0.01
y2 = EulerMethod(0.01,1);
% Plot the corresponding solutions
t1 = 0:0.1:1;
plot(t1,y1)
hold on
t2 = 0:0.01:1;
plot(t2,y2)
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!