0% found this document useful (0 votes)
13 views49 pages

MATLAB Programs 1

The document contains MATLAB programs for various mathematical computations, including finding angles between vectors, determining the rank and nullity of matrices, checking vector spans, and analyzing the consistency of linear equations. Each program includes user input prompts and outputs results based on the calculations performed. The programs cover a range of topics in linear algebra, such as linear dependency, basis and dimension of subspaces, and coordinate matrices.
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)
13 views49 pages

MATLAB Programs 1

The document contains MATLAB programs for various mathematical computations, including finding angles between vectors, determining the rank and nullity of matrices, checking vector spans, and analyzing the consistency of linear equations. Each program includes user input prompts and outputs results based on the calculations performed. The programs cover a range of topics in linear algebra, such as linear dependency, basis and dimension of subspaces, and coordinate matrices.
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/ 49

M.Sc.

(Mathematics) Second Semester


MATLAB Programs
PROGRAM-01

Program to find angle between two vectors in n-


dimensional space and check the orthogonality.

%Angle between two vectors


clear all;clc;
n=input("Enter the dimension of vectors A & B:");
A=zeros(1,n);
B=zeros(1,n);
for i=1:n
A(i)=input(sprintf("A[%d]=",i));
end
fprintf("\n");
for i=1:n
B(i)=input(sprintf("B[%d]=",i));
end
dp=A*B';
theta=acos(dp/(norm(A)*norm(B)));
theta=(180/pi)*theta;
fprintf("The angle between two vector is %2.2f\n",theta);
if (dp==0)
fprintf("Vectors are orthogonal\n");
else
fprintf("Vectors are not orthogonal\n");
end

1
Out Put
1) Enter the dimension of vectors A & B:3

A[1]=1

A[2]=0

A[3]=0

B[1]=0

B[2]=1

B[3]=0

The angle between two vectors is 90.00

Vectors are orthogonal

2) Enter the dimension of vectors A & B:3

A[1]=1

A[2]=2

A[3]=3

B[1]=4

B[2]=5

B[3]=6

The angle between two vectors is 12.93

Vectors are not orthogonal

2
PROGRAM-02

Program to find the rank and nullity of a matrix through


its row reduced echelon form.

%Rank and Nullity


clear all;clc;
A=input("Enter the matrix A:");
[m,n]=size(A);
R=rref(A);
disp("Row Reduced Echelon Form of A is:");
disp(R);
rank_A=rank(A);
nullity_A=n-rank_A;
fprintf("Rank of the matrix A is %d\n",rank_A);
fprintf("Nullity of the matrix A is %d\n",nullity_A);

Out Put

1) Enter the matrix A:[1 2 4;2 4 8]

Row Reduced Echelon Form of A is:

1 2 4

0 0 0

Rank of the matrix A is 1

Nullity of the matrix A is 2

2) Enter the matrix A:[1 1 0 -2;2 0 2 2;4 1 3 1]

Row Reduced Echelon Form of A is:

1 0 1 1

0 1 -1 -3

0 0 0 0

Rank of the matrix A is 2

Nullity of the matrix A is 2

3
PROGRAM-03

Program to check whether the given vector is in the span


of a set of vectors.

%Check whether a set of vectors (S) spans the given vector (V)
clear all;clc;
n=input("Enter the dimension of R^n:");
k=input("Enter the number of vectors in the subspace S of
R^n:");
V=zeros(n,1);
S=zeros(n,k);
fprintf("Enter a vector in R^%d\n",n);
for i=1:n
V(i,1)=input("");
end
for j=1:k
fprintf("Enter the elements of vector %d of subspace
S\n",j);
for i=1:n
S(i,j)=input("");
end
end
[m,n]=size(S);
disp('The subspace S is:');
disp(S);
if length(V) ~= m
error('The number of rows of S must match the length of V');
end
coefficients=S\V;
disp('The coefficients are:');
for i=1:length(coefficients)
fprintf('Coefficients %d:%0.2f\n',i,coefficients(i));
end
V_check=S*coefficients;
disp('Verification of the linear combination:');
disp(V_check);
if isequal(V,V_check)
disp('V is the span of S.');
else
disp('V is NOT in the span of S.');
end

4
Out Put
1) Enter the dimension of R^n:2

Enter the number of vectors in the subspace S of R^n:2

Enter a vector in R^2

Enter the elements of vector 1 of subspace S

Enter the elements of vector 2 of subspace S

The subspace S is:

1 0

0 1

The coefficients are:

Coefficients 1:4.00

Coefficients 2:5.00

Verification of the linear combination:

V is the span of S.

2) Enter the dimension of R^n:3

