Solve x′′ = −x − α(x2 − 1)x′, 0 ≤ t ≤ 30, with the initial conditions x(t = 0) = 0.5 and x′(t = 0) = 0, using RK4 method in your computer where α = 1. Plot the solution.

3 次查看(过去 30 天)
Urgent. Please solve it with explanation as soon as possible.
  3 个评论
KAUSHIK JAS
KAUSHIK JAS 2019-9-12
编辑:KAUSHIK JAS 2019-9-12
I am a beginner of matlab.Not able to write this code. If you can do then answer it.
James Tursa
James Tursa 2019-9-12
编辑:James Tursa 2019-9-12
There are many people on this forum that can write the code for this, but we don't do that for homework problems. You must show some effort first, and then we can help you solve your coding problems. To code an RK4 scheme from scratch, I would first suggest you look at the equations here and try to code them up:
The RK4 equations are spelled out. Just remember that your "y" and "k"s will be 2-element vectors since you have a 2nd order ODE to solve.

请先登录,再进行评论。

回答(1 个)

KAUSHIK JAS
KAUSHIK JAS 2019-9-22
I solve it correctly by my own. It is in below.
%RK2 of two variables
h=0.5;
t=0:h:30;
x=zeros(1,length(t));
z=zeros(1,length(t));
alpha=1.0;
x(1)=0.5;
z(1)=0;
f=@(p,q,r) (r);
g=@(p,q,r) (-q-alpha*(q^2-1)*r);
for i=1:(length(t)-1)
k11=h*f(t(i),x(i),z(i));
k12=h*g(t(i),x(i),z(i));
k21=h*f(t(i)+0.5*h,x(i)+0.5*k11,z(i)+0.5*k12);
k22=h*g(t(i)+0.5*h,x(i)+0.5*k11,z(i)+0.5*k12);
k31=h*f(t(i)+0.5*h,x(i)+0.5*k21,z(i)+0.5*k22);
k32=h*g(t(i)+0.5*h,x(i)+0.5*k21,z(i)+0.5*k22);
k41=h*f(t(i)+h,x(i)+k31,z(i)+k32);
k42=h*g(t(i)+h,x(i)+k31,z(i)+k32);
x(i+1)=x(i)+(1/6)*(k11+2*k21+2*k31+k41);
z(i+1)=z(i)+(1/6)*(k12+2*k22+2*k32+k42);
end
subplot(2,1,1);
plot(t,x);
subplot(2,1,2);
plot(t,z);

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by