Euler's method for second ODE

1 回表示 (過去 30 日間)
reem
reem 2012 年 12 月 2 日
The equation given is: y′′+2y=0 y(0) = 5 y′(0) = 0
And I know how to solve it with standard methods but I need to solve it in Matlab with Euler's method.
How would I split this second order into 2 first order equations that I could plug into this code? I think it becomes v=y' and then v'=-2y... but I do not know how to plug this with v and y.
Here is example code with y'=-y (NOT second order)
% Numerical code for Euler integration of y'=-y; y(0)=1;
Tstart=0; %start Time Tstop=10; %Stop Time N=20; %Number of Time steps h=(Tstop-Tstart)/N; %Time step length
Time=[Tstart:h:Tstop]; %Time vector
y=zeros(1,length(Time)); %Solution vector
y(1)=1;
for i=2:length(Time)
y(i)=y(i-1)+h*(-y(i-1)); %Euler iteration
end
figure;plot(Time,y,'.k');
y_analytic=exp(-Time); %analytic solution
hold;plot(Time,y_analytic,'r');
THANKS!

回答 (1 件)

bym
bym 2012 年 12 月 2 日
you are very close, try defining v as
v = zeros(length(t),2);
v (1,:)= [5,0]; % initial conditions
then write your y (from your example) in terms of [v, v']

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by