5
Enter the number of vectors in the subspace S of R^n:3

Enter a vector in R^3

Enter the elements of vector 1 of subspace S

Enter the elements of vector 2 of subspace S

Enter the elements of vector 3 of subspace S

The subspace S is:

1 0 1

0 1 1

0 0 1

The coefficients are:

Coefficients 1:-2.00

Coefficients 2:-1.00

6
Coefficients 3:6.00

Verification of the linear combination:

V is the span of S.

3) Enter the dimension of R^n:3

Enter the number of vectors in the subspace S of R^n:2

Enter a vector in R^3

Enter the elements of vector 1 of subspace S

Enter the elements of vector 2 of subspace S

The subspace S is:

1 0

0 1

0 0

7
The coefficients are:

Coefficients 1:1.00

Coefficients 2:2.00

Verification of the linear combination:

V is NOT in the span of S.

8
PROGRAM-04

Program to discuss the nature of consistency for system


of linear equations.

%The nature of consistency for system of LE


clear all;clc;
A=input("Enter the matrix A:");
B=input("Enter the matrix B:");
[m,n]=size(A);
AB=[A B];
rank_A=rank(A);
rank_AB=rank(AB);
fprintf("\n The rank of matrix A is %d\n",rank_A);
fprintf("The rank of augmented matrix is %d\n",rank_AB);
fprintf("The size of the matrix is m=%d and n=%d\n",[m,n]);
if rank_A==rank_AB
if rank_A==n
fprintf("\nThe system is consistent and has unique
solution.\n");
else
fprintf("\nThe system is consistent and has infinite
solution.\n");
end
else
fprintf("\nThe system is inconsitent and has no
solution.\n");
end

9
Out Put
1) Enter the matrix A:[1 -2 3;2 -3 -1;1 1 2]

Enter the matrix B:[5;-1;7]

The rank of matrix A is 3

The rank of augmented matrix is 3

The size of the matrix is m=3 and n=3

The system is consistent and has unique solution.

2) Enter the matrix A:[1 1 1;2 2 2;3 3 3]

Enter the matrix B:[3;6;9]

The rank of matrix A is 1

The rank of augmented matrix is 1

The size of the matrix is m=3 and n=3

The system is consistent and has infinite solution.

3) Enter the matrix A:[1 1 1;1 1 1;1 1 1]

Enter the matrix B:[2;3;4]

The rank of matrix A is 1

The rank of augmented matrix is 2

The size of the matrix is m=3 and n=3

The system is inconsistent and has no solution.

10
PROGRAM-05

Program to verify the linear dependency of set of


vectors.

%Linear dependency and Independency


clear all;clc;
n=input("Enter the dimension of R^n:");
k=input("Enter the dimension of the subspace A of R^n:");
A=zeros(n,k);
for j=1:k
fprintf("Enter the elements of vector %d of subspace S\n",j);
for i=1:n
A(i,j)=input('');
end
end
num_vect=size(A,2);
if rank(A)<num_vect
fprintf("The set of vector is linearly dependent.\n");
else
fprintf("The set of vector is linearly independent.\n");
end

Out Put
1) Enter the dimension of R^n:3

Enter the dimension of the subspace A of R^n:4

Enter the elements of vector 1 of subspace S

Enter the elements of vector 2 of subspace S

11
Enter the elements of vector 3 of subspace S

Enter the elements of vector 4 of subspace S

The set of vector is linearly dependent.

2) Enter the dimension of R^n:3

Enter the dimension of the subspace A of R^n:3

Enter the elements of vector 1 of subspace S

Enter the elements of vector 2 of subspace S

-2

Enter the elements of vector 3 of subspace S

-1

-2

The set of vector is linearly independent.

12
PROGRAM-06

Program to determine whether the system of linear


equations Ax = b, where A=ones(3,2),b=[1;2;3], possesses
an exact solution x.

%Consistency of linear equation


clear all;clc;
A=ones(3,2);
b=[4;2;3];
aug=[A b];
disp("The augmented matrix is:");
disp(aug);
rank_A=rank(A);
rank_aug=rank(aug);
if rank_A~=rank_aug
fprintf("The system doesn't possess exact solution.");
else
if rank_A==size(A,2)
fprintf("The system possess exact solution.\n");
X=A\b;
disp("The solution is");
disp(X);
end
end

