필터 지우기
필터 지우기

How to solve a system of ODE's by using Forward Euler method?

조회 수: 17 (최근 30일)
Momo
Momo 2018년 4월 14일
댓글: Jan 2019년 11월 4일
How I can rewrite the following code for a system ODE's y(t)=[y_1 y_2]'. Also, how can I write f( t(i) , y(i)) ,which is equal to f(t,y)=A *y, as a function.
function [y , t ] = forwardEuler (f , t0 ,T , y0 , N )
%Solve dy/dt = f(t,y) , y(t0 )= y0
h = ( T - t0 )/( N -1); % Calulate and store the step - size
t = linspace( t0 ,T , N ); % A vector to store the time values .
y = zeros (1 , N ); % Initialize the Y vector .
y (1) = y0 ; % Start y at the initial value .
for i = 1:( N -1)
y (i +1)= y(i)+ h*f( t(i) , y(i)); % Update approximation y at t+h
end

답변 (1개)

Jan
Jan 2018년 4월 15일
편집: Jan 2018년 4월 15일
Replace
y = zeros (1 , N ); % Initialize the Y vector .
y (1) = y0 ; % Start y at the initial value .
by
y = zeros (numel(y0) , N); % Initialize the Y matrix .
y(:, 1) = y0(:); % Start y at the initial value .
And
y (i +1)= y(i)+ h*f( t(i) , y(i)); % Update approximation y at t+h
by
y(:, i +1) = y(:, i) + h * f(t(i), y(:, i)); % Update approximation y at t+h
Then your f(t,y) is simply:
function dy = f(t, y)
A = ???
dy = A * y;
end
Hint: It improves the readability of code to use a fixed scheme of inserting spaces. Some users prefer: spaces around operators and the equal character. No spaces after parenthesis and before commas.
  댓글 수: 2
Hesham Hendy
Hesham Hendy 2019년 10월 26일
편집: Hesham Hendy 2019년 10월 26일
I have a question for you !
In your dy = f(t,y) you pass the time instant as input argument but you don't use it !
Should the time instant be used by the calculation or not ? and why ??
Jan
Jan 2019년 11월 4일
@Hesam Hendy: It depends on the function to be integrated if it depends on t or not.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by