total number steps is (n=50 or n=100)
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]
3 views (last 30 days)
Show older comments
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])
Answers (0)
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!