diff options
| author | Christian Kolset <christian@kolset.com> | 2021-07-09 23:50:15 -0600 |
|---|---|---|
| committer | Christian Kolset <christian@kolset.com> | 2021-07-09 23:50:15 -0600 |
| commit | 0f1c29b7c59ad4499168e0f521cf0522d01cc594 (patch) | |
| tree | 7a3265be4569dd6dd4bc3809232cd8a11f13e788 /Functions/Heun | |
| parent | 34dd12861a9a90bf88ee9abc6d8398f93f1a7b01 (diff) | |
Added Functions and Scripts
Diffstat (limited to 'Functions/Heun')
| -rw-r--r-- | Functions/Heun/Heun.m | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Functions/Heun/Heun.m b/Functions/Heun/Heun.m new file mode 100644 index 0000000..5812dfe --- /dev/null +++ b/Functions/Heun/Heun.m @@ -0,0 +1,44 @@ +function [t,y] = Heun(dydt,tspan,y0,h,es,maxit) +% [t,y] = Heun(dydt,tspan,y0,h): uses the heun method to integrate an ODE +% inputs: +% dydt = the differential equation of interest (must be anonymous +% function) +% tspan = [ti,tf] the initial and final values of the independent +% variable +% y0 = the initial value of the dependent variable +% h = step size +% es = stopping criterion (%), optional (default = 0.001) +% maxit = maximum iterations of corrector, optional (default = 50) +% outputs: +% t = vector of independent variable values +% y = vector of solution for dependent variable + +%Error checking +if nargin<4, error('At least 4 aruments required.');end +if nargin<5|isempty(es), es = 0.001;end +if nargin<6|isempty(maxit), maxit = 50;end +if length(tspan)~=2, error('Argument tspan must have a length of 2');end + +%Trims tspan according to step size (h) +t=tspan(1):h:tspan(2); +%if rem(tspan(2),h)~=0 +if t(length(t))~=tspan(2) + t=[t,tspan(end)]; +end + +y=ones(1,length(t))*y0; +h=diff(t); + +for i=1:length(t)-1 + S=dydt(t(i),y(i)); + ynew=y(i)+S*h; + for j=1:maxit + yold=ynew; + ynew=y(i)+h(i)/2*(S+dydt(t(i+1),yold)); + ea = abs((ynew-yold)/ynew)*100; + if ea <= es, break, end + end + y(i+1)=ynew; +end +plot(t,y) +end
\ No newline at end of file |
