Magesh 21BMC026 Matlab Task 8 Completed
Magesh 21BMC026 Matlab Task 8 Completed
1. Introduction to MATLAB
6. Curve tracing
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 =[12345;23456;34567;45678]
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 =[12345;23456;34567;45678];
a(2,5)
MATLAB will execute the above statement and return the following result −
ans= 6
a =[12345;23456;34567;45678];
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 =[12345;23456;34567;45678];
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 =[12345;23456;34567;45678];
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 =[12345;23456;34567;45678];
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 =[12345;23456;34567;45678];
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 =[12345;23456;34567;45678];
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 =[123;456;789];
new_mat=a([2,3,2,3],:)
>> z1/z2
2
>> z1-z3
42
>> z2+z3-z1
-25
>>1+2/3*4-5
-1.3333
>>1/2/3/4
0.0417
>>1/2+3/4*5
4.2500
>>5-2*3*(2+7)
-49
>>(1+3)*(2-3)/3*4
-5.3333
>>(2-3*(4-3))*4/5
-0.8000
4. Calculate the expressions:
(1) sin 600 in radians
Sin(60)
-0.3048
(3) elog4
exp(log(4))
4
(4) cos 450 - sin 450
cosd(45)-sind(45)
(5) log e2
log(exp(2))
2
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, s 2, 1/s, s/2,
s+1
s=1: t=6:-1:1
(i) s+t
Output:
ans =
7 7 7 7 7 7
(ii) s-t
Output:
ans =
-5 -3 -1 1 3 5
(iii) s*t
Output:
ans =
6 10 12 12 10 6
(iv) s./t
Output:
ans =
(v) s.^2
Output:
ans =
1 4 9 16 25 36
(vi) 1./s
Output:
ans =
(vii) s./2
Output:
ans =
(viii) s+1
Output:
ans =
2 3 4 5 6 7
PROGRAM:
x=-1:0.1:1
y=((x+2).^2).*(x.^3+1)
poly(y)
OUTPUT:
ans =
1.0e+13 *
Columns 1 through 9
Columns 19 through 22
OUTPUT:
ans =
0
2
1
WORKSHEET-2
MATRIX OPERATIONS -
ADDITION, MULTIPLICATION, TRANSPOSE, INVERSE
2.1 OBJECTIVES:
( ) ( )
1 2 4 0 3 6
A= 0 5 6 B= 7 1 −1
7 8 4 5 7 −9
Example 1:Find the addition of the matrices and
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
A= 0 5 6 B= 7 1 −1
Example 2:Find the multiplication of the matrices 7 8 4 and 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
A= 0 5 6
Example 3: Find the transpose and inverse of the matrix 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
A= 0 19 16
17 8 6
Find the addition and subtraction of the matrices and
1.
( )
8 13 8
B= 15 10 −1
5 9 −9
PROGRAM:
A=[5 2 12;0 19 16;17 8 6];
B=[8 13 8;15 10 -1;5 9 -9];
disp('The matrix of A is');A
disp('The matrix of B is');B
disp('The sum of A and B is');C=A+B
disp('The difference of A and B is');D=A-B
OUTPUT:
The matrix of A is
A=
5 2 12
0 19 16
17 8 6
The matrix of B is
B=
8 13 8
15 10 -1
5 9 -9
C=
13 15 20
15 29 15
22 17 -3
-3 -11 4
-15 9 17
12 -1 15
( ) ( )
11 2 14 10 8 6
A= 10 5 60 B= 7 1 −11
17 8 4 9 7 −9
Find the multiplication of the matrices and
2.
PROGRAM:
A=[11 2 14;10 5 60;17 8 4];
B=[10 8 6;7 1 -11;9 7 -9];
disp('The matrix of A is');A
disp('The matrix of B is');B
disp('The multiplication of A and B is');C=A*B
OUTPUT:
The matrix of A is
A=
11 2 14
10 5 60
17 8 4
The matrix of B is
B=
10 8 6
7 1 -11
9 7 -9
C=
250 188 -82
675 505 -535
262 172 -22
( )
1 22 4
A= 18 5 16
Find the transpose and inverse of the matrix 7 28 4
3.
PROGRAM:
A=[1 22 4;18 5 16;7 28 4];
A=
1 22 4
18 5 16
7 28 4
The transpose of A
ans =
1 18 7
22 5 28
4 16 4
The inverse of A is
ans =
, .
PROGRAM:
A=[6 2 4;8 5 6;7 8 4];
B=[12 21 14;11 15 36;17 68 24];
disp('The matrix of A is');A
disp('The matrix of B is');B
disp('The multiplication of A and B is');C=A*B
OUTPUT:
The matrix of A is
A=
6 2 4
8 5 6
7 8 4
The matrix of B is
B=
12 21 14
11 15 36
17 68 24
C=
( )
91 24 42
A= 18 52 48
72 45 64
OUTPUT:
The matrix of A is
A=
91 24 42
18 52 48
72 45 64
The transpose of A
ans =
91 18 72
24 52 45
42 48 64
The inverse of A is
ans =
WORKSHEET-3
RANK OF A MATRIX AND SOLUTION OF A SYSTEM OF LINEAR EQUATIONS
3.1 OBJECTIVES:
( )
1 2 3 2
A= 2 3 5 1
Example 1: Find the rank of the matrices 1 3 4 5
A=[1 2 3 2;2 3 5 1;1 3 4 5]
disp('The matrix A= ');A
% To find rank of A
disp('The rank of A is ');
C=rank(A)
Output:
The matrix A=
A=
1 2 3 2
2 3 5 1
1 3 4 5
The rank of A is
C=
( )
1 2 3 2
A= 2 3 5 1
Example 2.Find the echelon form of the matrix 1 3 4 5
A=[1 2 3 2;2 3 5 1;1 3 4 5]
disp('The matrix A= ');A
% to find rank of A ;
disp('The echelon form of A is ');
D=rref(A)
Output:
The matrix A=
A=
1 2 3 2
2 3 5 1
1 3 4 5
The echelon form of A is
D =
1 0 1 -4
0 1 1 3
0 0 0 0
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
A= 1 1 −1 1
1. Find the rank of the matrices 3 3 −5 1
PROGRAM:
A=[1 1 1 3;1 1 -1 1;3 3 -5 1]
disp('The rank of A is :'); rank=rank(A)
OUTPUT:
A=
1 1 1 3
1 1 -1 1
3 3 -5 1
The rank of A is :
rank =
( )
−2 1 1 1
A= 1 −2 1 1
2. Find the echelon form of the matrix 1 1 −2 1
PROGRAM:
A=[-2 1 1 1;1 -2 1 1;1 1 -2 1]
disp('The echelon form of A is :'); B=rref(A)
OUTPUT:
A=
-2 1 1 1
1 -2 1 1
1 1 -2 1
B=
1 0 -1 0
0 1 -1 0
0 0 0 1
PROGRAM:
syms x y z
eqn1 = x+2*y-z==4;
eqn2 = x+3*y-2*z==5;
eqn3 = 2*x-y+3*z==3;
[A,B] = equationsToMatrix([eqn1,eqn2,eqn3],[x,y,z])
X = linsolve(A,B)
OUTPUT:
A=
[ 1, 2, -1]
[ 1, 3, -2]
[ 2, -1, 3]
B=
4
5
3
X=
2
1
0
[ ]
2 1 2 1
6 −6 6 12
A=
4 3 3 −3
4. Find the row echelon form of the matrix
2 2 −1 1
PROGRAM:
A=[2 1 2 1;6 -6 6 12;4 3 3 -3;2 2 -1 1]
disp('The echelon form of A is');B=rref(A)
OUTPUT:
A=
2 1 2 1
6 -6 6 12
4 3 3 -3
2 2 -1 1
B=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
( )
4 −5 1 2
A= 3 1 −2 9
5. Find the rank of the matrix 1 4 1 5
4 -5 1 2
3 1 -2 9
1 4 1 5
ans =
3
6. Solving system of linear equations
x+2 y+z=3
2 x+3 y+2 z=5
3 x−5 y+5 z=2
3 x+9 y−z=4
PROGRAM:
syms x y z
eqn1 = x+2*y+z==3;
eqn2 = 2*x+3*y+2*z==5;
eqn3 = 3*x-5*y+5*z==2;
eqn4 = 3*x+9*y-z==4;
[A,B]= equationsToMatrix([eqn1,eqn2,eqn3,eqn4],[x,y,z])
X = linsolve(A,B)
OUTPUT:
A=
[ 1, 2, 1]
[ 2, 3, 2]
[ 3, -5, 5]
[ 3, 9, -1]
B=
3
5
2
4
X=
-1
1
2
WORKSHEET-4
CHARACTERISTIC EQUATION OF A MATRIX AND CAYLEY-HAMILTON THEOREM
4.1 OBJECTIVES
To verify Cayley Hamilton Theorem and find the inverse of the matrix
( )
1 −1 4
A= 3 2 1
Example 2:Verify Cayley Hamilton Theorem for the matrix 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) (Hint:)
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 --------- A3 −2 A 2−7 A +12i=0−−−−−¿
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
( )
1 0 3
A= 2 1 −1
1 −1 1
PROGRAM:
A=[1 0 3;2 1 -1;1 -1 1]
x=poly(A)
round(x)
y=polyvalm(x,A)
display('Cayley Hamilton Theorem :');round(y)
i=eye(3)
InvA=(-A^2+3*A+i)/9
display('Inverse of A is :');InvA
OUTPUT:
A=
1 0 3
2 1 -1
1 -1 1
x=
ans =
1 -3 -1 9
y=
1.0e-13 *
ans =
0 0 0
0 0 0
0 0 0
i=
1 0 0
0 1 0
0 0 1
InvA =
0 0.3333 0.3333
0.3333 0.2222 -0.7778
0.3333 -0.1111 -0.1111
Inverse of A is :
InvA =
0 0.3333 0.3333
0.3333 0.2222 -0.7778
0.3333 -0.1111 -0.1111
PRODUCT:
A=[1 3 7;4 2 3;1 2 1]
x=poly(A)
round(x)
y=polyvalm(x,A)
display('Cayley Hamilton Theorem :');round(y)
i=eye(3)
display('Inverse of A is :');InvA=((-A^2+4*A+20*i)/-35)
OUTPUT:
A =
1 3 7
4 2 3
1 2 1
x =
ans =
1 -4 -20 -35
y =
1.0e-13 *
ans =
0 0 0
0 0 0
0 0 0
i =
1 0 0
0 1 0
0 0 1
Inverse of A is :
InvA =
( )
1 2 3
A= 2 −1 4
3 1 −1
PRODUCT:
A=[1 2 3;2 1 -4;3 1 -1]
x=poly(A)
round(x)
y=polyvalm(x,A)
display('Cayley Hamilton Theorem :');round(y)
i=eye(3)
display('Inverse of A is :');InvA=((-A^2+A+10*i)/20)
%s=inv(A)
OUTPUT:
A=
1 2 3
2 1 -4
3 1 -1
x=
ans =
1 -1 -10 20
y=
1.0e-13 *
0.0888 0 -0.0888
-0.0488 -0.0711 -0.0711
ans =
0 0 0
0 0 0
0 0 0
i=
1 0 0
0 1 0
0 0 1
Inverse of A is :
InvA =
-0.1500 -0.2500 0.5500
( )
2 1 1
A= 0 1 0
1 1 2
PRODUCT:
A=[2 1 1;0 1 0;1 1 -2]
x=poly(A)
round(x)
y=polyvalm(x,A)
display('Cayley Hamilton Theorem :');round(y)
i=eye(3)
display('Inverse of A is :');InvA=((-A^2+A+5*i)/5)
OUTPUT:
A=
2 1 1
0 1 0
1 1 -2
x=
ans =
1 -1 -5 5
y=
1.0e-14 *
0 0 0
ans =
0 0 0
0 0 0
0 0 0
i=
1 0 0
0 1 0
0 0 1
Inverse of A is :
InvA =
0 1.0000 0
PRODUCT:
A=[1 0 -1;3 4 5;0 -6 -7]
x=poly(A)
round(x)
y=polyvalm(x,A)
display('Cayley Hamilton Theorem :');round(y)
i=eye(3)
display('Inverse of A is :');InvA=((-A^2-2*A+i)/-20)
OUTPUT:
A=
1 0 -1
3 4 5
0 -6 -7
x=
ans =
1 2 -1 -20
y=
1.0e-13 *
0.4263 0 -0.1243
0 -0.7105 -0.5684
ans =
0 0 0
0 0 0
0 0 0
i=
1 0 0
0 1 0
0 0 1
Inverse of A is :
InvA =
5.1 OBJECTIVES
( )
1 −1 0
A= −1 2 1
0 1 1
Program:
A=[1 -1 0 ; -1 2 1 ; 0 1 1]
disp(' The Eigenvalues of A are ');
eig(A)
disp(' The Eigenvector of A are ');
[v,d]=eig(A)
round([v,d])
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
( )
1 −3 2 −1
−3 9 −6 3
B=
2 −6 4 −2
−1 3 −2 1
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
1.
PRODUCT:
OUTPUT:
A=
11 -4 -7
7 -2 -5
10 -4 -6
ans =
2.0000
-0.0000
1.0000
v=
2.0000 0 0
0 -0.0000 0
0 0 1.0000
ans =
-1 1 0 2 0 0
0 1 0 0 0 0
-1 1 1 0 0 1
( )
2 0 −1
A= 0 2 0
Find the eigenvalues of given that −1 0 2
2.
PROGRAM:
OUTPUT:
A=
2 0 -1
0 2 0
-1 0 2
B=
0.6667 0 0.3333
0 0.5000 0
0.3333 0 0.6667
ans =
0.3333
0.5000
1.0000
v=
0.3333 0 0
0 0.5000 0
0 0 1.0000
ans =
1 0 1 0 0 0
0 -1 0 0 0 0
-1 0 1 0 0 1
3.
PROGRAM:
A=
6 -2 2
-2 3 -1
2 -1 3
ans =
2.0000
2.0000
8.0000
v=
d=
2.0000 0 0
0 2.0000 0
0 0 8.0000
ans =
0 1 1 2 0 0
1 1 0 0 2 0
1 -1 0 0 0 8
( )
4 −20 −10
A= −2 10 4
Find the eigenvalues and eigenvectors of 6 −30 −13
4.
PROGRAM:
A=[4 -20 -10;-2 10 4;6 -30 -13]
OUTPUT:
A=
4 -20 -10
-2 10 4
6 -30 -13
-1.0000
0.0000
2.0000
v=
-0.8944 0.9806 0.0000
d=
-1.0000 0 0
0 0.0000 0
0 0 2.0000
ans =
-1 1 0 -1 0 0
0 0 0 0 0 0
0 0 -1 0 0 2
( )
−2 2 −3
A= 2 1 −6
Find the eigenvalues and eigenvectors of −1 −2 0
5.
PROGRAM:
OUTPUT:
A=
-2 2 3
2 1 -6
-1 -2 0
The eigen values of A are :
ans =
-2.1623
4.1623
-3.0000
d=
-2.1623 0 0
0 4.1623 0
0 0 -3.0000
ans =
-1 0 -1 -2 0 0
0 1 0 0 4 0
0 0 0 0 0 -3
WORKSHEET-6
CURVE TRACING
6.1 OBJECTIVES
To plot a two dimensional plot.
To plot a three dimensional plot.
6.2Example 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
2 2
x y
+ =1
Example 6. Plot the curve 4 9
ezplot('x^2/4+y^2/9=1');
axis equal;
3-D Plot
2 2
−( x + y )
Example 7. Create a 3D surface map for the function g=xe .
[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=sinx such that x varies from 0 to 2 with h=/100.
PROGRAM:
x=[0:pi/100:2*pi];
y=sin(x);
plot(x,y)
xlabel('x')
ylabel('sin(x)')
title('sin(x) Graph')
OUTPUT:
8 7 6 5 4 3
2. For f (x )=8 x −7 x +12 x −5 x +8 x +13 x −12 x+ 9 , compute f(2),roots of f(x) and
plot for x= 0 to 20 in step of 0.1.
PROGRAM:
x=[0:0.1:20];
y=(8*x.^8)-(7*x.^7)+(12*x.^6)-(5*x.^5)+(8*x.^4)+(13*x.^3)-12*x+9;
f=polyvalm(x,2)
a=[8 -7 12 -5 8 13 0 12 9];
roots(a)
plot (x,y)
OUTPUT:
f=
3.2139e+59
ans =
1.0326 + 0.7489i
1.0326 - 0.7489i
-0.2873 + 1.1658i
-0.2873 - 1.1658i
0.3608 + 0.9535i
0.3608 - 0.9535i
-0.6687 + 0.1194i
-0.6687 - 0.1194i
OUTPUT:
2
+( x + y 2)
4.Create a 3D surface map for the function g=xe .
PROGRAM:
[x,y] = meshgrid(-2:.2:2);
g=x.*exp(x.^2+y.^2);
surf(x,y,g)
OUTPUT:
5. Plot the curve y = x3
PROGRAM:
x=[-10:1:10];
y=x.^3;
plot(x,y,'*')
OUTPUT:
WORKSHEET-7
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
∫∫ xy 2 dxdy
Let’s take the following double integral for example: 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
f (t )=3 t 2 +2t 2
1) Compute the derivative of the function
PROGRAM:
syms t;
f=3*t^2+2*t^2;
a=diff(f)
OUTPUT:
syms t;
f=3*t^2+2*t^2;
a=diff(f)
3 2
2) Find the differentiation of f (x )=18 x +12 x +5 x
PROGRAM:
syms x;
f=18*x^3+12*x^2+5*x;
ans=diff(f)
OUTPUT:
ans =
54*x^2 + 24*x + 5
2
cos(3 x +2 x+1)
3) Compute the derivative of
PROGRAM:
syms x;
f=cos(3*x^2+2*x+1);
a=diff(f)
OUTPUT:
a=
2 3 5
PROGRAM:
syms x y z;
h=x*y*z.^3;
a=int(int(int(h,x,3,5),y,0,3),z,0,2)
OUTPUT:
a=
144
2 8
∫∫ a 2 c 5 da dc
5) Find the value of 0 5
PROGRAM:
syms a c
h=(a^2)+(c^5);
a=int(int(h,a,5,8),c,0,2)
OUTPUT:
a=
290
2 3 5
∫∫∫ u3 v 2 w 5 dudvdw
0 0 3
PROGRAM:
syms u v w
h=u^3*v^2*w^5;
a=int(int(int(h,u,3,5),v,0,3),w,0,2)
OUTPUT:
a=
13056
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
=5 y
.2Example 1: Solve dx
8
df
=−2 f +cos t
Example 2. Solve 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)
Example 3. Solve
dx
+
x
dy 1+ y 2
=tan
−1 y
( )
1+ y 2
% LEIBNITZ'S FORM - EX2
x=dsolve('Dx+(1/(1+y^2))*x=atany/(1+y^2)','y')
Output:
x= atany- C10*exp(-atan(y))
2 '
Example 4: Solve ( D −1) y=0; y(0)=−1 ∧ y (0)=2
Output:
y =exp(t)/2 - (3*exp(-t))/2
2 x
Example 5: Solve ( D +5 D+6) y=e
y=dsolve('D2y+5*Dy+6*y=exp(x)','x')
Output:
y=
exp(x)/12 + C25*exp(-2*x) + C26*exp(-3*x)
2 −x
Example 6. Solve ( D +5 D+6) y=4 e log 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))
3 2 x ' ''
Example 4. Solve ( D −D +D−1) y=e +cos x , y (0 )= y (0 )= y (0 )=0
y=dsolve('D3y-D2y+Dy-y=exp(x)
+cos(x)','y(0)=0','Dy(0)=0','D2y(0)=0','x')
TASK :8
1. Solve
( dy
dt ) −t
+4 y (t )=e , y (0 )=1 .
PROGRAM:
syms x;
y=dsolve('Dy+4*y=exp(-t)','t')
OUTPUT:
y=
exp(-t)/3 + (2*exp(-4*t))/3
dy
− y cot x=2 x sin x
2. Solve dx
PROGRAM:
syms x;
y=dsolve('Dy-y*cot(y)=2*x*sin(x)')
OUTPUT:
y=
x^2sin(x)+C1*sin(x)
( D2−D+1 ) y=0
3. Solve the differential equation
PROGRAM:
y=dsolve('D2y-Dy+1=0','x')
OUTPUT:
y=
C1 + x + C2*exp(x) + 1
PROGRAM:
y=dsolve('D3y-3*D2y+3*Dy-y=0','x')
OUTPUT:
y=
PROGRAM:
y=dsolve('D4y-2*D2y+y=0','x')
OUTPUT:
y=
C3*exp(x) + C1*exp(-x) + C2*x*exp(-x) + C4*x*exp(x)
4 3 2 2 x
( D −2 D +D ) y=x +e
6. Solve the differential equation
PROGRAM:
y=dsolve('D4y-2*D3y+D2y=x^2+exp(x)','x')
OUTPUT:
y=
2 −x
( D+1) y=e cos x
7. Solve the differential equation
PROGRAM:
y=dsolve('D2y+2*Dy+y=exp(-x)*cos(x)','x')
OUTPUT:
y=
( D2 +4) y=cos 3 x
8. Solve the differential equation
PROGRAM:
y=dsolve('D2y+4*y=cos(3*x)','x')
OUTPUT:
y=
C1*cos(2*x) - C2*sin(2*x) + sin(2*x)*(sin(5*x)/20 + sin(x)/4) - (2*cos(2*x)*(10*tan(x/2)^2
- 20*tan(x/2)^4 + 30*tan(x/2)^6 - 5*tan(x/2)^8 + 1))/(5*(tan(x/2)^2 + 1)^5)
WORKSHEET- 9
DETERMINING MAXIMA AND MINIMA OF A FUNCTION OF ONE
VARIABLE
9.1OBJECTIVE
To determine the maxima and minima of a function of a single variable
9.2Example 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 =08
fxx = 6*x - 24
D = 24 D=-24
Attains MinimaAttains Maxima
Task:9
4 3
f (x )=x −5 x +3 x−18
1.Find the maximum value of the function
3 2
f (x )=7 x −2 x −5
2. Find the minimum value of the function
3 2
f (x )=x −3 x −24 x +5
3. Find the extrema of the function
f (x )=x (12−2 x )2
4. Find the extrema of
WORKSHEET- 10
DETERMINING MAXIMA AND MINIMA OF A FUNCTION OF TWO VARIABLES
10.1 OBJECTIVES
To determine maxima and minima of a function of two variables.
10.2Example 1: Find the extreme value of the function
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
x4 - 12 x y2 - 20 y2 + 2 y4
600
400
200
-200
5
4 5
3 4
2 3
2
1 1
y 0 0
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
4 4 2 2
u( x , y )=x + y −2 x +4 xy−2 y
3. Examine for extreme values of the function
4. Find the maximum and minimum values of the function
f (x , y )=x 3 + y 3 −63( x+ y )+12 xy
5. Find the maximum or minimum value of the function
f (x , y )=2( x 2 − y 2 )−x 4 + y 4