Out Put
1) The augmented matrix is:

1 1 1

1 1 2

1 1 3

The system doesn't possess exact solution.

13
2) The augmented matrix is:

1 1 4

1 1 2

1 1 3

The system doesn't possess exact solution.

14
PROGRAM-07

Program to compute and print the basis and dimension of


each four fundamental subspaces associated with a matrix.

%Bases and Dimension of fundamental subspaces


clear all;clc;
A=input('Enter the Matrix A:');
B=A';
[m n]=size(A);

fprintf('Column space(C(A)):\n');
[P, pivots]=rref(A);
PCA=A(:,pivots);
fprintf('Basis for column space:\n');
disp(PCA);
col_dim=rank(A);
fprintf('Dimension of column space is %d\n',col_dim);

fprintf('\n Null space (N(A))\n');


null_basis=null(A,'r');
fprintf('Basis for null space:\n');
disp(null_basis);
null_dim=n-rank(A);
fprintf('Dimension of null space is %d\n',null_dim');

fprintf('\n Row space(C(A^T))\n');


[Q,pivots]=rref(B);
PCB=B(:,pivots);
fprintf('Basis for row space:\n');
disp(PCB);
row_dim=rank(B);
fprintf('Dimension of row space is %d\n',row_dim);

fprintf('\n Left null space (N(A^T))\n');


left_null_basis=null(B,'r');
fprintf('Basis for left null space:\n');
disp(left_null_basis);
left_null_dim=m-rank(A);
fprintf('Dimension of left null space is %d\n',left_null_dim');

15
Out Put
1) Enter the Matrix A:[1 2 3;2 4 6;3 6 9]

Column space(C(A)):

Basis for column space:

Dimension of column space is 1

Null space (N(A))

Basis for null space:

-2 -3

1 0

0 1

Dimension of null space is 2

Row space(C(A^T))

Basis for row space:

Dimension of row space is 1

Left null space (N(A^T))

Basis for left null space:

-2 -3

1 0

0 1

16
Dimension of left null space is 2

2) Enter the Matrix A:[1 1 0 0;0 0 1 1;1 1 1 1;2 2 2 2]

Column space(C(A)):

Basis for column space:

1 0

0 1

1 1

2 2

Dimension of column space is 2

Null space (N(A))

Basis for null space:

-1 0

1 0

0 -1

0 1

Dimension of null space is 2

Row space(C(A^T))

Basis for row space:

1 0

1 0

0 1

0 1

Dimension of row space is 2

Left null space (N(A^T))

Basis for left null space:

17
-1 -2

-1 -2

1 0

0 1

Dimension of left null space is 2

18
PROGRAM-08

Program to find the coordinate matrix of a vector in a


vector space of dimension 4 with respect to some basis.

%Program to find the co-ordinate matrix of a vector in R^4 with


respect to a given basis
clear all;clc;
n=input('Enter the dimension ofR^n:');
k=input('Enter the number of basis B of R^n:');
V=zeros(n,1);
B=zeros(n,k);
fprintf('Enter a vector in R^%d\n',n);
for i=1:n
V(i,1)=input('');
end
for j=1:k
fprintf('Enter the elements of basis %d\n',j);
for i=1:n
B(i,j)=input('');
end
end
if rank(B)==n
fprintf('Co-ordinate matrix of a vector in a vector space
is:\n');
C=B\V;
disp(C);
else
fprintf('The matrix B is not valid basis. Columns are not
linearly independent.');
end

Out Put
1) Enter the dimension ofR^n:4

Enter the number of basis B of R^n:4

Enter a vector in R^4

19
5

Enter the elements of basis 1

Enter the elements of basis 2

Enter the elements of basis 3

Enter the elements of basis 4

Co-ordinate matrix of a vector in a vector space is:

20
5

2) Enter the dimension ofR^n:4

Enter the number of basis B of R^n:4

Enter a vector in R^4

Enter the elements of basis 1

Enter the elements of basis 2

Enter the elements of basis 3

Enter the elements of basis 4

21
1

Co-ordinate matrix of a vector in a vector space is:

3) Enter the dimension ofR^n:4

Enter the number of basis B of R^n:4

Enter a vector in R^4

Enter the elements of basis 1

Enter the elements of basis 2

