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
Torsten 2017년 11월 20일

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

Kevin Savic
Kevin Savic 2017년 11월 20일
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;
Kevin Savic
Kevin Savic 2017년 11월 20일
Ok now is working, thank you very much

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2017년 11월 20일

편집:

2017년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by