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

Final Matlab Manua

The document provides examples of using MATLAB to perform various tasks related to 2D and polar plots, derivatives, differential equations, linear congruences, linear systems of equations, and the Gauss-Seidel method. Example 1 demonstrates how to plot three sine curves with different line styles. Example 2 uses the linspace function to plot a cosine curve and customize the line color. Example 3 plots the Cardioid curve in polar coordinates. Subsequent examples calculate partial derivatives, Jacobian, radius of curvature, solve linear congruences and differential equations, check consistency of systems, and use Gauss-Seidel iteration to solve systems.

Uploaded by

arindam samanta
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)
71 views

Final Matlab Manua

The document provides examples of using MATLAB to perform various tasks related to 2D and polar plots, derivatives, differential equations, linear congruences, linear systems of equations, and the Gauss-Seidel method. Example 1 demonstrates how to plot three sine curves with different line styles. Example 2 uses the linspace function to plot a cosine curve and customize the line color. Example 3 plots the Cardioid curve in polar coordinates. Subsequent examples calculate partial derivatives, Jacobian, radius of curvature, solve linear congruences and differential equations, check consistency of systems, and use Gauss-Seidel iteration to solve systems.

Uploaded by

arindam samanta
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/ 23

BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

(Autonomous Institution Affiliated to VTU, Belagavi)

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

Example 1: Find the radius of curvature of the curve y = log(sec x).

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)

Example 2: Find the radius of curvature of the curve r = 2/(1 - cosϴ).

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:

Radius of Curvature of the given curve is given by


rc = ((4*sin(theta)^2)/(cos(theta) - 1)^4 + 4/(cos(theta) - 1)^2)^(3/2)/((8*sin(theta)^2)/(cos(theta) -
1)^4 - (2*((4*sin(theta)^2)/(cos(theta) - 1)^3 + (2*cos(theta))/(cos(theta) - 1)^2))/(cos(theta) - 1) +
4/(cos(theta) - 1)^2)

EXERCISE
Find the Radius of curvature of the following curves:

1) y=ax2+bx+c 2) r=a (1+cosϴ)


PROGRAM 3: Finding partial derivatives, Jacobian and plotting the graph

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:

JACOBIAN OF THE VECTOR FUNCTION:


The Jacobian of a vector function is a matrix of the partial derivatives of that function.

Example 3: Find the Jacobian of u = x(1-y), v = xy.


MATLAB code:

syms x y

u = x*(1-y);

v = x*y;

J=det(jacobian([u,v],[x,y]))

Output:

*******************************************************************

PROGRAM 4: Applications to Maxima and Minima of two variables

Example 1: Find the Maxima and Minima of f(x,y)= x^4+y^4 + x*y-1

MATLAB code:
Output:

Example 2: Find the Maxima and Minima of f(x,y)= x^2+y^2-1

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:

>> xMin = -2;


>> xMax = 2;
>> yMin = -2;
>> yMax = 2;
>> numPoints = 200;
>> xv = linspace(xMin, xMax, numPoints);
>> yv = linspace(yMin, yMax, numPoints);
>> [x, y] = meshgrid(xv, yv);
>> % f(x,y) = x^4 + y^4 - x^2 - y^2 + 1
>> fprintf('Creating function.\n');
>> f = x.^4 + y.^4 - x.^2 - y.^2 + 1;
>> fprintf('Creating surface plot.\n');
>> surf(x, y, f, 'LineStyle', 'none');
>> xlabel('x', 'FontSize', 20);
>> ylabel('y', 'FontSize', 20);
>> zlabel('f', 'FontSize', 20);
>> title('f(x,y) = x^4 + y^4 - x^2 - y^2 + 1', 'FontSize', 20);
>> colorbar;

Output
*****************************************************************************************

PROGRAM 5: Solution of first order differential equation and plotting the graphs

Example 1: Solve dy/dx = xy

MATLAB code:

Output

Syntax to plot the graph:

Example 2: Solve
MATLAB code:

Output

Syntax to plot the graph :

𝒅𝒚 −𝟐𝒙𝒚−𝟑𝒚
Example 3: Solve =
𝒅𝒙 𝟑𝒙𝟑

MATLAB code:

Output

Syntax to plot the graph:


Plot:

Program-6: GCD using Euclid’s Algorithm:

The Euclidean Algorithm is a technique for quickly finding the GCD of two integers.

Program to find GCD of two numbers

GCD using Euclid's Algorithm

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

% Displays the result


GCD = b

OutPut:

Program 7: Matlab code to solve linear congruence ax≡b(modm)

Example 1: Solve the Linear Congruence: 5x≡12(mod19)


Matlab code: (main)

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

Matlab code: gcd_func(a,m)

function[gcd]=gcd_func(a,b)
if a==0
gcd=b;
else
gcd=gcd_func(mod(b,a),a);
end

Matlab code: gcd_extended(a,m)

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)

Example 2: Solve the Linear Congruence: 5x≡3(mod11)

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)

Example 3: Solve the Linear Congruence: 5x≡2(mod15)

Output:

Enter the value of a


5
Enter the value of b
2
Enter the value of m
15

solution does not exist


PROGRAM 8: Numerical solution of system of linear equations, test for consistency
and graphical representation

(i) C h ec k t h e c on s i s t en c y a nd he nc e Solve the system of Linear Equations


2x + y + z = 2
−x + y − z = 3
x + 2 y + 3z = −10

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:

PROGRAM 9: Solution of system of linear equations using Gauss-Seidel iteration

Example 1: Solve by Gauss Seidel method


10𝑥 + 3𝑦 + 𝑧 = 19
3𝑥 + 10𝑦 + 2𝑧 = 29
𝑥 + 2𝑦 + 10𝑧 = 35

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.

Example 1: Compute Eigenvalues and Eigenvectors of a matrix

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:

Syntax: [V,D] = eig(A)


• It returns the diagonal matrix D having diagonals as eigenvalues.
• It also returns the matrix of right vectors as V.
• Normal eigenvectors are termed as right eigenvectors.
• V is a collection of N eigenvectors of each N*1 size (A is N*N size) that satisfies A*V = V*D

Example 2: Create a matrix and find eigenvalues and eigenvectors of a matrix.

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

*************************************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy