0% found this document useful (0 votes)
36 views61 pages

Math 3043 (MathLab)

Uploaded by

abunuwudu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views61 pages

Math 3043 (MathLab)

Uploaded by

abunuwudu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Numerical Analysis

Math 3043

1
1. Introduction
 The name MATLAB stands for MATrix
LABoratory, because it was designed to
make matrix computations easy .
 It is a high-level technical computing
language and interactive environment for
numerical computation.
 It uses to graph functions, solve problems
faster than programming languages, such as
C, C++, and Fortran.
2
2. Starting MATLAB
Start -> Programs -> MATLAB -> MATLAB 7,
or
Double-click the MATLAB shortcut icon on
your Window desktop.

3
Command window
 The writing area that you see when you start
MATLAB is called command window.
 In command window, you see the prompt which
looks like >>.
 You type your commands after the prompt and
press ENTER key.
 The command window is helpful to
 use MATLAB as a calculator
 use MATLAB as a graphing tool
 run a program you have written in editor window
by typing its name (without the .m extension) and
clicking the Enter key.
4
Command History
• Statements you enter in the Command
Window are stored by MATLAB and can be
viewed in the Command History.
• To repeat a statement you have already used,
you can simply double-click on the statement
in the command history , or use the up arrow
key at the command prompt to iterate
through the statements you have used until
you reach the statement you desire to repeat.
5
Workspace
 The workspace browser shows the properties of
currently defined variables. It is empty at startup.
 The name of each variable, its array size, and its size in
bytes are listed.
 A function alternative for this panel is to type whos in
the command window.

6
Editor Window
• Editor window uses:
for Writing MATLAB commands (Programs)
to save the program files(end with .m file
extension)
for editing written programs.

7
Help Browser
• To open the help browser:
click on the Help tab on the top toolbar, OR,
 click the help button ? symbol
(immediately below the Help tab).
• NOTE that the tabs in the Help Window
Navigator (shown below) provide different
ways to search for information: contents,
index, or search.
8
Arithmetic operations

9
Exercises
Errors in Input
 If you make an error in an input line, MATLAB will
normally print an error message. For example,
here’s what happens when you try to evaluate 3uˆ2:
>> 3uˆ2
??? 3uˆ2
|
 Note that MATLAB places a marker (a
vertical line segment) at the place where it
thinks the error might be; however, the
actual error may have occurred earlier or
later in the expression.
11
ans
 MATLAB prints the answer and assigns the
value to a variable called ans.
If you want to perform further calculations
with the answer, you can use the variable ans
rather than retype the answer.

12
Built-in functions

13
Built –in Constants and Special Characters

14
Exercises

15
Assigning variables in MATLAB

16
... Assigning variables in MATLAB

17
Exercises

18
Matrices and vectors

19
20
21
22
Basic matrix and vector operations

23
Basic matrix and vector functions

24
Exercises

25
PROGRAMMING WITH MATLAB
27
28
29
30
31
32
33
34
35
36
Conditional Statements

37
38
39
40
41
42
43
44
45
46
Program 1
% MATLAB Program for Bisection Method

function y= bisection_method(f,a,b,tol,maxit)
f=inline(‘x^2-x-1’,’x’)
fa=feval(f,a);
fb=feval(f,b);
if fa*fb>0
fprintf('There is no root b/n these numbers\n')
else
fprintf(' i a b x f(x) error\n')
i=0;
while i<=maxit
i=i+1;
x=(a+b)/2;
fx=feval(f,x);
err=abs(b-a)/2;
fprintf('%2.0f %8.4f %8.4f %8.4f %12.6f %12.6f\n',i,a,b,x,fx,err)
if err<tol
fprintf('The required root is x=%5.4f\n',x)
break
end
if(fa*fx<0)
b=x;fb=fx;
else
a=x;fa=fx;
end
if(i>maxit)
fprintf('Iterations are not sufficient.\n')
end
end
end
Program 2
%MATLAB Program for false position method

function y = false_positionm( f, a, b, tol, maxi )


