second order differential equation
古いコメントを表示
Hi, I wanted to know where I was wrong in the program because the plot should come with a spiral and not a straight line
u=0.1; %damping parameter
h=0.4; %sampling step
x(1) = 2; %initial values
y(1) = 1.5;
for k=1:200
%runge kutta 4th order
K1=h*vandp(x(k), y(k));
K2=h*vandp(x(k)+h/2, y(k)+K1/2);
K3=h*vandp(x(k)+h/2, y(k)+K2/2);
K4=h*vandp(x(k)+h, y(k)+K3);
y(k+1) = y(k) + K1/6+K2/3+K3/3+K4/6;
x(k+1) = h + x(k);
end
plot(x,y);
%%%%%%%%%%%%%%%%%%%
%function van der pol
function van = vandp(x,y)
u = 0.1;
x(1) = 2;
y(1) = 1.5;
van=-u*(x^2-1)*y-x;
回答 (1 件)
Torsten
2017 年 11 月 20 日
function van = vandp(x,y)
u = 0.1;
%x(1) = 2;
%y(1) = 1.5;
van=-u*(x^2-1)*y-x;
Best wishes
Torsten.
3 件のコメント
Kevin Savic
2017 年 11 月 20 日
Torsten
2017 年 11 月 20 日
Try to put it in one file, name it "main.m":
function main
u = 0.1; %damping parameter
h = 0.4; %sampling step
x(1) = 2; %initial values
y(1) = 1.5;
for k=1:200
%runge kutta 4th order
K1=h*vandp(x(k), y(k));
K2=h*vandp(x(k)+h/2, y(k)+K1/2);
K3=h*vandp(x(k)+h/2, y(k)+K2/2);
K4=h*vandp(x(k)+h, y(k)+K3);
y(k+1) = y(k) + K1/6+K2/3+K3/3+K4/6;
x(k+1) = h + x(k);
end
plot(x,y);
%%%%%%%%%%%%%%%%%%%
function van = vandp(x,y)
u = 0.1;
van = -u*(x^2-1)*y-x;
Kevin Savic
2017 年 11 月 20 日
カテゴリ
ヘルプ センター および File Exchange で Runge Kutta Methods についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!