You are on page 1of 8

Bisection Method

Write the following code and save it as a Malab function file


function c = bisect(f, a, b, delta)

fa = f(a); %% compute initial values of f(a) and f(b)


fb = f(b);
n = 0;

if sign(fa) == sign(fb) %% sanity check: f(a) and f(b) must have


%% different signs
%%
%% the error function prints an error message and
%% exits
error('f must have different signs at the endpoints a and b.
Aborting.')
end
fprintf('initial interval: a=%d, b=%d, fa=%d, fb=%d\n',a,b,fa,fb)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% main routine %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

while ( abs(b - a) > 2*delta ) %% While the size of the interval is


%% larger than the tolerance

c = (b + a)/2; %% Set c to be the midpoint of the interval

fc = f(c); %% Calculate the value of f at c

if sign(fc) ~= sign(fb) %% If f(c) and f(b) are of different sign, then


%% f must have a zero between c and b (by the
n =n+1;
fprintf(' n=%d,a=%d, b=%d, c=%d, fc=%d\n',n,a,b,c, fc) %%
Intermediate Value Theorem).
a = c; fa = fc;

else %% This is the case where f(a) and f(c) are of


%% different sign.
%%
n=n+1;
fprintf(' n=%d,a=%d, b=%d, c=%d, fc=%d\n',n,a,b,c, fc)
b = c; fb = fc;

end
%% Repeat the algorithm on the new interval

end

Run using the following code in a .m file:


f=@(x)(x*cos(x)-2*(x^2)+3*x-1);
a=1.2; b=1.3; delta=1e-7;
y = bisect(f, a, b, delta);
Result
>> missioncontrol
initial interval: a=1.200000e+00, b=1.300000e+00, fa=1.548293e-01, fb=-
1.322515e-01
n=1,a=1.200000e+00, b=1.300000e+00, c=1.250000e+00, fc=1.915295e-02
n=2,a=1.250000e+00, b=1.300000e+00, c=1.275000e+00, fc=-5.458535e-02
n=3,a=1.250000e+00, b=1.275000e+00, c=1.262500e+00, fc=-1.722489e-02
n=4,a=1.250000e+00, b=1.262500e+00, c=1.256250e+00, fc=1.086892e-03
n=5,a=1.256250e+00, b=1.262500e+00, c=1.259375e+00, fc=-8.038288e-03
n=6,a=1.256250e+00, b=1.259375e+00, c=1.257813e+00, fc=-3.468020e-03
n=7,a=1.256250e+00, b=1.257813e+00, c=1.257031e+00, fc=-1.188644e-03
n=8,a=1.256250e+00, b=1.257031e+00, c=1.256641e+00, fc=-5.039580e-05
n=9,a=1.256250e+00, b=1.256641e+00, c=1.256445e+00, fc=5.183683e-04
n=10,a=1.256445e+00, b=1.256641e+00, c=1.256543e+00, fc=2.340162e-04
n=11,a=1.256543e+00, b=1.256641e+00, c=1.256592e+00, fc=9.181772e-05
n=12,a=1.256592e+00, b=1.256641e+00, c=1.256616e+00, fc=2.071284e-05
n=13,a=1.256616e+00, b=1.256641e+00, c=1.256628e+00, fc=-1.484101e-05
n=14,a=1.256616e+00, b=1.256628e+00, c=1.256622e+00, fc=2.936028e-06
n=15,a=1.256622e+00, b=1.256628e+00, c=1.256625e+00, fc=-5.952464e-06
n=16,a=1.256622e+00, b=1.256625e+00, c=1.256624e+00, fc=-1.508211e-06
n=17,a=1.256622e+00, b=1.256624e+00, c=1.256623e+00, fc=7.139104e-07
n=18,a=1.256623e+00, b=1.256624e+00, c=1.256623e+00, fc=-3.971496e-07
n=19,a=1.256623e+00, b=1.256623e+00, c=1.256623e+00, fc=1.583805e-07
Newton Raphson Method
Code
x = 0.05;
x_old = 100;
x_true = 0.0623776;
iter = 0;
while abs(x_old-x) > 10^-19 && x ~= 0
x_old = x;
x = x - (x^3 - x^2 + 1)/(3*x^2 - 2*x);
iter = iter + 1;
fprintf('Iteration %d: x=%.20f, err=%.20f\n', iter, x, x_true-x);

end

Result
>> newtonsimple
Iteration 1: x=10.83513513513513600000, err=-10.77275753513513700000
Iteration 2: x=7.33879377574983940000, err=-7.27641617574983980000
Iteration 3: x=5.00793477984931100000, err=-4.94555717984931140000
Iteration 4: x=3.45146490648721600000, err=-3.38908730648721600000
Iteration 5: x=2.40400695923320210000, err=-2.34162935923320200000
Iteration 6: x=1.67660874589255270000, err=-1.61423114589255270000
Iteration 7: x=1.10533828992829090000, err=-1.04296068992829080000
Iteration 8: x=0.32940872028529722000, err=-0.26703112028529724000
Iteration 9: x=3.11149659193232740000, err=-3.04911899193232740000
Iteration 10: x=2.17192159622983730000, err=-2.10954399622983720000
Iteration 11: x=1.50631040051809780000, err=-1.44393280051809780000
Iteration 12: x=0.93998512138526191000, err=-0.87760752138526188000
Iteration 13: x=-0.28865923806673521000, err=0.35103683806673519000
Iteration 14: x=-1.36763098822032720000, err=1.43000858822032730000
Iteration 15: x=-0.95686610424185692000, err=1.01924370424185700000
Iteration 16: x=-0.78699363803054223000, err=0.84937123803054226000
Iteration 17: x=-0.75587808748570084000, err=0.81825568748570088000
Iteration 18: x=-0.75487867975500456000, err=0.81725627975500459000
Iteration 19: x=-0.75487766624773434000, err=0.81725526624773437000
Iteration 20: x=-0.75487766624669272000, err=0.81725526624669276000
Iteration 21: x=-0.75487766624669272000, err=0.81725526624669276000
>> ....
Scaled Partial Pivoting

Code
% Gaussian elimination with scaled partial pivoting.

clear;
format short;

% Step 0: Assign the matrix A and the vector b.

n = 7;
A = [ 13, 72, 57, 94, 90, 92, 35;
40, 93, 90, 99, 1, 95, 66;
48, 91, 71, 48, 93, 32, 67;
7, 93, 29, 2, 24, 24, 7;
41, 84, 44, 40, 82, 27, 49;
3, 72, 6, 33, 97, 34, 4;
43, 82, 66, 43, 83, 29, 61 ];
b = [ 453; 484; 450; 186; 367; 249; 407 ];

% Step 1: Gaussian Elimination with scaled partial pivoting.


% Overwrite the matrix.

p = (1:n)'; % initialize the pivoting vector


s = max(abs(A')); % compute the scale of each row
for k = 1:(n-1)
r = abs(A(p(k),k)/s(p(k)));
kp = k;
for i = (k+1):n
t = abs(A(p(i),k)/s(p(i)));
if t > r, r = t; kp = i; end
end
l = p(kp); p(kp) = p(k); p(k) = l; % interchange p(kp) and p(k)
for i = (k+1):n
A(p(i),k) = A(p(i),k)/A(p(k),k);
for j = (k+1):n
A(p(i),j) = A(p(i),j)-A(p(i),k)*A(p(k),j);
end
end
end
p % output the pivoting vector
A % output the overwritten matrix

% Step 2: Forward subsitiution to solve L*y = b, where


% L(i,j) = 0 for j>i; L(i,i) = 1; L(i,j) = A(p(i),j) for i>j.

y = zeros(n,1); % initialize y to be a column vector


y(1) = b(p(1));
for i = 2:n
y(i) = b(p(i));
for j = 1:(i-1)
y(i) = y(i)-A(p(i),j)*y(j);
end
end
y % output y

% Step 3: Back subsitiution to solve U*x = y


% U(i,j) = A(p(i),j) for j>=i; U(i,j) = 0 for i>j.

x = zeros(n,1); % initialize x to be a column vector


x(n) = y(n)/A(p(n),n);
for i = (n-1):-1:1
x(i) = y(i);
for j = (i+1):n
x(i) = x(i)-A(p(i),j)*x(j);
end
x(i) = x(i)/A(p(i),i);
end
x % output x
Result
x =

1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000

>> ....
Newtons Divided Difference
Write the newtons divided difference method in Matlab as follows:
function TDD = divdiff(X, Y)
%
% TDD = divdiff(X, Y)
%
% DIVDIFF
% Newton's Method for Divided Differences.
%
% The following formula is solved:
% Pn(x) = f(x0) + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ...
% + f[x0,x1,..,xn](x-x0)(x-x1)..(x-x[n-1])
% where f[x0,x1] = (f(x1-f(x0))/(x1-x0)
% f[x0,x1,..,xn] = (f[x1,..,xn]-f[x0,..,x_[n-1]])/(xn-x0)
%
% NOTE: f^(n+1)(csi)/(n+1)! aprox. = f[x0,x1,..,xn,x_[n+1]]
%
% Input::
% X = [ x0 x1 .. xn ] - object vector
% Y = [ y0 y1 .. yn ] - image vector
%
% Output:
% TDD - table of divided differences
%
% Example:
% TDD = divdiff( [ 1.35 1.37 1.40 1.45 ], [ .1303 .1367 .1461 .1614 ])
%
%
%

if nargin ~= 2
error('divdiff: invalid input parameters');
end

[ p, m ] = size(X); % m points, polynomial order <= m-1


if p ~= 1 || p ~=size(Y, 1) || m ~= size(Y, 2)
error('divdiff: input vectors must have the same dimension');
end

TDD = zeros(m, m);


TDD(:, 1) = Y';
for j = 2 : m
for i = 1 : (m - j + 1)
TDD(i,j) = (TDD(i + 1, j - 1) - TDD(i, j - 1)) / (X(i + j - 1)
- X(i));
end
end

end
Step 2: Call the function using the appropriate data points
TDD = divdiff( [ 1.35 1.40 1.45 1.50 1.55 1.57 1.62 1.67 1.72 ], [ .1303 .
1367 .1461 .1614 .1686 .1692 .1699 .1932 .2122])

Result
>>mc3

>>TDD =

1.0e+05 *

0.0000 0.0000 0.0000 0.0000 -0.0011 0.0101 -0.0442 0.2093


-1.7578
0.0000 0.0000 0.0000 -0.0002 0.0011 -0.0018 0.0228 -0.4411
0
0.0000 0.0000 -0.0000 -0.0000 0.0007 0.0043 -0.1183 0
0
0.0000 0.0000 -0.0000 0.0001 0.0016 -0.0276 0 0
0
0.0000 0.0000 -0.0000 0.0004 -0.0044 0 0 0
0
0.0000 0.0000 0.0000 -0.0004 0 0 0 0
0
0.0000 0.0000 -0.0000 0 0 0 0 0
0
0.0000 0.0000 0 0 0 0 0 0
0
0.0000 0 0 0 0 0 0 0
0

Hermite Polynomial

Hermite polynomial in matlab is given by the Command:


>> hermiteH(11, sym([1/6, 1/3, 1/2, 2/3, 3/4]))
>>ans =
[ -17863446149/177147, -25987196992/177147, -107029, 1443416704/177147,
162018387/2048]

You might also like