0% found this document useful (0 votes)
117 views

MATLAB Manual CS and IS

Matlab

Uploaded by

vipin10thj.27828
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)
117 views

MATLAB Manual CS and IS

Matlab

Uploaded by

vipin10thj.27828
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/ 59

Rashtreeya Sikshana Samithi Trust

RV Institute of Technology and Management


(Institution Affiliated to VTU, Belagavi)

JP Nagar, Bengaluru - 560076

Department of Mathematics

Course Name: Mathematics-1 for Computer Science and


Engineering Stream
Course Code: 22MATS11

MATLAB MANUAL

I Semester
2022 Scheme

Prepared By:

Dr. Gangadharaiah YH Dr. Sivasankar S Dr. Naghabhushana P Dr. Sushma M P


Professor Assistant Professor Assistant Professor Assistant Professor
RV Institute of Technology & Management ®

Contents
TABLE OF CONTENTS

RULES & REGULATIONS ................................................................................... 3

FORMAT FOR RECORD WRITING ..................................................................... 4

MARK DISTRIBUTION ....................................................................................... 5

BASIC FEATURES, TOOL BOX AND DIFFERENT FUNCTIONS ................. 6

LIST OF EXPERIMENTS ...................................................................................... 15

EXPERIMENT NO. -1 .................................................................................... 17

EXPERIMENT NO. -2 .................................................................................... 25

EXPERIMENT NO. - 3 ...................................................................................... 27

EXPERIMENT NO. - 4 ...................................................................................... 31

EXPERIMENT NO. - 5 ................................................................................... 35

EXPERIMENT NO. - 6 ................................................................................... 39

EXPERIMENT NO. - 7 ...................................................................................... 43

EXPERIMENT NO. - 8 ...................................................................................... 45

EXPERIMENT NO. - 9 ................................................................................... 51

EXPERIMENT NO. - 10 ................................................................................... 55

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 ®

RULES & REGULATIONS

1. Don’t be late to the lab.

2. Put your signature on attendance sheet first, submit the record and then go to your place

correspondence to experiment kit.

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.

6. You should put on your own identity card.

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

present there else your record will not be considered.

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 ®

FORMAT FOR RECORD WRITING

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

circuit/block diagram, observation and conclusion.

4. Attached the printout sheet to the left side of the paper.

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

with pencil only.

7. Bad hand writing will affect the record mark.

8. Incomplete record will lead towards negative marking

9. Data on lab manual should match with fair record.

10. Don’t submit loose sheets without record at any condition.

FORMAT

1. Aim of the experiment

2. Theory

3. Circuit simulation diagram /block diagram (if any)

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

2. Open new M-file

3. Type the program

4. Save in current directory

5. Compile and Run the program

6. For the output see command window/Figure window

MARK DISTRIBUTION

Sl. No. Parts Marks


1 Record 15
2 Test 10
TOTAL 25

I hereby Mr./Ms………………………………………………..with USN… ......................... have

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.

Date: Signature of the student

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 ®

Different windows of MATLAB desktop:


MATLAB has the following four basic window normally and other windows are also available:
1. The Command Window
2. The editor window
3. The Workspace
4. The graphic window.
The desktop has the following windows:
Command Window: This is the main area where commands can be entered at the command
line. It is indicated by the command prompt (>>).
Editor window: Here we can write, edit ,create and save our own Program in file with “m .files”
extension. we can used any text editor to carryout these tasks.
Workspace: The workspace shows all the variables created and/or imported from files.
Graphic Window: Graphic window is also known as figure window and it is the display of the
figure result of the Program . the user can create as many figure window of the system memory
will allows.
Command History: This panel shows or rerun commands that are entered at the command line.
Current Directory: Show the files in Current Directory
Current Folder: This panel allows you to access the project folders and files.
Different Toolbox in MATLAB:
Tools box are the software device of MATLAB. It has number of tool boxes working in different
areas of engineering. They are as follows:
1. Communications System Toolbox.
2. SimPower System Toolbox.
3. Control System Toolbox.
4. DSP System Toolbox
5. Simspace Toolbox
6. Robust Control Toolbox
7. Fuzzy Logic Toolbox
8. Instrument Control Toolbox

I-Semester: MATLAB Manual-Mathematics-1 for Computer Science and Engineering Stream (22MATS11)
P a g e 7 | 59
RV Institute of Technology & Management ®

