Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rcc02007 committed Dec 20, 2019
0 parents commit b75c4fc
Show file tree
Hide file tree
Showing 30 changed files with 24,315 additions and 0 deletions.

Large diffs are not rendered by default.

821 changes: 821 additions & 0 deletions 18_initial_value_ode/.ipynb_checkpoints/lecture_22-checkpoint.ipynb

Large diffs are not rendered by default.

Binary file added 18_initial_value_ode/octave-workspace
Binary file not shown.
3,482 changes: 3,482 additions & 0 deletions 19_nonlinear_ivp/.ipynb_checkpoints/19_nonlinear_ivp-checkpoint.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
29 changes: 29 additions & 0 deletions 19_nonlinear_ivp/eulode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function [t,y] = eulode(dydt,tspan,y0,h,varargin)
% eulode: Euler ODE solver
% [t,y] = eulode(dydt,tspan,y0,h,p1,p2,...):
% uses Euler's method to integrate an ODE
% input:
% dydt = name of the M-file that evaluates the ODE
% tspan = [ti, tf] where ti and tf = initial and
% final values of independent variable
% y0 = initial value of dependent variable
% h = step size
% p1,p2,... = additional parameters used by dydt
% output:
% t = vector of independent variable
% y = vector of solution for dependent variable
if nargin<4,error('at least 4 input arguments required'),end
ti = tspan(1);tf = tspan(2);
if ~(tf>ti),error('upper limit must be greater than lower'),end
t = (ti:h:tf)'; n = length(t);
% if necessary, add an additional value of t
% so that range goes from t = ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
end
y=zeros(n,length(y0));
y(1,:)=y0;
for i = 1:n-1 %implement Euler's method
y(i+1,:) = y(i,:) + dydt(t(i),y(i,:),varargin{:})'*(t(i+1)-t(i));
end
Binary file added 19_nonlinear_ivp/octave-workspace
Binary file not shown.
6 changes: 6 additions & 0 deletions 19_nonlinear_ivp/predprey.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function yp=predprey(t,y,a,b,c,d)
% predator-prey model (Lotka-Volterra equations)
yp=zeros(2,1);
yp(1)=a*y(1)-b*y(1)*y(2);
yp(2)=-c*y(2)+d*y(1)*y(2);
end
53 changes: 53 additions & 0 deletions 19_nonlinear_ivp/rk4sys.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function [tp,yp] = rk4sys(dydt,tspan,y0,h,varargin)
% rk4sys: fourth-order Runge-Kutta for a system of ODEs
% [t,y] = rk4sys(dydt,tspan,y0,h,p1,p2,...): integrates
% a system of ODEs with fourth-order RK method
% input:
% dydt = name of the M-file that evaluates the ODEs
% tspan = [ti, tf]; initial and final times with output
% generated at interval of h, or
% = [t0 t1 ... tf]; specific times where solution output
% y0 = initial values of dependent variables
% h = step size
% p1,p2,... = additional parameters used by dydt
% output:
% tp = vector of independent variable
% yp = vector of solution for dependent variables
if nargin<4,error('at least 4 input arguments required'), end
if any(diff(tspan)<=0),error('tspan not ascending order'), end
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);
if hh>h,hh = h;end
while(1)
if tt+hh>tend,hh = tend-tt;end
k1 = dydt(tt,y(i,:),varargin{:})';
ymid = y(i,:) + 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,:) + k3*hh;
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
Loading

0 comments on commit b75c4fc

Please sign in to comment.