9 Applications of MATLAB
9 Applications of MATLAB
APPLICATIONS OF MATLAB
Integration And Differentiation
AIM:
Write matlab codes for finding integral and differential of a given function and verify the
result.
OBJECTIVES:-
Upon completion of this experiment the students will be able to
1. To know basic functions for numerical integration and differentiation
2. To understand symbolic mathematics(using variable like x, y, t etc without
assigning values to them)
Program 1
clc
clear all;
t=0:0.01:2*pi;
y=sin (-t);
dy=diff(y);
dt=diff(t);
c=dy./dt;
plot(t/pi,y,t(1:end-1)/pi,c);
Program 2
clc;
clear all;
q2=quad(@sin,0,pi);
disp('integral result using quadratic rule is');
disp(q2);
x=0:.01:pi;
z=trapz(x,sin(x));
disp('integral result using trepizoidal rule is');
disp(z);
PROCEDURE
1. Open MATLAB software
2. Select Desktop Desktop layout Default; to get all relevant sub-windows on a
single window
3. Now type the following on command window
clc; clear all;
syms x;
w = diff(sin(x));
z = diff(x^5);
r = diff(exp(x)-5);
u = diff(x^3,2);
clc; clear all;
syms x
s=int(sin(x))
Page 1 of 5
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Applications of MATLAB
s1=int(x^5)
s2=int(sin(x),-pi,pi)
4. Now create a script file( .m file) by selecting File New Blank M-file
5. Type the program1 and save it as diff_int.m
6. Now the program can be run by either pressing F5 Key or selecting
Run command from Debug menu or by simply pressing the play button () on
the debug toolbar
7. On command window Enter the Matrices and press Enter
8. Observe and note down the Result
9. Repeat Steps 4 to 8 for other programs
SOLUTION OF LINEAR EQUATIONS
AIM:-
To write matlab codes for the solution of linear equations and verify rhe result.
OBJECTIVES:-
Upon completion of this experiment the students will be able to
1. To solve various system of linear equations
2. To know left division operation
x = A\B solves the system of linear equations A*x = B. The matrices A and B must have the
same number of rows. MATLAB® displays a warning message if A is badly scaled or nearly
singular, but performs the calculation regardless.
If A is a scalar, then A\B is equivalent to A.\B.
If A is a square n-by-n matrix and B is a matrix with n rows, then x = A\B is a solution to the
equation A*x = B, if it ex ists.
If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\
B returns a least-squares solution to the system of equations A*x= B.
x = mldivide(A,B) is an alternative way to execute x = A\B, but is rarely used. It enables
operator overloading for classes.
Program
%solution of linear equation
clc;
clear all;
a = input('Input the matrix A \n = ');
b = input('Input the vector b \n = ');
if (rank(a) ~= rank([a b]))
disp('The system has no solution');
else
if(det(a)~=0) OJH
x = a\b;
disp('The solution is');
disp(x);
else
disp('Determinant of A is zero; The system has infinite solutions');
end
Page 2 of 5
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Applications of MATLAB
end
PROCEDURE
1. Open MATLAB software
2. Select Desktop Desktop layout Default; to get all relevant sub-windows on a single
window
3. Now type the following on command window
clc; clear all;
A = [1 3 5; 4 1 6; 2 7 1]
B = [5; 10; 8]
X = A\B
Y = inv(A) * B
4. Now create a script file( .m file) by selecting File New Blank M-file
5. Type the program1 and save it as lin_eqn.m
6. Now the program can be run by either pressing F5 Key or selecting
Run command from Debug menu or by simply pressing the play button () on the debug
toolbar
7. On command window Enter the Matrices and press Enter
8. Observe and note down the Result
EIGEN VALUE AND EIGEN VECTOR OF A SQUARE MATRIX
AIM:-
Wrte matlab codes to find eigen value and eigen vector of a matrix and verify the result
OBJECTIVES:-
Upon completion of this experiment the students will be able to
1. To understand the usage of the function eig to find eigen values and eigen
vecotrs
2. To understand the basic properties of eigen values and vectors
Program
clc
clear all;
disp('this program calculates eigen values and eigen vectors');
a=input('\n enter the square matrix');
[v d]=eig(a);
[m n]=size(a);
for k=1:n
disp(['the eigen values(' num2str(k) ')is']);
disp(d(k,k));
disp(['the vector(' num2str(k) ')is']);
disp(v(:,k));
disp('press enter to continue');
pause()
end
disp('The basic property of eigen value is A*eigen vector = eigen value *&eigen vector\n\n')
for k=1:n
disp(['A*the eigen vector(' num2str(k) ')is']);
Page 3 of 5
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Applications of MATLAB
a*v(:,k)
disp(['eigen value(' num2str(k) ')*eigen vector(' num2str(k) ')is']);
d(k,k)*v(:,k)
disp('press enter to continue');
pause()
end
PROCEDURE
1. Open MATLAB software
2. Select Desktop Desktop layout Default; to get all relevant sub-windows on a single
window
3. Now type the following on command window
clc; clear all;
A = [2 2 -3;2 1 -6; -1 -2 0]
[v d] = eig(A)
4. Now create a script file( .m file) by selecting File New Blank M-file
5. Type the program1 and save it as eig_val.m
6. Now the program can be run by either pressing F5 Key or selecting
Run command from Debug menu or by simply pressing the play button () on the debug
toolbar
7. On command window press Enter repeatedly so that all the eigen values and vectors are
displayed
8. Observe and note down the Result
ROOTS OF A POLYNOMIAL
AIM:-
Write MATLAB codes to find the roots of a polynomial and verify the result.
OBJECTIVES:-
Upon completion of this experiment the students will be able to
1. To find the roots of a single variable polynomial with degree n using
MATLAB
Program
clc
clear all;
p=input ('enter the coefficient of the polynomial\n ');
c=roots(p);
disp('the roots of the polynomial');
disp(c);
PROCEDURE
1. Open MATLAB software
2. Select Desktop Desktop layout Default; to get all relevant sub-windows on a single
window
3. Now type the following on command window
clc; clear all;
P = [ 5 2 1 0 1 8]
roots(P)
Page 4 of 5
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala
Applications of MATLAB
solve(‘5*x^5+2*x^4+x^3+x+8=0’)
4. Now create a script file( .m file) by selecting File New Blank M-file
5. Type the program1 and save it as root_poly.m
6. Now the program can be run by either pressing F5 Key or selecting
Run command from Debug menu or by simply pressing the play button () on the debug
toolbar
7. On command window Enter the Matrices and press Enter
8. Observe and note down the Result
9. Repeat Steps 4 to 8 for other programs
Page 5 of 5
HDL and MATLAB at GPC Thrikaripur by SITTTR Kerala