DIFFERENT FUNCTION IN MATLAB:


Algebraic function:
Matrix function
1.det- determinant of a square matrix.
2.rank-rank of a matrix
3.trace-sum of diagonal element
linear equation function
1. inv- inverse of a matrix
2. linsolve-solve linear system equation
eigen value and singular value function
1.eig- eigenvalues and eigenvectors
2.eigs- largest eigenvalues and eigenvectors
3.poly- characteristic polynomial
4.sqrtm-matrix square root

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 ®

MATRIX OPERATIONS IN MATLAB:


A matrix is a two-dimensional array of numbers. Matrices can be defined by separating the
elements of a row with blank space or comma and using a semicolon to terminate each row. The
list of elements should be surrounded by square brackets []. Parentheses used to denote a function
argument list).
Matrix Functions:
are used to access elements and sub arrays (they are also
eye - identity matrix zeros - matrix of zeros ones - matrix of ones
diag - extract diagonal of a matrix or create diagonal matrices triu - upper triangular part of a matrix
tril - lower triangular part of a matrix rand - randomly generated matrix Size-size of a matrix
det- determinant of a square matrix. inv- inverse of a matrix
rank-rank of a matrix

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 OPERATIONS IN MATLAB:

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 ®

Specifying colors, line styles and markers used in plotting


Symbol Color Symbol Line Style Symbol Marker
k Black - Solid + Plus sign
r Red -- Dashed o Circle
b Blue : Dotted ⁄ Asterisk
g Green -. Dash-dot : Point
c Cyan none No line £ Cross
m Magenta s Square
y Yellow d Diamond

BASIC CALCULUS OPERATIONS IN MATLAB:


_ diff(F,var) differentiates F with respect to the variable var.
_ diff(F,n) computes the nth derivative of F with respect to the variable determined by symvar.
_ diff(F,var,n) computes the nth derivative of F with respect to the variable var.
_ diff(F,var1,...varN) differentiates F with respect to the variables var1,...,varN.
If you do not specify it, int uses the default variable determined by symvar. If expr is a constant,
then the default variable is x.
Note: int(expr,var,[a,b]), int(expr,var,[a b]), and int(expr,var,[a;b]) are
equivalent to int(expr,var,a,b).

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

EXP. LIST OF EXPERIMENT


NO.
1. 2D plots for Cartesian and polar curves

2. Finding curvature and radius of curvature of a given curve

3. Finding partial derivatives, Jacobian

4. Applications to Maxima and Minima of two variables

5. Solution of first order differential equation and plotting the graphs

6. Program to compute area, Volume and centre of gravity

7. Evaluation of improper integrals

8. Numerical solution of system of linear equations, test for consistency and graphical

representation

9. Solution of system of linear equations using Gauss-Seidel iteration

10. Compute eigenvalues and eigenvectors and find the largest and smallest eigenvalue by

Rayleigh power method.

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.

Theory: The sinusoidal waves are 𝑦1 = 𝑠𝑖𝑛 𝑥 & 𝑦2 = 𝑐𝑜𝑠 𝑥.

Program:
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2)

Output:

Problem 2: Plot the three leaved roses using MATLAB software.

Theory: Mathematical Description of n-Leaved Rose


n-leaved rose (also referred to as p-leaved rose) can be expressed by polar equations that are
given as follows:
r = cos ( n ) or , r = sin ( n )

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.

The equation of three leaved rose is 𝑟 = 2 𝑠𝑖𝑛 3𝜃

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.

Theory: The equation of Strophoid is 𝑦 2 (2 − 𝑥) = 𝑥 2 (2 + 𝑥) .

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 ®

Problem 5: Plot the graph of ellipsoid using MATLAB software.

(𝑥−𝑎)2 (𝑦−𝑏)2 (𝑧−𝑐)2


Theory: The general equation of the ellipsoid is + + = 1 with centre (a,b,c).
𝐴2 𝐵2 𝐶2
𝑥2 𝑦2 𝑧2
The ellipsoid with a center at (0, 0, 0) is 𝑎2 + 𝑏2 + 𝑐 2 = 1 and semiaxis lengths (1.5, 1.5, 3).

Program:
syms y x z
[X,Y,Z] = ellipsoid(0,0,0,1.5,1.5,3);
surf(X,Y,Z);

Output:

Problem 6: Plot a Step signal using MATLAB Software


1 t0
Theory: The Step signal is defined as u ( t ) = 
0 t0
Program:
t = (-100:1:100)'
unitstep =t>=0
plot(t,[unitstep])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Step Signal')

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:

Problem 7: Plot a Impulse signal using MATLAB Software


 t = 0
Theory: The Impulse signal is defined as  ( t ) = 
0 else where

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 ®

Problem 8: Plot a Ramp signal using MATLAB Software


t t 0
Theory: The Ramp signal is defined as r ( t ) = 
0 t 0
Program:
t = (-1:0.01:1)';
ramp = t.*unitstep;
plot(t,[ ramp ])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Ramp Signal')

Output:

Problem 9: Plot a Square wave using MATLAB Software.


 1 0t 
Theory: The Square wave is defined as f ( t ) = 
−1   t  2
Program:
t = (-2:0.01:2)';
sqwave = square(2*pi*t);
plot(t,sqwave)
xlabel('Time (sec)')
ylabel('Amplitude')
title('Square Periodic Wave')

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:

1. Plot the curve 𝑦 2 (2 − 𝑥) = 𝑥 3 (Cissoid) using MATLAB software.

2. Trace the curve- 𝑥𝑦 2 = 4(2 − 𝑥) (Witch of Agnesi) using MATLAB software.

3. Trace the curve r = a (1 - sin) (Cardioid) using MATLAB software.

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.

Problem 1: Compute the curvature and radius of curvature of the curve 𝑦 = 2𝑥 2 + 3𝑥 + 5 at


point (1,2) using MATLAB software.

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

Output: rc=(125 ∗ 2^(1/2))/2


curvature = 2^(1/2)/125

Problem 2: Compute the curvature and radius of curvature of the curve


y = (𝑎1/2 − 𝑥1/2 )2 at x=a/4 using MATLAB software.

Theory: y = (𝑎1/2 − 𝑥1/2 )2


𝑎
𝑦 ′ = −1 𝑎𝑡 𝑥 =
4
𝑎
𝑦′′ = 4𝑎 𝑎𝑡 𝑥 =
4
3
[1 + 𝑦′2 ]2 𝑎 √2
𝜌= = , curvature =
𝑦′′ √2 𝑎

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

Output: rc=(2^(1/2) ∗ 𝑎)/2


curvature = 2^(1/2)/a

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.

Theory: 𝑓(𝑥) = 𝑦𝑒 𝑥 + 𝑥 2 𝑦 − log(𝑥𝑦)


𝜕𝑓 1 𝜕𝑓 1
= 2xy + yexp(x) − ; = exp(x) + x 2 −
𝜕𝑥 x 𝜕𝑦 y
𝜕 2𝑓 𝜕 2𝑓 1
= 2x + exp(x); 2
= 2y + yexp(x) + 2
𝜕𝑥𝜕𝑦 𝜕𝑥 x
Program:
syms y x
f=y*exp(x)+x^2*y-log(x*y);
fx=diff(f,x); fy=diff(f,y);
fxy=diff(f,x,y); fxx=diff(f,x,x);

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.

Theory: 𝑓(𝑥, 𝑦) = 𝑒 𝑥 (𝑥𝑐𝑜𝑠𝑦 − 𝑦𝑠𝑖𝑛𝑦)


𝜕𝑓
= 𝑒 2 𝑐𝑜𝑠3 + 𝑒 2 (2𝑐𝑜𝑠3 − 3𝑠𝑖𝑛3) at x = 2, y = 3
𝜕𝑥

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:

ans = cos(3)*exp(2) + exp(2)*(2*cos(3) - 3*sin(3))

𝜕[𝑥 𝑦 𝑧]
Problem 3: Compute 𝐽 = 𝜕[𝑢,𝑣,𝑤] for 𝑥 = 𝑢 − 𝑢𝑣; 𝑦 = 𝑢𝑣(1 − 𝑤); 𝑧 = 𝑢𝑣𝑤 using MATLAB
software.

Theory: 𝑥 = 𝑢 − 𝑢𝑣; 𝑦 = 𝑢𝑣(1 − 𝑤); 𝑧 = 𝑢𝑣𝑤

𝜕[𝑥 𝑦 𝑧] 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.