f=inline(‘x^2-x-1’,’x’)
x0 = a;
fa = feval(f,a);
fb = feval(f,b);
if fa*fb>0
fprintf('There is no real root b/n %d and %d\n',a,b)
else
fprintf(' i a b x Error \n')
for i = 1 : maxi
x = (a*fb - b*fa)/( fb - fa );
fx = feval(f,x);
err=abs(x-x0);
if nargout == 0
fprintf ('%3d %4.4f %5.4f %0.4f %3.4f', i, a, b, x, err)
fprintf('\n')
end
if err < tol
fprintf(' The real root is : %f\n',x)
if nargout == 1
y = x;
end
return
elseif fa * fx < 0
b = x;
fb = fx;
else
a = x;
fa = fx;
end
x0 = x;
end
fprintf('Maximum number of iterations not sufficient \n')
if nargout== 1
y = x;
end
end
Program 3
% MATLAB Program for Newton-Raphson Method

function y= newton_raphsonmethod(f,df,x0,tol,maxit)
fprintf(' i x f(x) error\n')
i=0;
while i<=maxit
i=i+1;
x=x0-(feval(f,x0)/feval(df,x0));
err=abs(x-x0);
fx=feval(f,x);
fprintf('%3.0f %12.5f %12.5f %12.9f\n',i,x,fx,err)
x0=x;
if err<tol
fprintf('The required root is x =%8.4f\n',x)
break
end
end
if i>maxit
fprintf('Iterations are not sufficient.\n')
end
Instruction:- By using the programs 1, 2 and 3 answer the following questions and identify
the answer and error.

1. Find the real root of the equation between 1 and 2 correct to


four decimal places.
2. Find the root of the equation √ between 0 and 1 correct to three
decimal places.
3. Find the real root of the equation 0 between 1 and 2 correct to five
decimal places.
4. Find the real root of the equation between 1 and 2 correct to five
decimal places.
5. Find the real root of the equation 0 between 1 and 2 correct to five
decimal places.
Program 4

% MATHLAB Program for Gauss Elimination Method

function y=gauss_eliminationmethod(A,b)
fprintf('The augmented matrix is :\n');
agm=[A b]
n=length(A);
for i=1:n-1
for j=i+1:n
m=A(j,i)/A(i,i);
A(j,:)=A(j,:)-m*A(i,:);
b(j)=b(j)-m*b(i);
end
end
fprintf('The row echolen form of the matrix A is:\n');
A=[A]
fprintf('The row echolen form of the agumented matrix agm is:\n');
agm=[A b]
if rank(A)~=rank(agm)
fprintf('The systm has no real root\n')
elseif rank(A)<n
fprintf('The systm has many real roots\n')
else
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
k=b(i);
for j=i+1:n
k=k-A(i,j)*x(j);
x(i)=k/A(i,i);
end
end
fprintf('The solution vector has components:\n');
for i=1:n
fprintf('x(%d) = %d\n',i,x(i));
end
end
Program 5

% MATHLAB Program for Cramer's Method

function y=cramer_method(A,b)
n=length(A);
fprintf('The solution vector has components:\n');
for i=1:n
D=A;
D(:,i)=b;
x(i)=det(D)/det(A);
fprintf('x(%d)=%d\n',i,x(i))
end
Instruction:- By using the programs 4 and 5 solve the following system of linear equations.

