Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ME3255S2017/lecture_23/eulode.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
29 lines (29 sloc)
1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |