• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# ode_RK

UtamaDonny Dec 9th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function [t,y] = ode_RK4(f,tspan,y0,N,varargin)
2. %Runge-Kutta method to solve vector differential eqn y’(t) = f(t,y(t))
3. % for tspan = [t0,tf] and with the initial value y0 and N time steps
4.
5. if nargin &lt; 4 | N &lt;= 0, N = 100; end
6. if nargin &lt; 3, y0 = 0; end
7. y(1,:) = y0(:)'; %make it a row vector
8. h = (tspan(2) - tspan(1))/N; t = tspan(1)+[0:N]'*h;
9. for k = 1:N
10. f1 = h*feval(f,t(k),y(k,:),varargin{:}); f1 = f1(:)'; %(6.3.2a)
11. f2 = h*feval(f,t(k) + h/2,y(k,:) + f1/2,varargin{:}); f2 = f2(:)';%(6.3.2b)
12. f3 = h*feval(f,t(k) + h/2,y(k,:) + f2/2,varargin{:}); f3 = f3(:)';%(6.3.2c)
13. f4 = h*feval(f,t(k) + h,y(k,:) + f3,varargin{:}); f4 = f4(:)'; %(6.3.2d)
14. y(k + 1,:) = y(k,:) + (f1 + 2*(f2 + f3) + f4)/6; %Eq.(6.3.1)
15.
16. end
17. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top