Matlab Odd Workbook- 2022-2023 (1)
Matlab Odd Workbook- 2022-2023 (1)
1. Introduction to MATLAB
6. Curve tracing
S. Progra Execu
Viva Total Staff
No Date Name of the experiment m tion
(10) (30) sign
. (10) (10)
1 Introduction to MATLAB
Characteristic equation of a
4
matrix and Cayley-Hamilton
theorem.
6 Curve tracing
INTRODUCTION TO MATLAB
1.1 OBJECTIVES
1.2 Introduction
MATLAB is a high-level language and interactive environment for numerical
computation, visualization, and programming. Using MATLAB, you can analyze
data, develop algorithms, and create models and applications. The language,
tools, and built-in math functions enable you to explore multiple approaches and
reach a solution faster than with spread sheets or traditional programming
languages, such as C/C++ or Java. You can use MATLAB for a range of
applications, including signal processing and communications, image and video
processing, control systems, test and measurement, computational finance, and
computational biology. More than a million engineers and scientists in industry
and academia use MATLAB, the language of technical computing.
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
MATLAB will execute the above statement and return the following result −
a =
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(2,5)
MATLAB will execute the above statement and return the following result −
ans = 6
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)
MATLAB will execute the above statement and return the following result −
v =
4
5
6
7
You can also select the elements in the mth through nth columns, for this we write −
a(:,m:n)
Let us create a smaller matrix taking the elements from the second and third columns −
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)
MATLAB will execute the above statement and return the following result −
ans =
2 3
3 4
4 5
5 6
In the same way, you can create a sub-matrix taking a sub-part of a matrix.
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)
MATLAB will execute the above statement and return the following result −
ans =
2 3
3 4
4 5
5 6
In the same way, you can create a sub-matrix taking a sub-part of a matrix.
For example, let us create a sub-matrix sa taking the inner subpart of a −
3 4 5
4 5 6
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
sa = a(2:3,2:4)
MATLAB will execute the above statement and return the following result −
sa =
3 4 5
4 5 6
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a( 4 , : ) = []
MATLAB will execute the above statement and return the following result −
a =
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(: , 5)=[]
MATLAB will execute the above statement and return the following result −
a =
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
Example
In this example, let us create a 3-by-3 matrix m, then we will copy the second and third
rows of this matrix twice to create a 4-by-3 matrix.
Create a script file with the following code −
a = [ 1 2 3 ; 4 5 6; 7 8 9];
new_mat = a([2,3,2,3],:)
>> z1/z2
>> z1-z3
>> z2+z3-z1
>>1+2/3*4-5
>>1/2/3/4
>>1/2+3/4*5
>>5-2*3*(2+7)
>>(1+3)*(2-3)/3*4
>>(2-3*(4-3))*4/5
(3) elog4
(5) log e2
5. Create two vectors running from one to six and from six to one and then
demonstrate the use of the dot arithmetical operations: s+t, s-t, st, s/t, s2, 1/s,
s/2, s+1
s=1:6
t=6:-1:1
(i) s+t
(ii) s-t
(iii) s*t
(iv) s./t
(v) s.^2
(vi) 1./s
(vii) s./2
(ix) s+1
2.1 OBJECTIVES:
1 2 4 0 3 6
Example 1: Find the addition of the matrices A 0 5 6 and B 7 1 1
7 8 4 5 7 9
A=[1 2 4 ; 0 5 6; 7 8 4];
B=[0 3 6; 7 1 -1; 5 7 9];
disp('The matrix A= ');A
disp('The matrix B= ');B
% to find sum of a and b, c=a+b;
disp('The sum of A and B is ');
C=A+B
Output:
The matrix A=
A=
1 2 4
0 5 6
7 8 4
The matrix B=
B=
0 3 6
7 1 -1
5 7 9
The sum of A and B is
C=
1 5 10
7 6 5
12 15 13
MATRIX MULTIPLICATION:
1 2 4 0 3 6
Example 2: Find the multiplication of the matrices A 0 5 6 and B 7 1 1
7 8 4 5 7 9
A=[1 2 4 ; 0 5 6; 7 8 4];
B=[0 3 6; 7 1 -1; 5 7 9];
disp('The matrix A= ');A
disp('The matrix B= ');B
disp('The multiplication of A and B is ');
C=A*B
Output:
The matrix A=
A=
1 2 4
0 5 6
7 8 4
The matrix B=
B=
0 3 6
7 1 -1
5 7 9
C=
34 33 40
65 47 49
76 57 70
1 2 4
Example 3: Find the transpose and inverse of the matrix A 0 5 6
7 8 4
A=[1 2 4 ; 0 5 6; 7 8 4];
disp('The matrix A= ');A
% to find transpose of A;
disp('The transpose of A is ');A'
% to find inverse of A;
disp('The inverse of A is ');
inv(A)
Output:
The matrix A=
A=
1 2 4
0 5 6
7 8 4
The transpose of A is
ans =
1 0 7
2 5 8
4 6 4
The inverse of A is
ans =
5 2 12
1. Find the addition and subtraction of the matrices A 0 19 16 and
17 8 6
8 13 8
B 15 10 1
5 9 9
11 2 14 10 8 6
2.Find the multiplication of the matrices A 10 5 60 and B 7 1 11
17 8 4 9 7 9
1 22 4
3. Find the transpose and inverse of the matrix A 18 5 16
7 28 4
6 2 4 12 21 14
4. Find the matrix multiplication of the two matrices A 8 5 6 B 11 15 36
7 8 4 17 68 24
, .
91 24 42
5. Find the transpose and inverse of the matrix A 18 52 48
72 45 64
WORKSHEET-3
RANK OF A MATRIX AND SOLUTION OF A SYSTEM OF LINEAR EQUATIONS
3.1 OBJECTIVES:
3.2 PROGRAM
Output:
The matrix A=
A=
1 2 3 2
2 3 5 1
1 3 4 5
The rank of A is
C=
Output:
The matrix A=
A=
1 2 3 2
2 3 5 1
1 3 4 5
The rank of A is
C=
syms x y z
eqn1 = 2*x + y + z == 2;
eqn2 = -x + y - z == 3;
eqn3 = x + 2*y + 3*z == -10;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z])
X = linsolve(A,B)
Output:
A=
[ 2, 1, 1]
[ -1, 1, -1]
[ 1, 2, 3]
B=
2
3
-10
X=
3
1
-5
Task 3
1 1 1 3
1. Find the rank of the matrices A 1 1 1 1
3 3 5 1
2 1 1 1
2. Find the echelon form of the matrix A 1 2 1 1
1 1 2 1
4 5 1 2
5. Find the rank of the matrix A 3 1 2 9
1 4 1 5
4.1 OBJECTIVES
To verify Cayley Hamilton Theorem and find the inverse of the matrix
1 3
4.2 Example 1:Verify Cayley Hamilton Theorem for the matrix A
2 4
Program:
A=[1 3;2 4]
disp('The matrix A= ');A
% to find the polynomial of A ;
X=poly(A)
disp('The polynomial of A is ');
round(X)
y=polyvalm(X,A)
disp(' Cayley Hamilton theorem ');
round(y)
disp('The identity matrix is ');
i=eye(2,2)
disp('The inverse of A is ');
InvA=(A-5*i)/2
s=inv(A)
Output:
The matrix A=
A=
1 3
2 4
X = 1.0000 -5.0000 -2.0000
The polynomial of A is
ans = 1 -5 -2
Cayley Hamilton theorem
ans =
0 0
0 0
The identity matrix is
i=
1 0
0 1
The inverse of A is
InvA =
-2.0000 1.5000
1.0000 -0.5000
s=
-2.0000 1.5000
1.0000 -0.5000
1 1 4
Example 2:Verify Cayley Hamilton Theorem for the matrix A 3 2 1
2 1 1
Program:
A=[1 -1 4;3 2 1;2 1 -1]
disp('The matrix A= ');A
% to find the polynomial of A ;
X=poly(A)
disp('The polynomial of A is ');
round(X)
y=polyvalm(X,A)
disp(' Cayley Hamilton theorem ');
round(y)
disp('The identity matrix is ');
i=eye(3,3)
disp('The inverse of A is ');
InvA=(-A^2+2*A+7*i)/12
s=inv(A)
Output:
The matrix A=
A=
1 -1 4
3 2 1
2 1 -1
X = 1.0000 -2.0000 -7.0000 12.0000
The polynomial of A is
ans = 1 -2 -7 12
Cayley Hamilton theorem
ans =
0 0 0
0 0 0
0 0 0
The identity matrix is
i=
1 0 0
0 1 0
0 0 1
The inverse of A is
InvA = 0.2500 -0.2500 0.7500
-0.4167 0.7500 -0.9167
0.0833 0.2500 -0.4167
s = 0.2500 -0.2500 0.7500
-0.4167 0.7500 -0.9167
0.0833 0.2500 -0.4167
Task 4:
1. Verify Cayley Hamilton theorem for the matrix and find the value of A1
1 0 3
A 2 1 1
1 1 1
2. Verify Cayley Hamilton theorem for the matrix and find A1 .
1 3 7
A 4 2 3
1 2 1
3. Verify Cayley Hamilton theorem for the matrix and find A1 .
1 2 3
A 2 1 4
3 1 1
4 . Verify Cayley Hamilton theorem for the matrix and find A1 .
2 1 1
A 0 1 0
1 1 2
5. Verify Cayley Hamilton theorem for the matrix and find A1 .
1 0 1
A 3 4 5
0 6 7
WORKSHEET-5
EIGENVALUES AND EIGENVECTORS OF HIGHER ORDER MATRICES
5.1 OBJECTIVES
Output:
A=
1 -1 0
-1 2 1
0 1 1
The eigenvalues of A are
ans =
0.0000
1.0000
3.0000
The Eigenvector of A are
v=
0.5774 0.7071 -0.4082
0.5774 -0.0000 0.8165
-0.5774 0.7071 0.4082
d=
0.0000 0 0
0 1.0000 0
0 0 3.0000
ans =
1 1 0 0 0 0
1 0 1 0 1 0
-1 1 0 0 0 3
Program:
B=[1 -3 2 -1 ; -3 9 -6 3 ; 2 -6 4 -2 ; -1 3 -2 1]
disp(' The Eigenvalues of B are ');
eig(B)
disp(' The Eigenvector of B are ');
[v,d]=eig(B)
round([v,d])
Output:
B=
1 -3 2 -1
-3 9 -6 3
2 -6 4 -2
-1 3 -2 1
The Eigenvalues of B are
ans =
-0.0000
-0.0000
0.0000
15.0000
The Eigenvector of B are
v=
0.0481 0.9649 0.0069 -0.2582
0.1516 0.1956 0.5820 0.7746
-0.2712 -0.1304 0.8017 -0.5164
-0.9493 0.1173 -0.1357 0.2582
d=
-0.0000 0 0 0
0 -0.0000 0 0
0 0 0.0000 0
0 0 0 15.0000
ans =
0 1 0 0 0 0 0 0
0 0 1 1 0 0 0 0
0 0 1 -1 0 0 0 0
-1 0 0 0 0 0 0 15
Task: 5
11 4 7
1.Find the eigenvalues and eigenvectors of A= 7 2 5
10 4 6
2 0 1
1
2.Find the eigenvalues of A , given that A 0 2 0
1 0 2
6 2 2
3. Compute the eigenvalues and eigenvectors of the matrix A 2 3 1
2 1 3
4 20 10
4. Find the eigenvalues and eigenvectors of A 2 10 4
6 30 13
2 2 3
5. Find the eigenvalues and eigenvectors of A 2 1 6
1 2 0
WORKSHEET-6
CURVE TRACING
6.1 OBJECTIVES
To plot a two dimensional plot.
To plot a three dimensional plot.
6.2 Example 1: Plot the simple function y = x for the range of values for x from 0 to
100, with an increment of 5.
x = [0:5:100];
y = x;
plot(x, y)
100
90
80
70
60
50
40
30
20
10
0
0 10 20 30 40 50 60 70 80 90 100
x = [-100:20:100];
y = x.^2;
plot(x, y)
10000
9000
8000
7000
6000
5000
4000
3000
2000
1000
0
-100 -80 -60 -40 -20 0 20 40 60 80 100
x = [0:0.01:10];
y = sin(x);
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x)
Graph'), grid on, axis equal
Sin(x) Graph
1
Sin(x)
-1
-2
-3
0 1 2 3 4 5 6 7 8 9 10
x
Example 4: Plot multiple functions on the same graph for the curve y=sin x and
g = cos x
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g, '.-'), legend('sin(x)', 'cos(x)')
1
Sin(x)
0.8 Cos(x)
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7 8 9 10
Example 5. Draw the graph of two polynomials f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and
g(x) = 5x3 + 9x + 2.
2.5
1.5
0.5
-0.5
-1
-10 -8 -6 -4 -2 0 2 4 6 8 10
x2 y2
Example 6. Plot the curve 1
4 9
ezplot('x^2/4+y^2/9=1');
axis equal;
3-D Plot
[x,y] = meshgrid(-2:.2:2);
g = x .* exp(-x.^2 - y.^2);
surf(x, y, g)
0.5
-0.5
2
1 2
0 1
0
-1
-1
-2 -2
Task: 6
1. Plot the sine-wave curve y=sin x such that x varies from 0 to 2 with h=/100.
7.1 OBJECTIVES:
To do differentiation and Integration
To differentiate:diff(x)
The ‘diff’ operator will return the difference between the n and n+1 row or
column of data if applied to an array of numbers. When applied to symbolic
variables, ‘diff’ will return the approximate derivative. The derivative, as defined
geometrically, is the tangent of a curve at a given point. When applied to a
function, the derivative returns a new function that is the tangent at every point
in the domain. Mathematically, this derivative is the infinitesimally small change
in the dependent variable from point a to a+h, over the change in h, where h
approaches 0 (similar to rise/run or slope for linear equations). The derivative of
our function f(x) is equal to cos(x).
>> f=sin(x)
>> d=diff(f)
d=cos(x)
>> subs(d,pi)
ans=-1
To Integrate:int(x)
The opposite of a derivative is the integral, or the anti-derivative, which provides the
area under a curve. The first fundamental theorem of calculus guarantees that a
continuous function will have an antiderivative, ‘int’ which allows us to both determine
the explicit and numerical solutions to an equation. We can further use ‘int’ to calculate
both the indefinite (int(f)) and definite integrals over the range of a to b (int(f,a,b)).
Let’s evaluate the integral of f(x), both based on the indefinite integral and over the
range of 0 to pi.
>> f=cos(x)
>>ans=int(f)
ans=sin(x)
>> int(ans,0,pi/2)
ans=1
Example 3:
Just like the ‘diff’ function, we can utilize the ‘int’ function to evaluate an equation with
respect to x (dx), y (dy) or z (dz). This is specified with the 2nd argument (i.e. ‘int(g,x)’).
If a definite integral is to be performed, this can be done using 4 arguments, with limits
a to b specified (i.e. ‘int(g,y,a,b)’).
>> int(g,y)
ans =(x*y^3)/3 - y*sin(z)
>> int(g,y,0,2)
ans =(8*x)/3 - 2*sin(z)
Example 4:
We can even evaluate multiple integrals for functions with more than one variable.
While a single integral can be thought of as the area under the curve, a double integral
can be visualized as the volume underneath a surface.
1 2
Let’s take the following double integral for example: xy dxdy
2
0 0
>>h = x*y^2;
h =x*y^2;
>> h_intx = int(h,x,0,2)
h_intx =2*y^2
>>h_doubleint int(h_intx,y,0,1)
h_doubleint= 2/3
TASK: 7
0 0 3
2 8
5) Find the value of a c da dc
2 5
0 5
2 3 5
6) Compute the value of u v
3 2
w5 dudvdw
0 0 3
WORKSHEET- 8
SOLVING FIRST AND SECOND ORDER ORDINARY DIFFERENTIAL
EQUATIONS
8.1 OBJECTIVES
To solve the first order ordinary differential equations
To solve the second order ordinary differential equations
dy
8.2 Example 1: Solve 5y
dx
df
Example 2. Solve 2 f cos t
dt
%Solving First order ODE
f= dsolve('Df = -2*f + cos(t)','t')
Output:
f=
(2*cos(t))/5 + sin(t)/5 + C4*exp(-2*t)
dx x 1
y
Example 3. Solve tan
dy 1 y 2 1 y2
Output:
y =exp(t)/2 - (3*exp(-t))/2
Example 5: Solve ( D 2 5D 6) y e x
y=dsolve('D2y+5*Dy+6*y=exp(x)','x')
Output:
y=
exp(x)/12 + C25*exp(-2*x) + C26*exp(-3*x)
y=dsolve('D2y+5*Dy+6*y=4*exp(-x)*log(x)','x')
Output:
y=
C28*exp(-2*x) - exp(-2*x)*(4*ei(x) - 4*exp(x)*log(x)) + C29*exp(-3*x) +
exp(-3*x)*(2*ei(2*x) - 2*exp(2*x)*log(x))
y=dsolve('D3y-D2y+Dy-
y=exp(x)+cos(x)','y(0)=0','Dy(0)=0','D2y(0)=0','x')
TASK :8
dy
1. Solve 4 y (t ) e t , y (0) 1.
dt
dy
y cot x 2 x sin x
2. Solve dx
2
3. Solve the differential equation D D 1 y 0
3 2
4. Solve the differential equation D 3D 3D 1 y 0
4 2
5. Solve the differential equation D 2D 1 y 0
4 3 2 2 x
6. Solve the differential equation ( D 2D D ) y x e
2 x
7. Solve the differential equation ( D 1) y e cos x
2
8. Solve the differential equation ( D 4) y cos3x
WORKSHEET- 9
DETERMINING MAXIMA AND MINIMA OF A FUNCTION OF ONE
VARIABLE
9.1 OBJECTIVE
To determine the maxima and minima of a function of a single variable
9.2 Example 1: Find the extreme value of the function f(x)= x3 – 12x2 – 20.
Program:
syms x
f=x^3-12*x^2-20
fx=diff(f,x)
a=solve(fx)
double(a)
fxx=diff(fx,x)
D=subs(fxx,x,8)
if D >0
disp('Attains Minima')
else
disp('Attains Maxima')
end
D=subs(fxx,x,0)
if D >0
disp('Attains Minima')
else
disp('Attains Maxima')
end
OUTPUT
f =x^3 - 12*x^2 - 20
fx = 3*x^2 - 24*x
a=0 8
fxx = 6*x - 24
D = 24 D=-24
Attains Minima Attains Maxima
Task:9
1. Find the maximum value of the function f ( x) x 4 5x 3 3x 18
10.1 OBJECTIVES
To determine maxima and minima of a function of two variables.
10.2 Example 1: Find the extreme value of the function
f ( x, y) x 4 2 y 4 12 xy 2 20 y 2 .
PROGRAM:
syms x y
f=x.^4+2*y.^4-12*x*y.^2-20*y.^2
fx=diff(f,x)
fy=diff(f,y)
[a,b]=solve(fx,fy);
double([a,b])
fxx=diff(fx,x)
fxy=diff(fx,y)
fyy=diff(fy,y)
D=fxx*fyy-(fxy)^2
D1=subs(D,[x,y],[3.6247,3.9842])
A1=subs(fxx,[x,y],[3.6247,3.9842])
D2=subs(D,[x,y],[3.6247,-3.9842])
A2=subs(fxx,[x,y],[3.6247,-3.9842])
if D1>0
if A1>0
disp('Attains Minima')
else
disp('Attains Maxima')
end
else
if D1==0
disp('No conclusion')
else
disp('Neither Minima nor Maxima')
end
end
if D2>0
if A2>0
disp('Attains Minima')
else
disp('Attains Maxima')
end
else
if D2==0
disp('no conclusion')
else
disp('Neither Minima nor maxima')
end
end
ezsurf(f,[0,5,0,5])
Output:
f =x^4 - 12*x*y^2 + 2*y^4 - 20*y^2
fx = 4*x^3 - 12*y^2
fy = 8*y^3 - 24*x*y - 40*y
ans =
0.0000 + 0.0000i 0.0000 + 0.0000i
3.6247 + 0.0000i 3.9842 + 0.0000i
3.6247 + 0.0000i -3.9842 + 0.0000i
-1.8123 - 0.9240i 1.0884 - 1.2734i
-1.8123 + 0.9240i 1.0884 + 1.2734i
-1.8123 - 0.9240i -1.0884 + 1.2734i
-1.8123 + 0.9240i -1.0884 - 1.2734i
fxx =12*x^2
fxy = -24*y
fyy = 24*y^2 - 24*x - 40
D = - 12*x^2*(- 24*y^2 + 24*x + 40) - 576*y^2
ans =
3941535027/25000000
4 2 2 4
x - 12 x y - 20 y + 2y
600
400
200
-200
5
4 5
3 4
2 3
2
1 1
0 0
y x
Program:
syms x y
f=x.^3+2*y.^3-12*x*y.^2-20*y.^2
fx=diff(f,x)
fy=diff(f,y)
[a,b]=solve(fx,fy);
double([a,b])
fxx=diff(fx,x)
fxy=diff(fx,y)
fyy=diff(fy,y)
D=fxx*fyy-(fxy)^2
D1=subs(D,[x,y],[ -1.4815,0.7807])
A1=subs(fxx,[x,y],[ -1.4815,0.7807])
D2=subs(D,[x,y],[ -1.9048,-0.9524])
A2=subs(fxx,[x,y],[ -1.9048,-0.9524])
if D1>0
if A1>0
disp('Attains Minima')
else
disp('Attains Maxima')
end
else
if D1==0
disp('No conclusion')
else
disp('Neither Minima nor Maxima')
end
end
if D2>0
if A2>0
disp('Attains Minima')
else
disp('Attains Maxima')
end
else
if D2==0
disp('no conclusion')
else
disp('Neither Minima nor maxima')
end
end
ezsurf(f,[0,5,0,5])
Output:
f =x^3 - 12*x*y^2 + 2*y^3 - 20*y^2
fx =3*x^2 - 12*y^2
fy =6*y^2 - 24*x*y - 40*y
ans =
0 0
-1.9048 -0.9524
-1.4815 0.7407
fxx =6*x
fxy =-24*y
fyy =12*y - 24*x - 40
D =- 576*y^2 - 6*x*(24*x - 12*y + 40)
D1=-4935508323/12500000
A1 =-8889/1000
D2 =-178582143/390625
A2 =7143/625