Theory: 𝑥 = 𝑟 𝑠𝑖𝑛𝜃 𝑐𝑜𝑠Φ; 𝑦 = 𝑟 𝑠𝑖𝑛𝜃 𝑠𝑖𝑛Φ; 𝑧 = 𝑟 𝑐𝑜𝑠𝜃


𝜕[𝑥 𝑦 𝑧] 1−𝑣 −𝑣 0
𝐽= = |𝑣(1 − 𝑤) 𝑢(1 − 𝑤) −𝑢𝑣 | = 𝑟 2 𝑠𝑖𝑛𝜃
𝜕[𝑟, 𝜃, Φ]
𝑣𝑤 𝑢𝑤 𝑢𝑣

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

Problem 1: Compute the extreme value of the function 𝑓 (𝑥, 𝑦) = 𝑥3 + 𝑦3 − 3𝑥 − 12𝑦 + 20


at (1,2) using MATLAB software.
𝜕𝑓
Theory: Here 𝑝 = 𝜕𝑥 = 3𝑥 2 − 3, 𝑞 = 3𝑦 2 − 12

𝜕 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 ®

Problem 2: Compute the extreme values of the function 𝑓(𝑥, 𝑦) = 𝑥3 + 3𝑥 𝑦3 − 15 𝑥2 − 15𝑦2 +


72𝑥 at (4,0) using MATLAB software.

Theory: Here 𝑝 = 3𝑥 2 + 3𝑦 3 − 30𝑥 + 72, 𝑞 = 6𝑥𝑦 − 30𝑦


𝜕 2𝑓
𝑟 = 6x-30 , 𝑠 = 6𝑦, t= 2 = 6𝑥 − 30 𝑎𝑛𝑑 𝑟𝑡 − 𝑠 2 = 36
𝜕𝑦
2
At the point (4,0) , 𝑟 < 0 and 𝑟𝑡 − 𝑠 > 0.
It follows that 𝑓(𝑥, 𝑦) is maximum at the point (4,0). The maximum value is 112.

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.

Theory: Here 𝑝 = 3𝑥 2 𝑦 2 − 4𝑥 3 𝑦 2 − 30𝑥 2 𝑦 3 , 𝑞 = 2𝑥 3 𝑦 − 2𝑥 4 𝑦 − 3𝑥 3 𝑦 2


1
𝑟 = 6𝑥𝑦 2 (1-2x-y) , 𝑠 = 𝑥 2 𝑦(6 − 8𝑥 − 9𝑦), t=2𝑥 3 (1 − 𝑥 − 3𝑦)𝑎𝑛𝑑 𝑟𝑡 − 𝑠 2 =
144
1 1
At the point (2 , 3) , 𝑟 < 0 and 𝑟𝑡 − 𝑠 2 > 0.
1 1 1
It follows that 𝑓(𝑥, 𝑦) is maximum at the point (2 , 3). The maximum value is 144.

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)

tspan=[0 2]; % specify time interval


x0=0; % specify x0
[t,x]=ode45(@(t,x)x+t,tspan,x0); % now execute ode45
disp([t,x])
plot(t,x) % plot t verses x

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

Theory: Given acceleration is a = −0.003v2 − 10

dv
The DE for parachute model is = −0.003v2 − 10 with v ( 0) = 25
dt

Solving above equations by separation of variables, we get solution


−100 √3 √3
𝑣(𝑡) = 𝑡𝑎𝑛 [ 30 (3𝑡 − 10√3 𝑡𝑎𝑛−1 ( 4 ))]
√3

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

Theory: The algorithm of Euclid’s theorem is given by

Algorithm produce the equations


12378 = 4. 3054 + 162
3054 = 18. 162 + 138
162 = 1. 138 + 24
138 = 5. 24 + 18
24 = 1. 18 + 6
18 = 3. 6 + 0
last nonzero remainder appearing in these equations, namely, the integer 6, is the greatest
common divisor of 12378 and 3054:
6 = GCD(12378,3054)

Program:

a = input('First number: ');


b = input('Second number: ');
% assigns a,b to x, y for displaying Output
x=a;
y=b;
a = abs(a);
b = abs(b);
r = a - b*floor(a/b);
% Repeats the operation until equal updates of b
while r ~= 0
a = b;
b = r;
r = a - b*floor(a/b);
end
% Displays the result

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.

Theory: The Euclid’s algorithm for given numbers is as follows:


10672 = 4147 × 2 + 2378
4147 = 2378 × 1 + 1769
2378 = 1769 × 1 + 609
609 = 551 × 1 + 58
551 = 58 × 9 + 29
58 = 29 × 2
Hence 𝐺𝐶𝐷(4147, 10672) = 29.

Program:

a = input('First number: ');


b = input('Second number: ');
% assigns a,b to x, y for displaying Output
x=a;
y=b;
a = abs(a);
b = abs(b);
r = a - b*floor(a/b);
% Repeats the operation until equal updates of b
while r ~= 0
a = b;
b = r;
r = a - b*floor(a/b);
end
% Displays the result
GCD = b;
fprintf('The GCD(%2d, %2d) is (%2d)',x,y,GCD)

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

Problem 1: Use MATLAB to find the remainder when 721001 is divided by 31

Theory: Wilson’s theorem states that 𝑝 𝑖𝑠 𝑝𝑟𝑖𝑚𝑒 𝑖𝑓𝑓 (𝑝 − 1)! ≡ −1(𝑚𝑜𝑑𝑝)


Let 𝑝 = 31, Consider 72 ≡ 10(𝑚𝑜𝑑 81) ⟹ 721001 ≡ 101001 (𝑚𝑜𝑑31) -----(1)
Take a=10 and GCD(a, p)=(10,31) =1
By Fermat’s Little Theorem: 𝑎𝑝−1 ≡ 1(𝑚𝑜𝑑𝑝)
1031−1 ≡ 1(𝑚𝑜𝑑31)
(1030 )33 ≡ 133 (𝑚𝑜𝑑 31)
(10990 ) ≡ 133 (𝑚𝑜𝑑 31)------------(2)
(101001 ) = (10990 ∗ 108 ∗ 102 ∗ 10)
We have 10 ≡ 169(𝑚𝑜𝑑 31), 102 ≡ 7(𝑚𝑜𝑑 31)
8

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

19 is a remainder of 72^1001 division by 31

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

0 is the remainder when 11 divides 3628801

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.

Theory: The reduced echelon form is

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

Problem 3: Write a MATLAB code to solve the system of equations


𝑥 + 𝑦 + 𝑧 = 9; 𝑥 − 2𝑦 + 3𝑧 = 8; 2𝑥 + 1𝑦 − 𝑧 = 3

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 ®

Problem 4: Write a MATLAB code to solve the following set of equations


x + y +2 z = 1; x + 2y +z = 3; 2x + y - z = 3 and plot the surfaces.

Theory: The Augmented matrix is

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 ®

Problem 5: Write a MATLAB code to test for consistency


𝑥 + 2𝑦 + 2𝑧 = 1, 2𝑥 + 𝑦 + 𝑧 = 2, 3𝑥 + 2𝑦 + 2𝑧 = 3.

Theory: The rank of given augmented matrix is obtained by


1 2 2 ∶1
[𝐴: 𝐵] = [2 1 1 ∶ 2 ]
3 2 2 ∶3
1 2 2 ∶1
[𝐴: 𝐵] = [0 −3 −3 ∶ 0 ]
0 −4 −4 ∶ 0
1 2 2 ∶1
[𝐴: 𝐵] = [0 −3 −3 ∶ 0]
0 0 0 ∶0
𝜌(𝐴) = 𝜌([A:B]) = 2 < 3, number of unknowns. The system of equations is consistent and has
infinite solution

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

Output: Enter the Co efficient Matrix A:


[1 2 2;2 1 1;3 2 2]
Enter the Matrix B (column matrix):
[1; 2; 3]
m2 = 2
Number of unknowns 3
System of equations is consistent and has infinite solution

Exercise

2 − 1 3
1. Find the rank of the matrix 1 4 − 2 using MATLAB software.
 
5 2 4

2. Write a MATLAB code to solve the system of equations


𝑥 + 2𝑦 + 3𝑧 = 1; 3𝑥 + 4𝑦 + 4𝑧 = 4; 7𝑥 + 10𝑦 + 12𝑧 = 2.
3. Write a MATLAB code to test for consistency
𝑥 + 𝑦 + 𝑧 = 9; 2𝑥 − 3𝑦 + 4𝑧 = 13; 3𝑥 + 4𝑦 + 5𝑧 = 40.

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.

Theory: The set of equation for iteration is given by


