second order differential equation
Show older comments
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;
Answers (1)
Torsten
on 20 Nov 2017
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 Comments
Kevin Savic
on 20 Nov 2017
Torsten
on 20 Nov 2017
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
on 20 Nov 2017
Categories
Find more on Runge Kutta Methods in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!