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;

Answers (1)

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

There are two separate programs, if you comment the initial values, matlab will give me a mistake saying that i did not put x and y
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;
Ok now is working, thank you very much

Sign in to comment.

Tags

Asked:

on 20 Nov 2017

Edited:

on 20 Nov 2017

Community Treasure Hunt

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

Start Hunting!