22
Enter the elements of basis 3

Enter the elements of basis 4

The matrix B is not valid basis. Columns are not linearly


independent.

23
PROGRAM-09

Program to check whether a given vector is orthogonal to


column space of a given matrix.

%Program to check whether a given vector is orthogonal to column


space of a given matrix.
clear all;clc;
A=input('Enter the matrix A is:');
[m n]=size(A);
V=zeros(m,1);
fprintf('Enter a vector in R^%d\n',m);
for i=1:m
V(i,1)=input('');
end
dot_products = V'*A;
if dot_products==0
fprintf('The vector is orthogonal to the column space of the
matrix.');
else
fprintf('The vector is NOT orthogonal to the column space of
the matrix.');
end

Out Put
1) Enter the matrix A is:[1 2;2 4]
Enter a vector in R^2:
2
-1
The vector is orthogonal to the column space of the matrix.

2) Enter the matrix A is:[1 2;3 4]


Enter a vector in R^2:
2
-1
The vector is NOT orthogonal to the column space of the
matrix.

3) Enter the matrix A is:[1 0 1;0 1 1;1 1 0]


Enter a vector in R^3:
1
-1
0
The vector is NOT orthogonal to the column space of the
matrix.

24
PROGRAM-10

Program to compute the transition matrix from one basis


(T) to another basis (S).

%Program to compute the transition matrix from one basis (T) to


another basis (S).
clear all;clc;
n=input('Enter the standard basis in R^n:');
T=zeros(n,1);
S=zeros(n,1);
for j=1:n
fprintf('Enter the elements of basis %d of base T\n',j);
for i=1:n
T(i,j)=input('');
end
end
for j=1:n
fprintf('Enter the elements of basis %d of base S\n',j);
for i=1:n
S(i,j)=input('');
end
end
P_T_to_S = inv(S) * T;
fprintf('Transition matrix from basis T to basis S is:\n');
disp(P_T_to_S);

Out Put
1) Enter the dimension of basis in R^n:3

Enter the elements of basis 1 of base T

Enter the elements of basis 2 of base T

25
0

Enter the elements of basis 3 of base T

Enter the elements of basis 1 of base S

Enter the elements of basis 2 of base S

Enter the elements of basis 3 of base S

Transition matrix from basis T to basis S is:

1 0 1

0 1 1

0 0 1

2) Enter the dimension of basis in R^n:3

Enter the elements of basis 1 of base T

26
0

Enter the elements of basis 2 of base T

Enter the elements of basis 3 of base T

Enter the elements of basis 1 of base S

Enter the elements of basis 2 of base S

Enter the elements of basis 3 of base S

Transition matrix from basis T to basis S is:

1 1 0

1 0 1

0 1 1

27
PROGRAM-11

Program to find root of a given equation using Bisection


method.
%Bisection method
clear all;clc;
f = @(x) x^3-7*x+5;
a=input('Enter the lower bound of the interval a:');
b=input('Enter the upper bound of the interval b:');
if f(a)*f(b)>0
fprintf('Invalid initial approximation\n');
else
n=input('Enter the number of iterations:');
fprintf('\nIteration\t a\t\t b\t\t f(x)\t\t root\n');
itr=0;
prev_x=a;
while itr<n
itr=itr+1;
x=(a+b)/2;
fprintf('%d\t\t\t %0.4f\t %0.4f\t
%0.4f\t\t%0.4f\n',itr,a,b,f(x),x);
if abs(x-prev_x)<0.0001
fprintf('\n After %d of iterations the root
is:%0.4f\n',itr,x);
return;
end
if f(a)*f(x)<0
b=x;
else
a=x;
end
prev_x=x;
end
fprintf('Insufficient iteration\n');
end

28
Out Put
1) Enter the lower bound of the interval a:0

Enter the upper bound of the interval b:1

Enter the number of iterations:15

Iteration a b f(x) root

1 0.0000 1.0000 1.6250 0.5000

2 0.5000 1.0000 0.1719 0.7500

3 0.7500 1.0000 -0.4551 0.8750

4 0.7500 0.8750 -0.1511 0.8125

5 0.7500 0.8125 0.0081 0.7812

6 0.7812 0.8125 -0.0721 0.7969

7 0.7812 0.7969 -0.0322 0.7891

8 0.7812 0.7891 -0.0121 0.7852

9 0.7812 0.7852 -0.0020 0.7832

10 0.7812 0.7832 0.0030 0.7822

11 0.7822 0.7832 0.0005 0.7827

12 0.7827 0.7832 -0.0007 0.7830

13 0.7827 0.7830 -0.0001 0.7828

14 0.7827 0.7828 0.0002 0.7828

After 14 of iterations the root is:0.7828

29
2) Enter the lower bound of the interval a:1