85−6𝑦+𝑧
𝑥= . (1)
27
72−6𝑥−2𝑧
𝑦= . (2)
15
110−𝑥−𝑦
𝑧= . (3)
54

Let us choose [𝑥, 𝑦, 𝑧] = [0,0,0] as the initial solution.


1
First iteration: 𝑥 (1) = 27 [85 − 0 + 0] = 3.148
1
𝑦 (1) = 15 [72 − 6(3.1481) − 0] = 3.5407
1
𝑧 (1) = 54 [110 − 3.1481 − 3.5407] = 1.9132.
𝟏
Second iteration: 𝒙(𝟐) = 𝟐𝟕 [𝟖𝟓 − 𝟔(𝟑. 𝟓𝟒𝟎𝟕) + 𝟏. 𝟗𝟏𝟑𝟐] = 𝟐. 𝟒𝟑𝟐𝟐
1
𝑦 (2) = 15 [72 − 6(2.4322) − 2(1.9132] = 3.5720
1
𝑧 (2) = 54 [110 − 2.4322 − 3.5720] = 1.9258.
1
Third iteration: 𝑥 (3) = 27 [85 − 6(3.5720) + 1.9258] = 2.4257
1
𝑦 (3) = 15 [72 − 6(2.4257) − 2(1.9258)] = 3.5729
1
𝑧 (3) = 54 [110 − 2.4257 − 3.5729] = 1.9259.

Therefore [𝑥, 𝑦, 𝑧] = [2.4257,3.5729,1.9259].


1
Fourth iteration: 𝑥 (4) = 27 [85 − 6(3.5729) + 1.9259] = 2.4255
1
𝑦 (4) = 15 [72 − 6(2.4255) − 2(1.9259)] = 3.5730
1
𝑧 (4) = 54 [110 − 2.4255 − 3.5730] = 1.9259.

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:

A=input('Enter the Coefficient Matrix A: ');


B=input('Enter the solution Matrix b (column matrix): ');
C=[A B];
n=input('Enter the number of iteration: ');
X=input('Enter the initial guess (Column matrix): ');
disp('The co efficient matrix is')
disp(C)
x(1)=X(1,1);
y(1)=X(2,1);
z(1)=X(3,1);
for i=1:n %Iterative step
x(i+1)=(1/C(1,1))*(C(1,4)-C(1,2)*y(i)-C(1,3)*z(i));
y(i+1)=(1/C(2,2))*(C(2,4)-C(2,1)*x(i+1)-C(2,3)*z(i));
z(i+1)=(1/C(3,3))*(C(3,4)-C(3,1)*x(i+1)-C(3,2)*y(i+1));
end
disp('x= ')
disp(x(i+1))
disp('y= ')
disp(y(i+1))
disp('z= ')
disp(z(i+1))

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.

Theory: For a given matrix 𝐴, the characteristic equation is given by,


2−𝜆 0 1
|𝐴 − 𝜆𝐼| = | 0 2−𝜆 0 |=0
1 0 2−𝜆
On solving above determinant, we obtain cubic equation: 𝜆3 − 6𝜆2 + 11𝜆 − 6 = 0
And roots are 𝜆 = 1,2,3 are required Eigen values. To solve Eigen vector, consider the matrix
2−𝜆 0 1 𝑥 0 𝑥
𝐴𝑋 = 𝜆𝑋 ⟹ [ 0 2−𝜆 0 ] [𝑦] = [0] where 𝑋 = [𝑦]
1 0 2−𝜆 𝑧 0 𝑧

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:

A=[2 0 1;0 2 0;1 0 2]


[V,D]=eig(A);
disp('Eigen values are')
disp(diag(D))
disp('Eigen vectors are')
disp(V)

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]𝑇

Let 𝛸0 = [1 1 1]𝑇 be the initial approximation.


7 1
st
1 iteration: 𝛢𝛸0 = [3] = 7 [ 3/7] = 𝜆1 𝛸1
4 4/7
1
2nd iteration: 𝛢𝛸1 = 5.28571 [0.24324] = 𝜆2 𝛸2
0.48649
1
3 iteration: A X2 =4.72972 [0.15428] = 𝜆3 𝛸3
rd

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:

Enter number if iterations 7


The Largest Eigen value by Power method is lambda = 4.159697e+00,
x = 0.899744 0.0333656 0.435142

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

You might also like