Euler method and Graph
조회 수: 14 (최근 30일)
이전 댓글 표시
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
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
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!