Enter the upper bound of the interval b:2

Enter the number of iterations:15

Iteration a b f(x) root

1 1.0000 2.0000 0.8750 1.5000

2 1.0000 1.5000 -0.2969 1.2500

3 1.2500 1.5000 0.2246 1.3750

4 1.2500 1.3750 -0.0515 1.3125

5 1.3125 1.3750 0.0826 1.3438

6 1.3125 1.3438 0.0146 1.3281

7 1.3125 1.3281 -0.0187 1.3203

8 1.3203 1.3281 -0.0021 1.3242

9 1.3242 1.3281 0.0062 1.3262

10 1.3242 1.3262 0.0020 1.3252

11 1.3242 1.3252 -0.0000 1.3247

12 1.3247 1.3252 0.0010 1.3250

13 1.3247 1.3250 0.0005 1.3248

14 1.3247 1.3248 0.0002 1.3248

After 14 of iterations the root is:1.3248

3) Enter the lower bound of the interval a:1

Enter the upper bound of the interval b:2

Invalid initial approximation

30
PROGRAM-12

Program to find root of a given equation using Regula-


falsi method.

%Regula falsi method


clear all;clc;
f = @(x) x^2-2*x-1;
a=input('Enter the lower bound of the interval a:');
b=input('Enter the upper bound of the interval b:');
if f(a)*f(b)>0
fprintf('Invalid initial approximation\n');
else
n=input('Enter the number of iterations:');
fprintf('\nIteration\t a\t\t b\t\t f(x)\t\t root\n');
itr=0;
prev_x=a;
while itr < n
itr=itr+1;
x=(a*f(b)-b*f(a))/(f(b)-f(a));
fprintf('%d\t\t\t %0.4f\t %0.4f\t %0.4f\t
%0.4f\n',itr,a,b,f(x),x);
if abs(x-prev_x)<0.0001
fprintf('\n After %d of iterations the root
is:%0.4f\n',itr,x);
return;
end
if f(a)*f(x)<0
b=x;
else
a=x;
end
prev_x=x;
end
fprintf('Insufficient iteration\n');
end

31
Out Put
1) Enter the lower bound of the interval a:0

Enter the upper bound of the interval b:1

Enter the number of iterations:10

Iteration a b f(x) root

1 0.0000 1.0000 -0.2546 0.8333

2 0.0000 0.8333 -0.0521 0.7930

3 0.0000 0.7930 -0.0101 0.7848

4 0.0000 0.7848 -0.0019 0.7832

5 0.0000 0.7832 -0.0004 0.7829

6 0.0000 0.7829 -0.0001 0.7828

After 6 of iterations the root is:0.7828

2) Enter the lower bound of the interval a:2

Enter the upper bound of the interval b:3

Enter the number of iterations:10

Iteration a b f(x) root

1 2.0000 3.0000 -0.2222 2.3333

2 2.3333 3.0000 -0.0400 2.4000

3 2.4000 3.0000 -0.0069 2.4118

4 2.4118 3.0000 -0.0012 2.4138

5 2.4138 3.0000 -0.0002 2.4141

6 2.4141 3.0000 -0.0000 2.4142

After 6 of iterations the root is:2.4142

32
3) Enter the lower bound of the interval a:1

Enter the upper bound of the interval b:2

Invalid initial approximation

33
PROGRAM-13

Program to find root of a given equation using Secant


method.

