i can try The system of ODE's y''=xy, y(0)=0, y'(0)=1 solve in runge kutta method, h=0.05 and interval is [-10 2]

2 次查看(过去 30 天)
function [x, u]= RK4_sys12(f, tspan, u0, n)
a=tspan(1);
b=tspan(2);
h=(b-a)/n;
x=(a:h:b)';
u(1,:)=u0;
for i=1:n
k1=h*feval(f, x(i), u(i,:) )';
k2=h*feval(f, x(i)+h/2, u(i,:)+k1/2)';
k3=h*feval(f, x(i)+h/2, u(i,:)+k2/2)';
k4=h*feval(f, x(i)+h, u(i,:)+k3)';
u(i+1, :)=u(i, :)+ k1/6 +k2/3 +k3/3 +k4/6;
end
%x=x
%u=u
%write on command window
clf; clc;
f=inline('[u(2); x*u(1)]','x','u');
[x, u]=RK4_sys12(f, [-10,2],[0,1], 50);
plot(x,u(:,2),'k') % i can take u(2)
hold on;grid on;
plot(x,u(:,1))
%disp(' x u(1) u(2)')
disp([x u])

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by