Professional Documents
Culture Documents
DEPARTMENT OF MATHEMATICS
I – SEMESTER
(2022-2023)
MATHEMATICS - 1
22MATE11/22MATC11/22MATM11
MATLAB MANUAL
PROGRAM 1: 2D plots for Cartesian and polar curves
Example 1: Plot three sine curves with a small phase shift between each line. Use the default line
style for the first line. Specify a dashed line style for the second line and a dotted line style for the
third line.
MATLAB code:
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
figure
plot(x,y1,x,y2,'--',x,y3,':')
Output
Example 2: Use the linspace function to define x as a vector of 150 values between 0 and 10.
Define y as cosine values of x. Create a 2-D line plot of the cosine curve. Change the line color to a
shade of blue- green using an RGB color value. Add a title and axis labels to the graph using the title,
xlabel and ylabel commands.
MATLAB code:
x = linspace(0,10,150);
y = cos(5*x);
figure
plot(x,y,'Color',[0,0.7,0.9])
title('2-D Line Plot')
xlabel('x')
ylabel('cos(5x)')
Output
240
Example 3: Plot the graph of Cardiod given by r = 3(1 + cos θ).
MATLAB code:
theta = 0:0.01:2*pi;
r = 3*(1+cos(theta));
polarplot(theta,r)
Output
*******************************************************************************
PROGRAM 2: Finding angle between polar curves, curvature and radius of curvature of a given
curve
MATLAB code:
syms x y rc
y = input('Enter the value of the function y is');
fprintf('Radius of Curvature of the given curve is',rc)
rc = ((1+(diff(y,x))^2)^(3/2))/diff(y,x,2)
Output:
Enter the value of the function y is
log(sec(x))
Radius of Curvature of the given curve is
rc = (sin(x)^2/cos(x)^2 + 1)^(1/2)
MATLAB code:
syms r r1 r2 rc theta
r = 2/(1-cos(theta));
r1 = diff(r,theta);
r2 = diff(r,theta,2);
fprintf('Radius of Curvature of the given curve is given by',rc)
rc = ((r^2+(r1)^2)^(3/2))/(r^2+2*(r1)^2-r*(r2))
Output:
EXERCISE
Find the Radius of curvature of the following curves:
PARTIAL DERIVATIVES:
For multivariable expressions, you can specify the differentiation variable. If you do not
specify any variable, MATLAB chooses a default variable by the proximity to the letter x:
Example 1: Find the first order partial derivative of f = sin2x + cos2y w.r.t. x and y.
MATLAB code:
Output:
SECOND PARTIAL AND MIXED DERIVATIVES :
Example 2: Find the second order partial derivative of f = sin2x + cos2y w.r.t. y.
To find a second derivative of the symbolic expression f with respect to a variable x, enter:
MATLAB code:
Output:
syms x y
u = x*(1-y);
v = x*y;
J=det(jacobian([u,v],[x,y]))
Output:
*******************************************************************
MATLAB code:
Output:
MATLAB code:
Output
Example 3: Find the Maxima and Minima of f(x,y)= x^4 + y^4 - x^2 - y^2 + 1
MATLAB code:
Output
*****************************************************************************************
PROGRAM 5: Solution of first order differential equation and plotting the graphs
MATLAB code:
Output
Example 2: Solve
MATLAB code:
Output
𝒅𝒚 −𝟐𝒙𝒚−𝟑𝒚
Example 3: Solve =
𝒅𝒙 𝟑𝒙𝟑
MATLAB code:
Output
The Euclidean Algorithm is a technique for quickly finding the GCD of two integers.
clear; clc; % Clears screen and deletes all the variables in the
workspace
% Asks the user for input and takes only positive numbers into
account
a = input('First number: ');
b = input('Second number: ');
a = abs(a);
b = abs(b);
r = a - b*floor(a/b);
% Repeats the operation until updates of a equal updates of b
while r ~= 0
a = b;
b = r;
r = a - b*floor(a/b);
end
OutPut:
syms a b m
a= input('Enter the value of a');
b= input('Enter the value of b');
m= input('Enter the value of m');
d = gcd_func(a,m);
if mod(b,d)==0
fprintf('d divides b')
fprintf('\n Thus, %d incongruent solutions for x exist\n', d)
[u0,v0]=gcd_extended(a,m);
x0=u0*b/d;
y0=v0*b/d;
fprintf('x0= %d y0= %d\n', x0, y0)
for t=0: d-1
ans=x0+floor(m/d)*t
fprintf('x==%d(mod%d)\n', ans,m)
end
else
fprintf(' solution does not exist')
end
function[gcd]=gcd_func(a,b)
if a==0
gcd=b;
else
gcd=gcd_func(mod(b,a),a);
end
function[x,y]= gcd_extended(a,b)
if (a==0)
x=0;
y=1;
else
[x1,y1]=gcd_extended(mod(b,a),a);
x=y1-floor(b/a)*x1;
y=x1;
end
Output:
Enter the value of a
5
Enter the value of b
12
Enter the value of m
19
d divides b
Thus, 1 incongruent solutions for x exist
x0= 48 y0= -12
ans = 48
x ≡ 48(mod19)
Output:
Enter the value of a
5
Enter the value of b
3
Enter the value of m
11
d divides b
Thus, 1 incongruent solutions for x exist
x0= -6 y0= 3
ans = -6
x ≡ -6(mod11)
Output:
A = [2 1 1 ; -1 1 -1; 1 2 3]
B = [2; 3; -10]
AB= [A B]
if rank(A) == rank(AB)
fprintf('The system is Consistence (Solution Exists)\n')
fprintf('Solution of the system is')
X = linsolve(A,B)
else
fprintf ('The system is Inconsistence(Solution doesn’t exists)')
end
Output:
(ii) Check for consistency and solve the system of Linear Equations
2x + 4 y + 6z = 7
3x − 2 y + z = 2
x + 2 y + 3z = 5
A = [2 4 6; 3 -2 1; 1 2 3]
B = [7; 2; 5]
AB= [A B]
if rank(A) == rank(AB)
fprintf('The system is Consistence (Solution Exists)\n')
fprintf('Solution of the system is')
X = linsolve(A,B)
else
fprintf ('The system is Inconsistence(Solution doesn’t exists)')
end
Output:
Example 3: Check for consistency and solve the system of Linear Equations. Also plot the graph
2x − y = 7
x+ y =2
syms x y
eq1 = 2*x-y==7;
eq2 = x + y ==2;
[A,B] = equationsToMatrix([eq1, eq2], [x, y])
fprintf('Solution of the system is')
X = linsolve(A,B)
fimplicit(eq1);
hold on
fimplicit(eq2);
hold off
output:
A= [10 3 1; 3 10 2; 1 2 10]
B= [19;29;35]
P= [0;0;0] % initial guess vector
iterations= 6
N=length(B);
X=zeros(N,1);
for j=1:iterations
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
P(i)=X(i);
end
fprintf('iteration no %d\n',j)
X
end
Example-2: A: 5x +2y +z=12
x+ 4y+ 2z=15;
x+ 2y+ 5z=0
Ans:1.0125 4.5016 -2.0031
Example-3: 28x+ 4y -z=32
2x+ 17y+ 4z=35
x+ 3y+ 10z=24
Ans: 0.9983 1.5070 1.8486
PROGRAM 10: Compute eigenvalues and eigenvectors and find the largest and smallest
eigenvalue by Rayleigh power method.
Syntax: e = eig(A)
It returns the vector of eigenvalues of square matrix A.
Example 1: Create a matrix and find eigenvalue of a matrix.
MATLAB code:
% Square matrix of size 3*3
A = [0 1 2; 1 0 -1;2 -1 0];
disp("Matrix");
disp(A);
% Eigenvalues of matrix A
e = eig(A);
disp("Eigenvalues");
disp(e);
Output:
MATLAB code:
% Square matrix of size 3*3
A = [8 -6 2; -6 7 -4; 2 -4 3];
disp("Matrix");
disp(A);
% Eigenvalues and eigenvectors of matrix A
[V,D] = eig(A);
disp("Diagonal matrix of Eigenvalues");
disp(D);
disp("Eigenvectors")
disp(V);
Ouput:
Example-3: Compute dominant eigenvalue and corresponding eigen vector by power method
A=input('Enter a matrix A: ');
v=input('Enter a initial guess');
n=input('Enter number of iterations');
v0=v;
for i=1:n
v=A*v0;
M=max(v);
v=v/M;
v0=v;
fprintf('iteration number # %d\n',i)
fprintf('current eigen value is %.4f\n', M)
fprintf('current eigen value is ')
v
end
Output:
Enter a matrix A:
[1 2 3;3 2 1;2 4 5]
Enter a initial guess
[1;0;0]
Enter number of iterations
5
iteration number # 1
current eigen value is 3.0000
current eigen value is
v =
0.3333
1.0000
0.6667
iteration number # 2
current eigen value is 8.0000
current eigen value is
v =
0.5417
0.4583
1.0000
iteration number # 3
current eigen value is 7.9167
current eigen value is
v =
0.5632
0.4474
1.0000
iteration number # 4
current eigen value is 7.9158
current eigen value is
v =
0.5632
0.4528
1.0000
iteration number # 5
current eigen value is 7.9375
current eigen value is
v =
0.5630
0.4529
1.0000
*************************************************