%Secant method
clear all;clc;
f = @(x) x^2-2*x-5;
a=input('Enter the lower bound of the interval a:');
b=input('Enter the upper bound of the interval b:');
if f(a)*f(b)>0
fprintf('Invalid initial approximation\n');
else
n=input('Enter the number of iterations:');
fprintf('\nIteration\t a\t\t b\t\t f(x)\t\t root\n');
itr=0;
prev_x=a;
while itr < n
itr=itr+1;
x=(a*f(b)-b*f(a))/(f(b)-f(a));
fprintf('%d\t\t\t %0.4f\t %0.4f\t
%0.4f\t\t%0.4f\n',itr,a,b,f(x),x);
if abs(x-prev_x)<0.0001
fprintf('\n After %d of iterations the root
is:%0.4f\n',itr,x);
return;
end
a=b;
b=x;
prev_x=x;
end
fprintf('Insufficient iteration\n');
end

34
Out Put
1) Enter the lower bound of the interval a:2

Enter the upper bound of the interval b:4

Enter the number of iterations:10

Iteration a b f(x) root

1 2.0000 4.0000 -0.9375 3.2500

2 4.0000 3.2500 -0.1020 3.4286

3 3.2500 3.4286 0.0044 3.4504

4 3.4286 3.4504 -0.0000 3.4495

5 3.4504 3.4495 -0.0000 3.4495

After 5 of iterations the root is:3.4495

2) Enter the lower bound of the interval a:1

Enter the upper bound of the interval b:2

Enter the number of iterations:10

Iteration a b f(x) root

1 1.0000 2.0000 -0.0344 1.9754

2 2.0000 1.9754 0.0006 1.9688

3 1.9754 1.9688 -0.0000 1.9689

4 1.9688 1.9689 -0.0000 1.9689

After 4 of iterations the root is:1.9689

3) Enter the lower bound of the interval a:1

Enter the upper bound of the interval b:2

Invalid initial approximation

35
PROGRAM-14

Program to find root of a given equation using Newton-


Raphson method.

% Newton Raphson method


clear all; clc;
f = @(x) x^3-7*x+5;
df = @(x) 3*x^2-7;
x0=input('Enter the initial approximation to the root:');
n=input('Enter the number of iterations:');
fprintf('\nIteration\t f(x)\t\t f''(x)\t\t root\n');
itr=0;
root=0;
while itr < n
h=(f(x0)/df(x0));
x=x0-h;
fprintf('%d\t\t\t %0.4f\t\t %0.4f\t
%0.4f\n',itr,f(x0),df(x0),x);
if abs(h)<0.0001
root=x;
break;
else
x0=x;
itr=itr+1;
end
end
if (root==x)
fprintf('\n After %d of iterations the root
is:%0.4f\n',itr,x);
else
fprintf('Insufficient iteration\n');
end

36
Out Put
1) Enter the initial approximation to the root:2

Enter the number of iterations:10

Iteration f(x) f'(x) root

0 -1.0000 5.0000 2.2000

1 0.2480 7.5200 2.1670

2 0.0071 7.0879 2.1660

3 0.0000 7.0748 2.1660

After 3 of iterations the root is:2.1660

2) Enter the initial approximation to the root:1.5

Enter the number of iterations:10

Iteration f(x) f'(x) root

0 -6.4375 12.5000 2.0150

1 4.4704 31.7254 1.8741

2 0.4616 25.3288 1.8559

3 0.0070 24.5682 1.8556

4 0.0000 24.5566 1.8556

After 4 of iterations the root is:1.8556

37
PROGRAM-15

Program to find root of a given equation using Fixed-


Point iterative method.

%Fixed-point iteration
clear all;clc;
f = @(x) x^2-4*x+3;
g = @(x) -3/(x-4);
x0=input('Enter the initial approximation root :');
n=input('Enter the number of iterations:');
fprintf('Iteration\t x\t\t\t g(x)\t\t f(x)\n');
for i = 1:n
x1=g(x0);
fprintf('%d\t\t\t %0.4f\t\t %0.4f\t\t
%0.4f\n',i,x1,g(x1),f(x1));
if abs (x1-x0) < 0.0001
fprintf('\n After %d of iterations the root
is:%0.4f\n',i,x1);
break;
end
x0 = x1;
end
if i == n
fprintf('Insufficient iteration\n');
end

Out Put

1) Enter the initial approximation root :0

Enter the number of iterations:15

Iteration x g(x) f(x)

1 0.7500 0.9231 0.5625

2 0.9231 0.9750 0.1598

3 0.9750 0.9917 0.0506

4 0.9917 0.9973 0.0166

5 0.9973 0.9991 0.0055

38
6 0.9991 0.9997 0.0018

7 0.9997 0.9999 0.0006

8 0.9999 1.0000 0.0002

9 1.0000 1.0000 0.0001

After 9 of iterations the root is:1.0000

2) Enter the initial approximation root :1

Enter the number of iterations:15

Iteration x g(x) f(x)

1 0.5000 0.6667 -0.2500

2 0.6667 0.6000 0.1111

