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!