MATLAB Manual CS and IS
MATLAB Manual CS and IS
Department of Mathematics
MATLAB MANUAL
I Semester
2022 Scheme
Prepared By:
Contents
TABLE OF CONTENTS
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 1 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 2 | 59
RV Institute of Technology & Management ®
2. Put your signature on attendance sheet first, submit the record and then go to your place
3. Without fair record and lab manual you are not allowed to the lab.
4. Handlings of other things, which are not related to the experiment, are strictly prohibited inside
the lab.
5. You may be charged with full cost and additional penalty for destroying the equipment’s.
7. Till the time you are inside the lab you should stick to your place and don’t move here & there.
8. Before leaving the lab, be sure that power supply is switched off and you have returned the
components proper manner otherwise all the group members will be punished.
9. Your duly filled lab manual along with stressing sheet must be verified by any of the instructors
10. You yourself will be responsible for exchange or missing of any precious materials.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 3 | 59
RV Institute of Technology & Management ®
1. Maintain the contain page with date of performing the experiment, experiment name, page
number.
2. Put the page numbers on the top of the individual page, Date & experiment no.
3. Write the aim of the experiment and required short theoretical description with required
5. Put your full signature at bottom right corner of the last page of that experiment.
6. Record should be very neat and clean and the entire circuits/block diagram should be drawn
FORMAT
2. Theory
4. Program
5. Output
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 4 | 59
RV Institute of Technology & Management ®
PROCEDURE:
1. Open MATLAB
MARK DISTRIBUTION
understood the above rules and regulations of the Simulation Practice on Mat lab and will accept
any kind of punishment given by the instructor for violating the rules mentioned above.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 5 | 59
RV Institute of Technology & Management ®
INTRODUCTION:
MATLAB, which stands for Matrix Laboratory, is a state-of-the-art mathematical software
package, which is used extensively in both academia and industry. It is an interactive Program for
numerical computation and data visualization, which along with its Programming capabilities
provides a very useful tool for almost all areas of science and engineering. Unlike other
mathematical packages, such as MAPLE or MATHEMATICA, MATLAB cannot perform
symbolic manipulations without the use of additional Toolboxes. It remains however, one of the
leading software packages for numerical computation. As you might guess from its name,
MATLAB deals mainly with matrices. One of the many advantages of MATLAB is the natural
notation used. It looks a lot like the notation that you encounter in a linear algebra. This makes the
use of the Program especially easy and it is what makes MATLAB a natural choice for numerical
computations. The purpose of this experiment is to familiarize MATLAB, by introducing the basic
features and commands of the Program.
Features of MATLAB:
1. It is a high-level language for numerical computation, visualization and application
development.
2. It also provides an interactive environment for iterative exploration, design and problem
solving.
3. It provides vast library of mathematical functions for linear algebra, statistics, Fourier analysis,
filtering, optimization, numerical integration and solving ordinary differential equations.
4. It provides built-in graphics for visualizing data and tools for creating custom plots.
MATLAB's Programming interface gives development tools for improving code quality,
maintainability, and maximizing performance.
5. It provides tools for building applications with custom graphical interfaces.
6. It provides functions for integrating MATLAB based algorithms with external applications and
languages such as C, Java, .NET and Microsoft Excel.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 6 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 7 | 59
RV Institute of Technology & Management ®
Trigonometric function:
1. sin - trigonometric sine
2. cos - trigonometric cosine
3. tan - trigonometric tangent
4. asin - trigonometric inverse sine (arcsine)
5. acos - trigonometric inverse cosine (arccosine)
6. atan - trigonometric inverse tangent (arctangent)
Exponential Function:
1.exp - exponential
2.log - natural logarithm
3.abs - absolute value
4.sqrt - square root
5.rem – remainder
6.power-array power
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 8 | 59
RV Institute of Technology & Management ®
OPERATORS IN MATLAB:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. MATLAB is designed to operate primarily on whole matrices and arrays.
Therefore, operators in MATLAB work both on scalar and non-scalar data.
MATLAB allows the following types of elementary operations:
Arithmetic operators:
MATLAB allows two different types of arithmetic operations:
1. Matrix arithmetic operations
2. Array arithmetic operations
Matrix arithmetic operations are same as defined in linear algebra. Array operations are executed
element by element, both on one-dimensional and multidimensional array.
The matrix operators and array operators are differentiated by the period (.) symbol. However, as
the addition and subtraction operation is same for matrices and arrays, the operator is same for
both cases. The following table gives brief description of the operators
Operator Description Operator
A+B Binary addition plus(A,B)
+A Unary plus uplus(A)
A-B Binary subtraction minus(A,B)
-A Unary minus uminus(A)
A*B Matrix multiplication mtimes(A,B)
A.*B Array-wise multiplication times(A,B)
A/B Matrix right division mrdivide(A,B)
A./B Array-wise right division rdivide(A,B)
A\B Matrix left division mldivide(A,B)
A.\B Array-wise left division ldivide(A,B)
A^B Matrix power mpower(A,B)
A.^B Array-wise power power(A,B)
A' Complex transpose ctranspose(A)
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 9 | 59
RV Institute of Technology & Management ®
Relational operators:
Relational operators can also work on both scalar and non-scalar data. Relational operators for
arrays perform element-by-element comparisons between two arrays and return a logical array of
the same size, with elements set to logical 1 (true) where the relation is true and elements set to
logical 0 (false) where it is not
operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Logic Operator:
MATLAB offers two types of logical operators and functions:
Element-wise - These operators operate on corresponding elements of logical arrays. Element-
wise logical operators operate element-by-element on logical arrays. The symbols &, |, and ~ are
the logical array operators AND, OR, and NOT.
Short-circuit - These operators operate on scalar and logical expressions. Short-circuit logical
operators allow short-circuiting on logical operations. The symbols && and || are the logical short-
circuit operators AND and OR.
operator Description
& Logical or element-wise
AND
| Logical or element-wise OR
~ logical or element-wise NOT
&& Short-circuit AND
|| Short-circuit OR
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 10 | 59
RV Institute of Technology & Management ®
PROGRAM:
%arithmetic operation of matrix
clc;
clear all; close all;
a=[3 2 1;0 1 2;3 0 1]
b=[2 1 0;2 0 0;1 2 1]
c=a+b d=a-b e=a*b f=5*a g=a.*b h=a/b i=a/2 j=a./b
%special matrix function
clc;
clear all; close all;
k=[2 4 6;1 3 7;5 8 9]
l=det(k) m=inv(k) n=rank(k) o=size(k) p=diag(k) q=triu(k) r=tril(k) s=eye(3,3) t=zeros(4,3)
u=ones(3,3) v=rand(3,4)
OUTPUT:
%arithmetic operation with matrix
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 11 | 59
RV Institute of Technology & Management ®
Vector: is a one-dimensional array of numbers. MATLAB allows creating two types of vectors:
Row vectors and Column vectors
Row vectors: are created by enclosing the set of elements in square brackets, using space or
comma to delimit the elements.
Column vectors: are created by enclosing the set of elements in square brackets, using semicolon
(;) to delimit the elements.
Linear spacing: is a command to generate linearly spaced vectors.
Syntax: variable=linspace(start number, end number, number of element)
Problems: theta = linspace(0,2*pi,50)
1. Scaler function:
sin - trigonometric sine cos - trigonometric cosine tan - trigonometric tangent
asin - trigonometric inverse sine (arcsine)
acos - trigonometric inverse cosine (arccosine)
atan - trigonometric inverse tangent (arctangent)
.exp - exponential
.log - natural logarithm
.abs - absolute value
.sqrt - square root
2. Vector Functions:
Other MATLAB functions operate essentially on vectors returning a scalar value. Some of these
functions are given below.
1. Max- largest component: get the row in which the maximum element lies
2. min- smallest component
3. length-length of a vector
4. sort-sort in ascending order
5. sum-sum of elements
6. prod- product of elements
7. median-median value
8. mean-mean value std standard deviation
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 12 | 59
RV Institute of Technology & Management ®
MATLAB provides numerous commands for plotting graphs. The commonly used commands
are:
plot: It is the command used to plot a continuous signal.
stem:it is used to draw a discrete signal i.e. Non-continuous spectral amplitude signal
subplot:it is the command used to create no. of plots in the same figure, each of these plots is
called a subplot. Syntax for the command is subplot(m, n, p)
where, m and n are the number of rows and columns of the plot array and p specifies where to
put a particular plot.
axis equal: this command allows generating the plot with the same scale factors and the spaces
on both axes.
The following table shows some of the commonly used commands for plotting.
Plotting commands:
Command Purpose
close Closes the current plot.
close all Closes all plots.
Axis Sets axis limits. axis([xmin xmax ymin ymax])
Grid on/off Displays gridlines.
Plot Generates xy plot. Ex-plot(x,y)
stem Creates stem plot.
subplot Creates plots in sub windows.
title -Puts text at top of plot.
xlabel Adds text label to x-axis.
ylabel Adds text label to y-axis.
Figure -Opens a new figure window.
Hold on/off Freezes/unfreezes current plot.
text Places string in figure
print Prints plot or saves plot to a file.
bar Creates bar chart.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 13 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 14 | 59
RV Institute of Technology & Management ®
LIST OF EXPERIMENTS
8. Numerical solution of system of linear equations, test for consistency and graphical
representation
10. Compute eigenvalues and eigenvectors and find the largest and smallest eigenvalue by
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 15 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 16 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 1
AIM OF THE EXPERIMENT: 2D- Plots for Cartesian and Polar Curves.
Problem 1: Plot the sinusoidal waves between −2𝜋 and 2𝜋 using MATLAB software.
Program:
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)
Output:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 17 | 59
RV Institute of Technology & Management ®
Here n is an integer whose value decides the number of leaves in the rose. There are:
• 2n petals, if n is even.
• n petals, if n is odd.
Program:
syms r theta
theta = 0:0.01:2*pi;
r = 2*sin(3*theta);
polarplot(theta,r)
Output:
Problem 3: Plot the graph of Strophoid between −2 < 𝑥 < 2 using MATLAB software.
Program:
syms y x
ezplot(y^2*(2-x)-x^2*(2+x),[-2,2])
xlabel(‘x’)
ylabel(‘y’)
grid on
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 18 | 59
RV Institute of Technology & Management ®
Output:
Problem 4: Plot the graph of Cardioid in the positive x-axes using MATLAB software.
Theory: The equation of the Cardioid in the positive x-axes is 𝑟 = 3(1 + 𝑐𝑜𝑠 𝜃).
Program:
syms theta
theta = 0:0.01:2*pi;
r = 3*(1+cos(theta));
polarplot(theta,r)
Output:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 19 | 59
RV Institute of Technology & Management ®
Program:
syms y x z
[X,Y,Z] = ellipsoid(0,0,0,1.5,1.5,3);
surf(X,Y,Z);
Output:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 20 | 59
RV Institute of Technology & Management ®
Output:
Program:
t = (-10:0.25:10)'
impulse = t==0
plot(t,[impulse])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Pluse Signal')
Output:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 21 | 59
RV Institute of Technology & Management ®
Output:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 22 | 59
RV Institute of Technology & Management ®
Output:
Exercise:
4. Trace the curve r = 2cos2 (Four leaved rose) using MATLAB software
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 23 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 24 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. – 2
AIM OF THE EXPERIMENT: Finding Curvature and Radius of Curvature of a Given Curve.
Theory: 𝑦 = 2𝑥 2 + 3𝑥 + 5
𝑦′ = 4𝑥 + 3 𝑎𝑡 (1,2) 𝑖𝑠 𝑦′ = 7
𝑦′′ = 4 𝑎𝑡 (1,2) 𝑖𝑠 𝑦′′ = 4
3
[1 + 𝑦′2 ]2 125 √2
𝜌= = , curvature =
𝑦′′ √2 125
Program:
syms y x a
y=2*x^2+3*x+5;
y1=diff(y);
y11=subs(y1,x,1);
y2=diff(y1);
rc=simplify(((1+y11^2)^(3/2))/y2)
curvature=1/rc
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 25 | 59
RV Institute of Technology & Management ®
Program:
syms y x a
y=(a^(1/2)-x^(1/2))^2;
y1=diff(y);
y11=subs(y1,x,a/4);
y2=diff(y1);
y12=subs(y2,x,a/4);
rc=simplify(((1+y11^2)^(3/2))/y12)
curvature=1/rc
Exercise:
1. Compute the radius of curvature for xy 2 = a3 - x 3 at (a, 0) using MATLAB software.
2. Find the radius of curvature of the curve x4 + x3 + a ( x2 + y 2 ) − a2 y = 0 at ( 0,0) using
MATLAB software.
𝑎 𝑏
3. Find the radius of curvature for the curve 𝑥 = 𝑎 𝑐𝑜𝑠 𝜃 , 𝑦 = 𝑏 𝑠𝑖𝑛 𝜃 𝑎𝑡 ( , ) using
√2 √2
MATLAB software.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 26 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 3
AIM OF THE EXPERIMENT: Finding partial derivatives and Jacobian.
𝜕𝑓 𝜕𝑓 𝜕2 𝑓 𝜕2 𝑓
Problem 1: Compute the derivatives 𝜕𝑥 , 𝜕𝑦 , 𝜕𝑥𝜕𝑦 𝑎𝑛𝑑 𝜕𝑥 2 for the function
𝑓(𝑥) = 𝑦𝑒 𝑥 + 𝑥 2 𝑦 − log(𝑥𝑦) using MATLAB software.
Output:
fx = 2*x*y + y*exp(x) - 1/x
fy= exp(x) + xˆ2 - 1/y
fxy = 2*x + exp(x)
fxx = 2*y + y*exp(x) + 1/xˆ2
𝜕𝑓
Problem 2: Compute the derivative 𝜕𝑥 at 𝑥 = 2, 𝑦 = 3 for the function 𝑓(𝑥, 𝑦) = 𝑒 𝑥 (𝑥𝑐𝑜𝑠𝑦 −
𝑦𝑠𝑖𝑛𝑦) using MATLAB software.
Program:
syms y x
f=exp(x)*(x*cos(y)-y*sin(y));
z=diff(f,x);
subs(z,{x,y},{2,3})
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 27 | 59
RV Institute of Technology & Management ®
Output:
𝜕[𝑥 𝑦 𝑧]
Problem 3: Compute 𝐽 = 𝜕[𝑢,𝑣,𝑤] for 𝑥 = 𝑢 − 𝑢𝑣; 𝑦 = 𝑢𝑣(1 − 𝑤); 𝑧 = 𝑢𝑣𝑤 using MATLAB
software.
𝜕[𝑥 𝑦 𝑧] 1−𝑣 −𝑣 0
𝐽= = |𝑣(1 − 𝑤) 𝑢(1 − 𝑤) −𝑢𝑣| = 𝑢2 𝑣
𝜕[𝑢, 𝑣, 𝑤]
𝑣𝑤 𝑢𝑤 𝑢𝑣
Program:
syms u v w
Jm=jacobian([u-u*v,u*v*(1-w),u*v*w],[u,v,w]);
J=det(Jm)
Output:
ans J= 𝑢2 𝑣
𝜕[𝑥 𝑦 𝑧]
Problem 4: Compute 𝜕[𝑟,𝜃,Φ] for 𝑥 = 𝑟 𝑠𝑖𝑛𝜃 𝑐𝑜𝑠Φ; 𝑦 = 𝑟 𝑠𝑖𝑛𝜃 𝑠𝑖𝑛Φ; 𝑧 = 𝑟 𝑐𝑜𝑠𝜃 using
MATLAB software.
Program:
syms r theta pi
Jm=jacobian(r*sin(theta)*cos(pi), r*sin(theta)*sin(pi), r*cos(theta) ,[r,theta,pi]);
J=det(Jm)
Output:
ans J= 𝑟 2 sin 𝜃
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 28 | 59
RV Institute of Technology & Management ®
Exercise:
𝜕2 𝑓
1. Show that the function 𝑓 = cos 𝑥 cosh 𝑦 satisfies 2-dimensional Laplace’s equation 𝜕𝑥 2 +
𝜕2 𝑓
= 0 using MATLAB software.
𝜕𝑦 2
𝜕2 𝑢 1 𝜕𝑢 1 𝜕2 𝑓
2. If 𝑢 = 𝑒 𝑎𝜃 cos(𝑎 log 𝑟), show that 𝜕𝑟 2 + 𝑟 𝜕𝑟 + 𝑟 2 𝜕𝜃2 = 0 using MATLAB software.
𝑥+𝑦 𝜕(𝑢, 𝑣)
3. If 𝑢 = 1−𝑥𝑦 , 𝑣 = tan−1 𝑥 + tan−1 𝑦 , find using MATLAB software.
𝜕(𝑥, 𝑦)
(u, v)
4. If u = 2 xy, v = x 2 − y 2 , x = r cos , y = r sin , find using MATLAB software.
(r , )
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 29 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 30 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 4
AIM OF THE EXPERIMENT: Applications to Maxima and Minima of two variables
𝜕 2𝑓 𝜕 2𝑓 𝜕 2𝑓
𝑟= =6x , 𝑠 = =0, t= = 6𝑦 𝑎𝑛𝑑 𝑟𝑡 − 𝑠 2 = 12𝑥𝑦
𝜕𝑥 2 𝜕𝑥𝜕𝑦 𝜕𝑦 2
At the point (1,2) , 𝑟 > 0 and 𝑟𝑡 − 𝑠 2 > 0.
It follows that 𝑓(𝑥, 𝑦) is minimum at the point (1,2). The maximum value is 2.
Program:
syms y x a
f=@(x,y) x^3+y^3-3*x-12*y+20;
p=diff(f,x);q=diff(f,y);
r(x)=diff(p,x)
s(x)=diff(q,x)
t(y)=diff(q,y)
r1=subs(r(x),{x,y},{1,2})
s1=subs(s(x),{x,y},{1,2})
t1=subs(t(y),{x,y},{1,2})
if r1*t1-s1^2>0
if r1<0
disp('The function is maximum')
disp('The maximum value')
t=f(1,2)
elseif r1>0
disp('The function is minimum')
disp('The minimum value')
is=f(1,2)
else
disp('Further investigation is needed')
end
end
[x,y]=meshgrid(-2:0.2:2)
z= x.^3+y.^3-3*x-12*y+20
surf(x,y,z)
Output:
The function is minimum at (1,2)
The minimum value 2
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 31 | 59
RV Institute of Technology & Management ®
Program:
syms y x a
f=@(x,y) = 𝑥^3 + 3 ∗ 𝑥 ∗ 𝑦^3 − 15 ∗ 𝑥 2 − 15 ∗ 𝑦 2 + 72 ∗ 𝑥;
p=diff(f,x);q=diff(f,y);
r(x)=diff(p,x)
s(x)=diff(q,x)
t(y)=diff(q,y)
r1=subs(r(x),{x,y},{4,0})
s1=subs(s(x),{x,y},{4,0})
t1=subs(t(y),{x,y},{4,0})
if r1*t1-s1^2>0
if r1<0
disp('The function is maximum')
disp('The maximum value')
t=f(1,2)
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 32 | 59
RV Institute of Technology & Management ®
elseif r1>0
disp('The function is minimum')
disp('The minimum value')
is=f(1,2)
else
disp('Further investigation is needed')
end
end
Output:
The function is maximum
The maximum value 112
Problems 3: Compute the extreme values of the function 𝑓(𝑥, 𝑦) = 𝑥^3 ∗ 𝑦^2 ∗ (1 − 𝑥 − 𝑦)
1 1
at (2 , 3) using MATLAB software.
Program:
syms y x a
f=@(x,y) = 𝑥 3 ∗ 𝑦 2 ∗ (1 − 𝑥 − 𝑦);
p=diff(f,x);q=diff(f,y);
r(x)=diff(p,x)
s(x)=diff(q,x)
t(y)=diff(q,y)
1 1
r1=subs(r(x),{x,y},{ , })
2 3
1 1
s1=subs(s(x),{x,y},{2 , 3})
1 1
t1=subs(t(y),{x,y},{2 , 3})
if r1*t1-s1^2>0
if r1<0
disp('The function is maximum')
disp('The maximum value')
t=f(1,2)
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 33 | 59
RV Institute of Technology & Management ®
elseif r1>0
disp('The function is minimum')
disp('The minimum value')
is=f(1,2)
else
disp('Further investigation is needed')
end
end
Output:
1 1
The function is maximum at (2 , 3).
1
The maximum value .
144
Exercise:
1. Determine the maxima and minima of f(x, y) = x.^3 +3*x*y.^2 -3*x.^2-3*y.^2+4 at (0,0) and
(2,0) using MATLAB software.
2. Determine the extremum value of f(x, y) = x.^2*y(x+2*y-4) at (2, 0.5) using MATLAB
software.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 34 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 5
AIM OF THE EXPERIMENT: Solutions of first order differential equations and plotting the
graphs
dy
Problem 1: Write the MATLAB code to solve ODE + xy = 2 x
dx
dy
Theory: The analytical solution of Linear DE + xy = 2 x is given by
dx
y ( I .F ) = ( I .F ) Qdx + c
x2 x2
ye = e 2 x dx + c
2 2
− x2
y = 2 + c1e 2
.
Program:
Solution: syms y(x)
y(x)=dsolve(diff(y,x)+x*y==2*x)
−𝑥2
Output: 𝑦(𝑥) = 2 + 𝑐1 𝑒 2 .
dy
Problem 2: Write the MATLAB code to solve IVP + xy = 2 x with y ( 0 ) = 1.
dx
dy
Theory: The analytical solution of Linear DE + xy = 2 x is given by
dx
y ( I .F ) = ( I .F ) Qdx + c
x2 x2
ye = e 2 x dx + c1
2 2
− x2
y = 2 + c1e 2
, Applying initial conditions, we get c1 = −1.
− x2
Therefore y = 2 − e 2
.
Program:
syms y(x)
y(x)=dsolve(diff(y,x)+x*y==2*x , y(0) == 1)
−𝑥2
Output: 𝑦(𝑥) = 2 − 𝑒 2
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 35 | 59
RV Institute of Technology & Management ®
𝑑𝑥
Problem 3: Write the MATLAB code to solve IVP = 𝑥 + 𝑡, given 𝑥(0) = 0 and hence plot
𝑑𝑡
the solution.
𝑑𝑥
Theory: The analytical solution of Linear DE = 𝑥 + 𝑡 is given by
𝑑𝑡
x ( I .F ) = ( I .F ) Qdt + c
xe−t = e−t t dx + c1
And applying initial conditions, we get
𝑥(𝑡) = 𝑒 −𝑡 − 𝑡 − 1
Program:
syms x(t)
ode = diff(x,t) == x+t;
xSol(t) = dsolve(ode)
cond = x(0) == 0;
xSol(t) = dsolve(ode,cond)
Output: 𝑥(𝑡) = 𝑒 −𝑡 − 𝑡 − 1
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 36 | 59
RV Institute of Technology & Management ®
2
dy
Problem 4: Write the MATLAB code to solve IVP + y = 1 with y ( 0 ) = 0.
dx
2
dy
Theory: The analytical solution of Linear DE + y = 1 is given by
dx
y ( I .F ) = ( I .F ) Qdx + c
ye− x = e− x dx + c1
And applying initial conditions, we get y = 1 − e− x .
Program:
Solution: syms y(x)
y(x)=dsolve((diff(y,x)+y)^2==1 , y(0) == 0)
Output:𝑦(𝑥) = 𝑒 −𝑥 − 1𝑎𝑛𝑑𝑦(𝑥) = 1 − 𝑒 −𝑥
Problem 5: The resistance of 100 , an inductance of 0.5H are connected in series with a battery
of 20V. Find the current in the circuit, if i = 0 at 𝑡 = 0 using MATLAB software.
di
Theory: The DE for LR circuit is L + Ri = V
dt
di
0.5 + 100i = 20
dt
di
+ 200i = 40 with i ( 0 ) = 0
dt
And its solution is given by
i ( I .F ) = ( I .F ) Qdt + c
ie200t = e200t 40 dt + c1
1
And applying initial conditions, we get 𝑖(𝑡) = 5 (1 − 𝑒 −200𝑡 ).
Program:
syms i(t)
i(t)=dsolve(diff(i,t)+200*i== 40 , i(0) == 0)
1
Output:𝑖(𝑡) = 5 (1 − 𝑒 −200𝑡 ).
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 37 | 59
RV Institute of Technology & Management ®
Problem 6: An Aeroplane uses a parachute and other means of breaking as it slows down on the
runway after landing. Its acceleration is given by a = −0.003v2 − 10 m / s 2 . Consider an Aeroplane
with a velocity of 25km/h that opens its parachute and starts decelerating at t = 0sec. By solving
dv
the differential equation find the velocity using MATLAB software. [Hint: Take a = ]
dt
dv
The DE for parachute model is = −0.003v2 − 10 with v ( 0) = 25
dt
Program:
syms v(t)
v(t)=dsolve(diff(v,t)+0.003*v^2== -10 , v(0) == 25)
−100 √3 √3
Output:𝑣(𝑡) = 𝑡𝑎𝑛 [ 30 (3𝑡 − 10√3 𝑡𝑎𝑛−1 ( 4 ))].
√3
Exercise:
dy
1. Write the MATLAB code to solve + cot x y = cos x
dx
2. The resistance of 10 , an inductance of 2H are connected in series with a battery of 40V.
Find the current in the circuit, if i = 0 at 𝑡 = 0 using MATLAB software.
2
dx
3. Write the MATLAB code to solve IVP + xt = 2 x with x ( 0 ) = 1.
dt
4. Write the MATLAB code to solve ydx + 2 xdy = 0 .
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 38 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 6
AIM OF THE EXPERIMENT: Finding GCD of two integers using Euclid’s Algorithm
Problem 1: Compute MATLAB code for GCD of 12378 and 3054 using Euclid’s algorithm
Program:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 39 | 59
RV Institute of Technology & Management ®
GCD = b;
fprintf('The GCD(%2d, %2d) is (%2d)',x,y,GCD)
Output:
The GCD(12378,3054)=6
Problem 2: Compute MATLAB code for the greatest common divisor of 4147 and 10672 using
Euclid’s algorithm.
Program:
Output:
The GCD(4147,10672)=29
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 40 | 59
RV Institute of Technology & Management ®
Exercise:
1. Find how the Euclidean Algorithm works in a concrete case by calculating GCD (12378,
3054), GCD(143, 227), GCD(306, 657), and GCD(272, 1479) with MATLAB software.
2. In MATLAB use the Euclidean Algorithm to obtain integers x and y satisfying the
following:
(a) 𝑔𝑐𝑑(56, 72) = 56𝑥 + 72𝑦.
(b) 𝑔𝑐𝑑(24, 138) = 24𝑥 + 138𝑦.
(c) 𝑔𝑐𝑑(119, 272) = 119𝑥 + 272𝑦.
(d) 𝑔𝑐𝑑(𝑙 769, 2378) = 1769𝑥 + 2378𝑦.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 41 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 42 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 7
AIM OF THE EXPERIMENT: Applications of Wilson Theorem
From (1) 721001 ≡ 101001 (𝑚𝑜𝑑 31) ≡ 1 ∗ 14 ∗ 7 ∗ 10(𝑚𝑜𝑑 31) = 19(𝑚𝑜𝑑 31)
Hence remainder = 19
Program:
p=31;
a=72;
n=isprime(p);
if n==1
fprintf('%d is prime number',p)
if gcd(a,p)==1
b=powermod(a,1001,p);
fprintf('%d is remainder of %d^1001 division by %d',b,a,p)
else
fprintf('%d divides %d and hence remainder by Wilsons theorem is not possible',p,a)
end
else
fprintf('%d is composite number',p)
end
Output:
31 is a prime number
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 43 | 59
RV Institute of Technology & Management ®
Problem 2: Find the remainder when 11 divides 10! + 1 using MATLAB software.
Theory:
let 𝑝 = 11 is prime
By Wilson’s theorem, (𝑝 − 1)! ≡ −1(𝑚𝑜𝑑𝑝)
⇒ (11 − 1)! ≡ −1(𝑚𝑜𝑑11)
⇒ (10)! ≡ −1(𝑚𝑜𝑑11)
⇒ (10)! + 1 ≡ 0(𝑚𝑜𝑑11)
∴ 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 = 0
Program:
p=input('Enter an integer');
n=isprime(p);
if n == 1 % implies p is prime
fprintf('%d is a prime number',p)
k=factorial(abs(p-1))+1;
r1=rem(k,p);
fprintf('%d is the remainder when %d divides %d',r1,p,k)
else
fprintf('%d is a composite number',p)
end
Output:
11 is a prime number
Exercise:
1. Use MATLAB to find the remainder when 15! is divided by 17.
2. Use MATLAB to find the remainder when (26!) is divided by 29.
3. Use MATLAB to find the remainder when 250 is divided by 7.
4. Use MATLAB to find the remainder when 223 is divided by 47.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 44 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 8
AIM OF THE EXPERIMENT: Numerical solution of system of linear equations, test for
consistency and graphical method.
1 2 3 −2
Problem 1: Determine the rank of the matrix A = 2 −2 1 3 using MATLAB
3 0 4 1
software.
Theory: The rank is obtained by reducing the given matrix to echelon form as follows
1 2 3 −2 1 2 3 −2
𝐴 ∼ [0 −6 −5 7 ] ∼ [0 −6 −5 7 ] Rank = 2.
0 −6 −5 7 0 0 0 0
Program:
A= [1 2 3 -2; 2 -2 1 3; 3 0 4 1]
rank(A)
Output: ans = 2
0 1 2
1 2 1
Problem 2: Reduce the following matrix to reduced echelon form A = . using
2 3 5
3 5 6
MATLAB software.
1 2 1 1 2 1 1 2 1 1 0 0
0 1 2 0 1 2 0 1 2 0 1 0
𝐴∼[ ]∼[ ]∼[ ]∼[ ]
2 3 5 0 −1 3 0 0 1 0 0 1
3 5 6 0 −1 3 0 0 1 0 0 0
Program:
A= [0 1 2; 1 2 1; 2 3 5; 3 5 6]
rref(A)
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 45 | 59
RV Institute of Technology & Management ®
Output: ans =
1 0 0
0 1 0
0 0 1
0 0 0
Theory: The solution of system of equations can be obtained by using elementary row
operations.
1 1 1:9 3 0 0:6
[𝐴 : 𝐵] = [ 1 − 2 3 : 8 ] ~ [0 − 3 0 : −9] 𝑔𝑖𝑣𝑒𝑠 𝑥 = 2, 𝑦 = 3, 𝑧 = 4
2 1 − 1:3 0 0 1:4
Program:
A =[ 1 1 1; 1 -2 3; 2 1 -1]
b =[ 9; 8; 3]
x = A\b
Output: A =
1 1 1
1 -2 3
2 1 -1
b=
9
8
3
x=
2
3
4
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 46 | 59
RV Institute of Technology & Management ®
1 1 2: 1 1 0 0 : 0.5
[𝐴 : 𝐵] = [ 1 2 1:3 ]~[ 0 1 0 : 1.5 ] 𝑔𝑖𝑣𝑒𝑠 𝑥 = 0.5, 𝑦 = 1.5, 𝑧 = −0.5
2 1 − 1:3 0 0 1 : −0.5
Program:
A =[ 1 1 2; 1 2 1; 2 1 -1]
b =[ 1; 3; 3]
x = A\b
syms Y
X=-2:.2:2;y=x;
[X,Y]=meshgrid(x,y);
Z1=1/2*(1-X-Y)
Z2=3-X-2*Y
Z3=2*X+Y-3
surf(X,Y,Z1)
hold on
surf(x,y,Z2)
surf(x,y,Z3)
Output: A =
1 1 2
1 2 1
2 1 -1
b=
1
3
3
x=
0.5000
1.5000
-0.5000
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 47 | 59
RV Institute of Technology & Management ®
Program:
format compact
A=input('Enter the Co efficient Matrix A:');
m1=rank(A);
B=input('Enter the Matrix B (column matrix): ');
C=[A B];
m2=rank(C)
n=input('Number of unknowns');
if m1==m2
if m1==n
fprintf('System of equations is Consistent and has unique solution')
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 48 | 59
RV Institute of Technology & Management ®
else
fprintf('System of equations is consistent and has infinite solution')
end
else
fprintf('system of equations is inconsistent and has no solution')
end
Exercise
2 − 1 3
1. Find the rank of the matrix 1 4 − 2 using MATLAB software.
5 2 4
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 49 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 50 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 9
AIM OF THE EXPERIMENT: Solution of diagonally dominant system of linear equations
using Gauss-Siedel iteration
Problem 1: Write a MATLAB code to solve the following system of equations using Gauss-Seidel
method 6𝑥 + 15𝑦 + 2𝑧 = 72; 𝑥 + 𝑦 + 54𝑧 = 110; 27𝑥 + 6𝑦 − 𝑧 = 85.
Since the solutions in 3𝑟𝑑 and 4𝑡ℎ iterations agree up to3 places of decimals, the solution can be
taken as [𝑥, 𝑦, 𝑧] = [2.4255, 3.5730, 1.9259]
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 51 | 59
RV Institute of Technology & Management ®
Program:
Output:
The co efficient matrix is
27 6 -1 85
6 15 2 72
1 1 54 110
x= 2.4255
y= 3.5730
z= 1.9260
Exercise:
1. Use Gauss -Seidel method to write a MATLAB code to solve the system
10𝑥 + 𝑦 + 𝑧 = 12; 2𝑥 + 10𝑦 + 𝑧 = 13; 2𝑥 + 2𝑦 + 10𝑧 = 14
2. Write a MATLAB code to solve the following system of equations using Gauss -Seidel method
starting with (2, 2, −1) as initial approximation: 5𝑥1 − 𝑥2 + 𝑥3 = 10; 𝑥1 + 𝑥2 + 5𝑥3 =
−1; 2𝑥1 + 4𝑥2 = 12.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 52 | 59
RV Institute of Technology & Management ®
3. The upward velocity of a rocket is given at three different times in the following table. The
velocity data is approximated by a polynomial as 𝑣(𝑡) = 𝑎1 𝑡 2 + 𝑎2 𝑡 + 𝑎3,5 ≤ 𝑡 ≤ 12.
Find the values of 𝑎1, 𝑎2, 𝑎3. Assume the initial guess of the solution as [𝑎1 𝑎2 𝑎3]𝑇 =
[1 2 5]𝑇 and conduct 5 iterations.
Time, 𝑡 (𝑠) Velocity 𝑣 (𝑚/𝑠)
5 106.8
8 177.2
12 279.2
4. Write a MATLAB code to solve the following system of equations by Gauss -Seidel method
10𝑥 − 2𝑦 − 𝑧 − 𝑤 = 3
−2𝑥 + 10𝑦 − 𝑧 − 𝑢 = 15
−𝑥 − 𝑦 + 10𝑧 − 2𝑢 = 27
−𝑥 − 𝑦 − 2𝑧 + 10𝑢 = −9
5. Write a MATLAB code to solution to the following system of equations using the Gauss-Seidel
method.
12𝑥1 + 3𝑥2 − 5𝑥3 = 1
𝑥1 + 5𝑥2 + 3𝑥3 = 28
3𝑥1 + 7𝑥2 + 13𝑥3 = 76
𝑥1 1
𝑥
Use [ 2 ] = [0] as initial approximation.
𝑥3 1
6. Write a MATLAB code to solve the following system of equations using the Gauss-Seidel
method to obtain the final solution correct to three places of decimals
3𝑥 + 20𝑦 − 𝑧 = −18,
20𝑥 + 𝑦 − 2𝑧 = 17,
2𝑥 − 3𝑦 + 20𝑧 = 25.
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 53 | 59
RV Institute of Technology & Management ®
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 54 | 59
RV Institute of Technology & Management ®
EXPERIMENT NO. - 10
AIM OF THE EXPERIMENT: Compute Eigen value and Eigen vectors and find the largest
and smallest eigen value by Rayleigh power method
2 0 1
Problem 1: Compute eigenvalues and eigenvectors of matrix 𝐴 = [0 2 0] using MATLAB
1 0 2
software.
Solving above matrix for each eigen value, we get corresponding eigen vector 𝑋
0.7071
For 𝜆 = 1 corresponding eigen vector is 𝑋 = [ 0 ]
0.7071
0
For 𝜆 = 2 corresponding eigen vector is 𝑋 = [−1]
0
0.7071
For 𝜆 = 3 corresponding eigen vector is 𝑋 = [ 0 ]
0.7071
Program:
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 55 | 59
RV Institute of Technology & Management ®
Output:
Eigen values are
1
2
3
Eigen vectors are
0.7071 0 0.7071
0 -1.0000 0
-0.7071 0 0.7071
4 3 0
Problem 2: Write a MATLAB code to compute largest eigenvalue of matrix 𝐴 = [0 3 0] using
1 1 2
Rayleigh power method by taking initial vector as 𝑋0 = [1 1 1]𝑇
0.46857
1
4th iteration: A X3 =4.46284 [0.10371] = 𝜆4 𝛸4
0.46863
1
5th iteration: A X4 =4.31113 [0.07217] = 𝜆5 𝛸5
0.47342
1
6th iteration: A X5 =4.02433 [0.00605] = 𝜆6 𝛸6
0.4970
1
7 iteration: A X6 =4.01815 [ 0.0045 ] = 𝜆7 𝛸7
th
0.49775
Therefore, the largest eigenvalue is 4.02.
1
The corresponding eigenvector is [ 0.0045 ].
0.49775
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 56 | 59
RV Institute of Technology & Management ®
Program:
% matrix A by the power method using xnot as the starting vector and
% carrying out number of 'no' interactions.
MATB=[4 3 0;0 3 0;1 1 2]
xnot=[1;1;1];
x=xnot;
no=input('Enter number of iterations');
for k=1:no
w=MATB*xnot;
lambda=norm(w);
xnot=w/lambda;
end
disp('The Largest Eigen value by Power method is')
fprintf('lambda = %d, x = %g %g %g\n', lambda, xnot');
Output:
Exercise:
1. Using power method find an approximate value of eigenvalue and the corresponding
4 3 0
eigenvector of the matrix 𝐴 = [0 3 0].
1 1 2
8 −4
2. Find the eigenvalues and eigenvectors of 𝐴 = [ ] using MATLAB software
2 2
1 1 3
3. Find the eigenvalues and eigenvectors of 𝐴 = [1 5 1] using MATLAB software
3 1 1
4. Use MATLAB code to compute the numerically largest eigenvalue and the corresponding
2 0 1
eigen vector of the matrix 𝐴 = [0 2 0] Starting with [1,0,0]𝑇 as the initial approximation
1 0 2
to the corresponding eigenvector carry out 7 iterations.
5. Use the power method to find the dominant eigenvalue and eigenvector for the matrix𝛢 =
0 11 −5
[−2 17 −7 ] by MATLAB software.
−4 26 −10
I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 57 | 59