Euler method and Graph

20 次查看(过去 30 天)
Nasir Holliday
Nasir Holliday 2020-2-22
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 个评论
Nasir Holliday
Nasir Holliday 2020-2-23
I'm sorry but I'm not really understanding
Walter Roberson
Walter Roberson 2020-2-23
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

请先登录,再进行评论。

回答(1 个)

Pravin Jagtap
Pravin Jagtap 2020-2-25
编辑:Pravin Jagtap 2020-2-25
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)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by