3 0.6000 0.6250 -0.0400

4 0.6250 0.6154 0.0156

5 0.6154 0.6190 -0.0059

6 0.6190 0.6176 0.0023

7 0.6176 0.6182 -0.0009

8 0.6182 0.6180 0.0003

9 0.6180 0.6181 -0.0001

10 0.6181 0.6180 0.0000

After 10 of iterations the root is:0.6181

39
PROGRAM-16

Program to find the smallest positive root of the


equation using Birge-Vieta method.

% Birge-Vieta method to find the smallest positive root


clear all;clc;
f = @(x) x^4+x^3+5*x^2+4*x+4;
coeffs = input('Enter the coefficients of the polynomial:');
x = input('Enter the initial approximation root:');
n = input('Enter the number of iterations:');
for i = 1:n
L = length(coeffs);
b = coeffs(1);
c = b;
for j = 2:L-1 %Synthetic Division
b = b * x + coeffs(j);
c = c * x + b;
end
b = b * x + coeffs(L);
if abs(b) < 0.0001
break;
end
x = x - b / c;
end
fprintf('Smallest positive root: %.4f\n', x);

Out Put

1) Enter the coefficients of the polynomial:[1 -3 3 -3 3]


Enter the initial approximation root:1.5
Enter the number of iterations:2
Smallest positive root: 1.5767

2) Enter the coefficients of the polynomial:[1 1 5 4 4]


Enter the initial approximation root:1
Enter the number of iterations:5
Smallest positive root: 2.0886

40
PROGRAM-17

Program to find the roots of a polynomial using Bairstow


method.

%Bairstow Method
clear all;clc;
f = @(x) x^3 + x^2 - x + 2;
coeffs = input('Enter the coefficients of the polynomial:');
p = input('Enter initial approximation root p:');
q = input('Enter initial approximation root q:');
n = input('Enter the number of iterations:');
L = length(coeffs);
fprintf('Iteration\t\t p\t\t q\t\t dp\t\t dq\n');
for itr = 1:n
b = zeros(1, L);
c = zeros(1, L);
b(1) = coeffs(1);
b(2) = coeffs(2) + (-p) * b(1);
for i = 3:L
b(i) = coeffs(i) + (-p) * b(i-1) + (-q) * b(i-2);
end
c(1) = b(1);
c(2) = b(2) + (-p) * c(1);
for i = 3:L
c(i) = b(i) + (-p) * c(i-1) + (-q) * c(i-2);
end
A = [c(L-2), c(L-3); c(L-1)-b(L-1), c(L-2)];
B = [b(L-1); b(L)];
delta = A \ B;
dp = delta(1);
dq = delta(2);
p = p + dp;
q = q + dq;
fprintf('%d\t\t\t %0.4f\t %0.4f\t %0.4f\t %0.4f\n', itr,
p, q, dp, dq);
if max(abs([dp dq])) < 0.0001
break;
end
end
fprintf('\nExtracted quadratic factor: x^2 + (%.1f)x +
(%.1f)\n', p, q);
fprintf('The Roots are:-\n'); % Roots of the quadratic
d = sqrt(p^2 - 4*q);
if isreal(d)

41
r1 = (-p + d)/2;
r2 = (-p - d)/2;
fprintf('Real roots: %.4f and %.4f\n', r1, r2);
else
fprintf('Complex roots,\n %.4f + %.4fi and %.4f -%.4fi\n', -
p/2, abs(imag(d))/2, -p/2, abs(imag(d))/2);
end

Out Put
1) Enter the coefficients of the polynomial:[1 1 -1 2]

Enter initial approximation root p:-0.9

Enter initial approximation root q:0.9

Enter the number of iterations:2

Iteration p q dp dq

1 -1.0047 1.0031 -0.1047 0.1031

2 -1.0000 1.0000 0.0047 -0.0031

Extracted quadratic factor: x^2 + (-1.0)x + (1.0)

The Roots are:-

Complex roots,

0.5000 + 0.8660i and 0.5000 -0.8660i

2) Enter the coefficients of the polynomial:[1 -3 20 40 54]

Enter initial approximation root p:2

Enter initial approximation root q:2

Enter the number of iterations:2

Iteration p q dp dq

1 1.8506 2.0035 -0.1494 0.0035

2 1.8455 2.0046 -0.0051 0.0011

Extracted quadratic factor: x^2 + (1.8)x + (2.0)

