4th order Runge Kutta for a system of ODEs
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a matlab code that i found in my textbook to implement 4th order Runge Kutta for a system of ODEs but I am know sure how to input my dydt or my varargin. here is what i have...
C = -.95121212 b = 1.479843791 k = 67.1170859 m=1 f(t)=sin(0.5t)
State Space X1 = u (X1)dot = u_dot = X2 X2 = u_dot (X2)dot = u_doubledot = f(t)-[(k*X1^b)/m]-[(c*X2)/m]
my output should be a bunch of y-values that i can graph with my t-values. Any help would be greatly appreciated
function [ tp,yp ] = rk4sys( dydt,tspan,y0,h,varargin )
%4th order Runge Kutta for a system of ODE's
% Detailed explanation goes here
n=length(tspan);
ti=tspan(1);
tf=tspan(n);
if n== 2
t=(ti:h:tf);
n=length(t);
if t(n)<tf
t(n+1)=tf;
n=n+1;
end
else t=tspan;
end
tt=ti;
y(1,:)=y0;
np=1;
tp(np)=tt;
yp(np,:)=y(1,:);
i=1;
while(1)
tend=t(np+1);
hh=t(np+1)-t(np);
while(1)
if hh>h,hh=h;
end
while(1)
if tt+hh>tend,hh=tend-tt;
end
k1=dydt(tt,y(i,:),varargin{:})';
ymid=y(1,:)+k1.*hh./2;
k2=dydt(tt+hh/2,ymid,varargin{:})';
ymid=y(i,:)+k2*hh/2;
k3=dydt(tt+hh/2,ymid,varargin{:});
yend=y(i,:);
k4=dydt(tt+hh,yend,varargin{:})';
phi=(k1+2*(k2+k3)+k4)/6;
y(i+1,:)=y(i,:)+phi*hh;
tt=tt+hh;
i=i+1;
if tt>=tend,break,end
end
np=np+1;
tp(np)=tt;
yp(np,:)=y(i,:);
if tt>=tf,break,end
end
plot(tp,yp)
end
0 commentaires
Réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!