求助!Matlab四阶R-K法求解一个二阶非线性微分方程;x’’+x^3/(1+x^2)=0;初始条件 x(0)=A x’(0)=0
clc
A=1;
h=0.01;
x=[A 0]
X=[];
t=0:h:20;
for i=1:length(t)
X_1 = Runge_Kutta_4(t(i),x,@matlab4rk,h);
X=[X;t(i),X_1];
x=X_1;
end
string1=['A=',num2str(A)];
A=2;
h=0.01;
x=[A 0]
X1=[];
t=0:h:20;
for i=1:length(t)
X1_1 = Runge_Kutta_4(t(i),x,@matlab4rk,h);
X1=[X1;t(i),X1_1];
x=X1_1;
end
plot(X(:,1),X(:,2),'-k',X1(:,1),X1(:,2),'-b','linewidth',2);
hold on
grid on
xlabel('t-time','fontsize',14)
ylabel('x','fontsize',14)
tstring=['x"+x^3/(1+x^2)=0'];
title(tstring,'fontsize',14);
string2=['A=',num2str(A)];
legend(string1,string2)
function Y = Runge_Kutta_4(t,X,f,h)
K1=f(t,X);
K2=f(t+h/2,X+h/2*K1);
K3=f(t+h/2,X+h/2*K2);
K4=f(t+h,X+h*K3);
Y=X+h/6*(K1+2*K2+2*K3+K4);
function dx=matlab4rk(t,x)
% x1=x;x2=x'
dx=zeros(1,2);
dx(1)=x(2);
dx(2)=-x(1)^3/(1+x(1)^2);
真的很感谢你,这个问题已经苦恼我很久了,thank you very much!!