42
The Roots are:-

Complex roots,

-0.9228 + 1.0739i and -0.9228 -1.0739i

43
PROGRAM-18

Program to solve the system of linear equations by Gauss-


elimination method.

%Gauss elimination
clear all;clc;
A = input('Enter the elements of the augumented matrix:');
disp('The augumented matrix is:');
disp(A);
n = size(A,1);
for i = 1:n-1
for j = i+1:n
factor = A(j,i) / A(i,i);
A(j,i:end) = A(j,i:end) -factor * A(i,i:end);
end
end
x = zeros(n,1);
x(n) = A(n,end) / A(n,n);
for i = n-1:-1:1
sum = 0;
for j = i+1:n
sum = sum + A(i,j) * x(j);
end
x(i) = (A(i,end) - sum) /A(i,i);
end
disp('The solution is:');
for i=1:n
fprintf('x[%d] =%0.4f\n',i,x(i));
end

44
Out Put
1) Enter the elements of the augumented matrix:[2 1 4 12;4 11 -1
33;8 -3 2 20]

The augumented matrix is:

2 1 4 12

4 11 -1 33

8 -3 2 20

The solution is:

x[1] =3.0000

x[2] =2.0000

x[3] =1.0000

2) Enter the elements of the augumented matrix:[2 1 1 10;3 2 3


18;1 4 9 16]

The augumented matrix is:

2 1 1 10

3 2 3 18

1 4 9 16

The solution is:

x[1] =7.0000

x[2] =-9.0000

x[3] =5.0000

45
PROGRAM-19

Program to solve the system of linear equations by Gauss-


Jordan method.

%Gauss Jordan
clear all;clc;
A = input('Enter the elements of the augumented matrix:');
disp('The augumented matrix is:');
disp(A);
n = size(A,1);
for i = 1:n
A(i,:) = A(i,:)/A(i,i);
for j = 1:n
if i~=j
A(j,:) = A(j,:) - A(j,i) * A(i,:);
end
end
end
disp('The solution is:');
for i = 1:n
fprintf('x[%d] =%0.4f\n',i,A(i,end));
end

46
Out Put
1) Enter the elements of the augumented matrix:[1 1 1 9;2 -3 4
13;3 4 5 40]
The augumented matrix is:
1 1 1 9
2 -3 4 13
3 4 5 40

The solution is:


x[1] =1.0000
x[2] =3.0000
x[3] =5.0000

2) Enter the elements of the augumented matrix:[1 3 1 10;1 -2 -1


-4;2 1 2 10]
The augumented matrix is:
1 3 1 10
1 -2 -1 -4
2 1 2 10

The solution is:


x[1] =2.0000
x[2] =2.0000
x[3] =2.0000

47
PROGRAM-20

Program to solve the system of linear equations by using


LU decomposition method.

%LU Decomposition
clear all;clc;
A = input('Enter the elements of matrix A: ');
B = input('Enter the elements of column matrix B: ');
n = size(A, 1);
L = zeros(n);
U = eye(n);
for j = 1:n
for i = j:n
L(i,j) = A(i,j) - L(i,1:j-1)*U(1:j-1,j);
end
for i = j+1:n
U(j,i) = (A(j,i) - L(j,1:j-1)*U(1:j-1,i)) / L(j,j);
end
end
Y = zeros(n,1);
for i = 1:n
Y(i) = (B(i) - L(i,1:i-1)*Y(1:i-1)) / L(i,i);
end
X = zeros(n,1);
for i = n:-1:1
X(i) = (Y(i) - U(i,i+1:n)*X(i+1:n)) / U(i,i);
end
disp('Lower triangular matrix L:');
disp(L);
disp('Upper triangular matrix U:');
disp(U);
disp('Solution vector X:');
disp(X);

48
Out Put
1) Enter the elements of matrix A: [3 4;7 -1]

Enter the elements of column matrix B: [7;6]

Lower triangular matrix L:

3.0000 0

7.0000 -10.3333

Upper triangular matrix U:

1.0000 1.3333

0 1.0000

Solution vector X:

1.0000

1.0000

2) Enter the elements of matrix A: [2 3;1 -2]

Enter the elements of column matrix B: [7;-3]

Lower triangular matrix L:

2.0000 0

1.0000 -3.5000

Upper triangular matrix U:

1.0000 1.5000

0 1.0000

Solution vector X:

0.7143

1.8571

49

You might also like