The following pages contain a collection of example files used in class at one time or another. There should be one file per page. Some lack comments, others have comments.
Some are files used to determine acceptable answers to homework problems.
function [ v, ea, iter ] = e2x(x, es, maxit)
% ***********************************************************************
% This is a function to calculate the approximate value of exp(x)using the
% general algorithm given in the text.
%
% J. Reising
%
%************************************************************************
% initial values
iter = 1;
sol = 1;
ea = 100;
% iterative calculations
while 1
solold = sol;
sol = sol + x^iter/factorial(iter);
iter = iter + 1;
if sol ~= 0
ea = abs(sol - solold)/abs(sol)*100;
end
if ea <= es || iter >= maxit, break, end
end % while
v = sol;
end % function
% Freefall.m
%
% This example uses Euler's method to find the velocity of a freefalling
% object given it's mass m and drag coefficient c. The model assumes a
% drag force proportional to the square of the object's velocity.
%
% Input: m, the mass in kg and c, the drag coefficient
%
% Output: the vectors t and v
%
% Note: In order to compare the results with the earlier example done in
% Excel, a time step of 0.1 seconds is used. In a more general program,
% the time step would be an input.
%
% Author: J Reising 5 Feb 2014
%
% ************************************************************************
%
g = 9.81; % the acceleration of gravity, assumed constant
fprintf('\n')
m = input('\nEnter the mass of the object in kg ');
c = input('\nNow enter the drag coefficient c ');
fprintf('\n')
t =[0:.1:10];
v = zeros(1,length(t)); %create a vector v the same length as t
for i = 2:length(t)
v(i) = v(i-1) + 0.1*(g-c/m*v(i-1)^2);
end
table = [t; v]';
plot(t,v)
for j = 1:10:101
% fprintf('t = %8.4f v = %8.4f\n',table(j,1),table(j,2))
fprintf('t = %8.4f v = %8.4f\n',t(j),v(j))
end
function [xr, iter, ea ] = Bisect( xl, xu, es, imax )
%Bisect computes an approximate value of the root of a function written as
%a separate function file fun(x).
% Detailed explanation goes here
iter = 0;
while 1
xrold = xl;
xr = (xl + xu)/2;
iter = iter + 1;
if xr~= 0 ea = abs((xr-xrold)/xr)*100;
end
test = fun(xl)*fun(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea < es || iter >= imax, break, end
end % while
end %Bisect
function y = fun(x)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
y=2.^(1-x)-2.220446e-16;
end
Above function used to compute function values.
function [ v, ea, iter ] = GenIter(guess, n, maxit)
% ***********************************************************************
% This is a function to calculate the approximate root v of a function
% using the simple fixed-point iteration.
%
% Input:
% guess is the initial guess
% n is the number of significant figures desired in result
% maxit is the maximum number of iterations allowed.
%
% J. Reising
%
%************************************************************************
% initial values
iter = 0
sol = guess
ea = 100;
es = .5 * 10^(2-n)
% iterative calculations
while 1
solold = sol;
sol = exp(-sol)
iter = iter + 1
if sol ~= 0
ea = abs((sol - solold)/sol)*100;
end
if ea <= es || iter >= maxit, break, end
end % while
v = sol;
end % function
function [ xr, iter, ea ] = FalsePos(xl, xu, es, imax)
%FalsePos computes an approximate value of the root of a function written as
%a separate function file fun(x).
% Detailed explanation goes here
iter = 0;
while 1
xrold = xl;
xr = xu + fun(xu)*(xu-xl)/(fun(xl)-fun(xu));
iter = iter + 1;
if xr~= 0
ea = abs((xr-xrold)/xr)*100;
end
test = fun(xl)*fun(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea < es || iter >= imax, break, end
end % while
end %FalsePos
function [ xr, iter, ea ] = ModFalsePos( xl, xu, es, imax)
%%ModFalsePos computes an approximate value of the root of a function written as
%a separate function file fun(x).
% Detailed explanation goes here
iter = 0;
il = 0;
iu = 0;
fl = fun(xl);
fu = fun(xu);
while 1
xrold = xl;
xr = xu + fun(xu)*(xu-xl)/(fl-fu);
fr = fun(xr);
iter = iter + 1;
if xr~= 0
ea = abs((xr-xrold)/xr)*100;
end
test = fl*fr;
if test < 0
xu = xr;
fu = fun(xu);
iu = 0;
il = il +1;
if il >= 2 % if this pass is executed more than twice in a row
fl = fl/2;
end
elseif test > 0
xl = xr;
fl = fun(xl);
il = 0;
iu = iu + 1;
if iu >= 2 % if this pass is executed more than twice in a row
fu = fu/2;
end
else
ea = 0;
end
if ea < es || iter >= imax, break, end
end % while
end
function [ v, ea, iter ] = NewtRaph(guess, n, maxit)
% ***********************************************************************
% This is a function to calculate the approximate root v of a function
% using the Newton-Raphson method.
%
% Input:
% guess is the initial guess
% n is the number of significant figures desired in result
% maxit is the maximum number of iterations allowed.
%
% J. Reising
%
%************************************************************************
% initial values
iter = 0
sol = guess
ea = 100;
es = .5 * 10^(2-n)
% iterative calculations
while 1
solold = sol;
sol = sol - (sol-exp(-sol))/(1+exp(-sol))
iter = iter + 1
if sol ~= 0
ea = abs((sol - solold)/sol)*100;
end
if ea <= es || iter >= maxit, break, end
end % while
v = sol;
end % function
function [r,theta] = rect2pol( x,y )
% Given rectangular coordinates x and y, the function returns polar
% coordinates r and theta.
r =sqrt(x.^2+y.^2);
if x > 0
theta = atand(y/x);
elseif x < 0
if y > 0
theta = atand(y/x) + 180;
elseif y < 0
theta = atand(y/x)-180;
else
theta = 180;
end
else
if y > 0
theta = 90;
elseif y < 0
theta = - 90;
else
theta = 0;
end
end
Script file used to do homework 5
g = 9.81;
m = 90;
c = 0.225;
t = 0:.1:100;
v = zeros(1,length(t));
x = zeros(1,length(t));
n = 1;
while 1
v(n+1) = v(n) + 0.1*(g-c/m*v(n)^2);
x(n+1) = x(n) + 0.1*v(n);
if x(n+1)>= 1000, break, end
n = n +1;
end
time = (t(n+1)+t(n))/2
vel = (v(n+1) + v(n))/2
function [ v, ea, iter ] = cosxJAR(x, n, maxit)
% ***********************************************************************
% This is a function to calculate the approximate value of cos(x)using the
% general algorithm given in the text.
%
% Input: x, n = significant figures in result, maxit = maximum number of
% iterations.
%
% Output: v, the value computed; ea, the approximate relative error, and
% iter, the number of iterations
%
% J. Reising
%
%************************************************************************
% initial values
iter = 1;
sol = 1;
ea = 100;
es = 0.5*10^(2-n);
% iterative calculations
while 1
solold = sol;
sol = sol + (-1)^iter * x^(2*iter)/factorial(2*iter);
iter = iter + 1;
if sol ~= 0
ea = abs(sol - solold)/sol*100;
end %if
if ea <= es || iter >= maxit, break, end
end % while
v = sol;
end % function cosx