1. Solve { 2.Solve {

2. Solve {
Program 6
%MATLAB Program for Jacobi methood.
function y=jocobi_method(A,b,x0,tol,maxit)
n=length(x0);
fprintf(' k');
for i=1:n
fprintf(' x(%d)',i);
end
fprintf(' \n');
for k=1:maxit
maxerr=0;
for i=1:n
s=0;
for j=1:n
if(j~=i)
s=s+(A(i,j)*x0(j));
end
end
x(i)=(b(i)-s)/A(i,i);
err=abs(x0(i)-x(i));
if err>maxerr
maxerr=err;
end
end
fprintf('%3.0f',k);
for i=1:n
x0(i)=x(i);
fprintf(' %12.4f ',x(i));
end
fprintf('\n')
if maxerr<tol
fprintf('The solutions are:\n');
for i=1:n
fprintf(' x(%d) = %0.4f\n',i,x(i));
end
break
end
end
if maxerr>tol
fprintf('The iterations are not sufficient\n')
end
Program 7
%MATLAB Program for Gauss_Seidel Method
function y=gauss_seidelmethod(A,b,x0,tol,maxit)
n=length(x0);
fprintf(' k');
for i=1:n
fprintf(' x(%d)',i);
end
fprintf(' \n');
for k=1:maxit
maxerr=0;
for i=1:n
s=0;
for j=1:n
if(j~=i)
s=s+A(i,j)*x0(j);
end
end
x(i)=(b(i)-s)/A(i,i);
err=abs(x0(i)-x(i));
if err>maxerr
maxerr=err;
end
x0(i)=x(i);
end
fprintf('%3.0f',k);
for i=1:n
fprintf(' %12.4f',x(i));
end
fprintf('\n')
if maxerr<tol
fprintf('The solutions are:\n');
for i=1:n
fprintf(' x(%d) = %0.4f\n',i,x(i));
end
break
end
end
if maxerr>tol
fprintf('The iterations are not sufficient\n')
end
Instruction:- Solve the following system of linear equations by using the programs 6 and 7.

1. { correct to three decimal places.

2. { correct to three decimal places.

3. { correct to three decimal places.


Program 8
%MATLAB Program for Newton Forward Interpolation Formula
function z=NFIF(X,Y,x)
n=length(X);
h=X(2)-X(1);
u=(x-X(1))/h;
for i=1:n
A(1,i)=Y(i);
end
for j=2:n
for i=1:n+1-j
A(j,i)=A(j-1,i+1)-A(j-1,i);
end
end
fprintf('The forward difference table is:\n')
fprintf(' X Y');
for i=1:n-1
fprintf(' FD(%d)',i);
end
fprintf(' \n');
for i=1:n
fprintf('%3.1f',X(i));
for j=1:n+1-i
fprintf(' %6.2f',A(j,i));
end
fprintf('\n')
end
p=1;
y=Y(1)*p;
for k=1:n-1
p=p*(u-(k-1))/k;
y=y+p*A(k+1,1);
end
fprintf('The required approximation is y=%5.2f\n',y)
end
Program 9
%MATLAB Program for Newton Backward Interpolation Formula
function z=NBIF(X,Y,x)
n=length(X);
h=X(2)-X(1);
u=(x-X(n))/h;
for i=1:n
A(1,i)=Y(i);
end
for j=2:n
for i=j:n
A(j,i)=A(j-1,i)-A(j-1,i-1);
end
end
fprintf('The backward difference table is:\n')
fprintf(' X Y');
for i=1:n-1
fprintf(' BD(%d)',i);
end
fprintf(' \n');
for i=1:n
fprintf('%3.1f',X(i));
for j=1:i
fprintf(' %6.3f',A(j,i));
end
fprintf('\n')
end
p=1;
y=Y(n)*p;
for k=1:n-1
p=p*(u+(k-1))/k;
y=y+p*A(k+1,n);
end
fprintf('The required approximation is y=%5.3f\n',y)
end
Program 10
%MATLAB Program for Newton divided difference Method

function z=NDDIF(X,Y,x)
n=length(X);
for i=1:n
DD(1,i)=Y(i);
end
for j=2:n
for i=1:n+1-j
DD(j,i)=(DD(j-1,i+1)-DD(j-1,i))/(X(i+j-1)-X(i));
end
end
fprintf('The divided difference table is:\n')
fprintf(' X Y');
for i=1:n-1
fprintf(' DD(%d)',i);
end
fprintf(' \n');
for i=1:n
fprintf('%2.0f',X(i));
for j=1:n+1-i
fprintf(' %5.2f',DD(j,i));
end
fprintf('\n')
end
p=1;
y=Y(1)*p;
for k=1:n-1
p=p*(x-X(k));
y=y+p*DD(k+1,1);
end
fprintf('The required approximation is y=%5.2f\n',y)
end
Program 11
%MATLAB Program for Lagrange Method

function y=lagrange(X,Y,x)
n=length(X);
y=0;
for i=1:n
p=1;
for j=1:n
if j~=i
p=p*(x-X(j))/(X(i)-X(j));
end
end
y=y+p*Y(i);
end
Instruction I:- Approximate using the programs 8, 9, 10 and 11.

1. Given the data


x 4 6 8 10
y 1 3 8 16
Find approximate value of y at x=9.
2. Given the data
x 0.1 0.2 0.3 0.4 0.5
y 1.4 1.56 1.76 2 2.28
Interpolate at x=0.25 and x=0.35.
3. Given the data
x 10 15 20 25 30 35
y 35.3 32.4 292 26.1 23.2 20.5
Find f(12).

Instruction II:- Approximate using the programs 10 and 11.

4. Given the data


X 0 2 3 4 7 8
y 4 26 58 112 466 668
Find f(1), f(5) and f(9).
5. Given the data
X 1 2 3 4 7
y 2 4 8 16 128
Find f(5) .
Program 12
%MATLAB Program for Trapezoidal rule

function I=trapezoidal(f,a,b,n)
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=feval(f,x(i));
end
I=(h/2)*(y(1)+2*sum(y(2:n))+y(n+1));
fprintf('The value of the integral I=%8.4f\n',I);

Program 13
%MATLAB Program for Simpson’s one third rule

function I=simpson_one3(f,a,b,n)
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=feval(f,x(i));
end
I=(h/3)*(y(1)+4*sum(y(2:2:n))+2*sum(y(3:2:n-1))+y(n+1));
fprintf('The value of the integral I=%3.4f\n',I);

Program 14
%MATLAB Program for Simpson’s three eight rule

function I=Simpsons_three8(f,a,b,n)
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=feval(f,x(i));
end
I=(3*h/8)*(y(1)+3*sum(y(2:3:n-1))+3*sum(y(3:3:n))+ 2*sum(y(4:3:n))+y(n+1));
fprintf('\n')
fprintf('The value of the integral I=%5.3f\n',I);
Program 15
%MATLAB Program For Numerical Integration

function I=Integration(ft,a,b,n)
h=(b-a)/n;
fprintf('The function value is :\n')
fprintf(' i x f(x)\n')
for i=1:n+1
x(i)=a+(i-1)*h;
y(i)=feval(ft,x(i));
fprintf('%2.0f %2.1f %5.4f\n',i,x(i),y(i))
end
if mod(n,6)==0
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
I2=(h/3)*(y(1)+4*sum(y(2:2:n))+2*sum(y(3:2:n))+y(n+1));
I3=(3*h/8)*(y(1)+3*sum(y(2:n))-sum(y(4:3:n))+y(n+1));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('The numerical integration by Simpsos one thrid rule is :\n')
fprintf(' I=%5.5f\n',I2)
fprintf('The numerical integration by Simpson three eigth rule is :\n')
fprintf(' I=%5.5f\n',I3)
elseif mod(n,2)==0
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
I2=(h/3)*(y(1)+4*sum(y(2:2:n))+2*sum(y(3:2:n))+y(n+1));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('The numerical integration by Simpsos one thrid rule is :\n')
fprintf(' I=%5.5f\n',I2)
fprintf('Integration by Simpsons three eigth rule does not work.\n')
elseif mod(n,3)==0
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
I3=(3*h/8)*(y(1)+3*sum(y(2:n))-sum(y(4:3:n))+y(n+1));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('The numerical integration by Simpson three eigth rule is :\n')
fprintf(' I=%5.5f\n',I3)
fprintf('Integration by Simpsons one third rule does not work.\n')
else
I1=h*(sum(y)-0.5*(y(1)+y(n+1)));
fprintf('The numerical integration by Trapezoidal rule is :\n')
fprintf(' I=%5.5f\n',I1)
fprintf('Integration by Simpsons one third and three eigth rules do not work.\n')
end
Instruction III:- Approximate using the programs 12,13 and 14.

1. Evaluate
∫ by using
i. Trapezoidal rule
ii. Simpson’s one third rule
iii. Simpson’s three eight rule
2. Evaluate
∫ by using
i. Simpson’s one third rule taking
ii. Simpson’s three eight rule taking
3. Evaluate
a. ∫ by using 5 intervals
b. ∫ by taking 9 mesh points

c. ∫ by taking 10 nodes
d. ∫ with n=12

You might also like