24MATEE11 Matlab 28 11 2024
24MATEE11 Matlab 28 11 2024
24MATEE11 Matlab 28 11 2024
Introduction to MATLAB
Department of Mathematics 1
Department of Mathematics 2
What is MATLAB ?
❖ MATLAB stands for MATrix LABoratory
It allows :
❖ matrix manipulation
❖ plotting of functions and data
Department of Mathematics 3
❖implementation of algorithms
❖analyze data
❖develop algorithms
❖create models and applications
❖numerous built-in commands and math
functions which helps in mathematical
calculations, generating plots and performing
numerical methods
Department of Mathematics 4
Uses of MATLAB
Department of Mathematics 5
Department of Mathematics 6
List of Programs
Introduction to MATLAB
1 2D plots for Cartesian and polar curves.
1.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.
1.2 Use the linspace function to define x as a vector of 150 values between 0 and 10. Create a 2-D line
plot of cos5x. 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, x-label and y-label commands.
1.3 Plot the graph of Cardiod given by r = 3(1 + cos θ).
2 Radius of curvature of a curve.
2.1 Find the radius of curvature of the curve 𝑦 = log(𝑠𝑒𝑐𝑥).
2.2 Find the radius of curvature of the curve r = 2/(1 – cos ϴ).
2.3 Find the radius of curvature of the curve y= x2 + x + 1
3 Partial derivatives and plotting the graph.
3.1 Find the first order partial derivative of 𝑓 = 𝑠𝑖𝑛2 𝑥 + 𝑐𝑜𝑠 2 𝑦 with respect to 𝑥 and 𝑦 and plot the
graph
3.2 Find the second order partial derivative of 𝑓 = 𝑠𝑖𝑛2 𝑥 + 𝑐𝑜𝑠 2 𝑦 with respect to 𝑥 and plot the graph
3.3 Find the second order partial derivative of 𝑓 = sin x + cos y + xy with respect to x and plot the
graph
4 Jacobians of a vector function.
4.1 Find the Jacobian of the vector function 𝑢 = 𝑥 1 − 𝑦 , 𝑣 = 𝑥𝑦.
4.2 Find the Jacobian of the vector function u = x3(1-y), v = xy2.
4.3 Find the Jacobian of the vector function 𝑥 = 𝑟𝑐𝑜𝑠𝜃
Department ,
of Mathematics 7
5 Gradient of a scalar.
2 2
5.1 Find the gradient vector of f x, y = xe−x −y with respect to vector [x,y]. Plot the contour lines and vectors in the same
figure.
5.2 Find the gradient of a function f(x,y) = (sinx + sin y)^2, and plot it as a quiver (velocity) plot.
5.3 Find the gradient vector of f x, y, z = 2 y z sinx + 3x sinz cosy with respect to vector [x ,y, z ].
6 Divergence and Curl of a vector field.
6.1 Find the divergence of the vector field 𝑉 𝑥, 𝑦, 𝑧 = 𝑥, 2𝑦 2 , 3𝑧 3 with respect to 𝑋 = 𝑥, 𝑦, 𝑧
6.2 Show that the divergence of the curl of the vector field V(x, y, z)=(x, 2 y^2, 3 z^3) with respect to X = (x, y, z) is 0.
6.3 Find the curl of the vector field V(x,y,z)=(𝑥^3y^2z, xy^3z^2, yz^3x^2) with respect to vector X=(x,y,z) in Cartesian
coordinates.
7 Solution of first order differential equation and plotting the solution curves
7.1 Solve the first order differential equation dy/dx = xy and plot the graph.
7.2 Solve .
7.3 Solve .
𝑑2 𝑦
Solve the second-order differential equation = 𝑎2 𝑦 with the initial conditions y(0)=b and y′(0)=1.
8.2 𝑑𝑡 2
Solve this given second-order differential equation with two initial conditions.
8.3 𝑑2 𝑦
= cos 2𝑥 − 𝑦 , y(0)=1, y′(0)=0.
𝑑𝑥 2
Department of Mathematics 8
9 Solution of homogeneous ordinary differential equations of higher order.
9.1 Solve the second order homogeneous differential equation.
2 x2 d2y/dx2 +3x dy/dx - y = 0.
9.2 Solve the third-order differential equation d3u /dx3 = u with three initial conditions.
u(0) = 1, u′(0) = −1, u′′(0) = π.
9.3 Solve the second-order differential equation (Puiseux series), (x2 + 1)d2y/dx2 – 2 x dy/dx + y = 0, with the initial
conditions y(0) = 5, y′(0) = a.
10 Solution of non-homogeneous ordinary differential equations of higher order.
10.1 Solve the second-order differential equation with two initial conditions:
d2y/dx2 = cos(2x) − y, y(0) = 1, y′(0) = 0.
10.2 Solve the second-order differential equation (Airy equation) d2y/dx2 = xy.
10.3 Solve the second-order differential equation (Airy equation)
d2y/dx2+ dy/dx = sin(x) + y.
Department of Mathematics 9
EXPERIMENT 1
Department of Mathematics 10
Example 1.1: Plot three sine curves in [0, 2] with a small shift of 0.25 between each line
and step size /100. Use the default line style for the first line. Specify a dashed line style
for the second line and a dotted line for the third line. Add a title and axis labels to the
graph.
Syntax
clear all;
clc;
x=0:pi/100:2*pi;
y1=sin(x); Step
y2=sin(x-0.25); size
y3=sin(x-0.5);
plot(x,y1,x,y2,'--',x,y3,':');
title('graph of sine curves with phase shift of 0.25');
xlabel('0\leq x\leq 2\pi');
ylabel('sine functions');
legend('sinx','sin(x-0.25)','sin(x-0.5)');
Department of Mathematics 11
Department of Mathematics 12
Example 1.2: Use the linspace function to define x as a vector of 150 values between 0
and 10. Define y as cos(5x). 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.
Syntax
x = linspace(0, 10, 150);
y = cos(5*x);
plot(x, y, ’color’, [0, 0.7, 0.9]);
title('2-D plot of cosine curve’); Values
xlabel('x’); ylabel('cos(5x)’);
of x
Department of Mathematics 13
[0,0.7,0.9] [0, 0.9, 0.7]
Department of Mathematics 14
Example 1.3: Plot the graph of cardioid given by r = 3(1+cos ), where lies between 0 and
2 and step size is 0.01 .
Syntax
clc;
theta=0:0.01:2*pi;
r=3*(1+cos(theta));
polarplot(theta, r, ’r’);
title(‘graph of the cardioid r=3(1+cos(theta))’);
Department of Mathematics 15
Department of Mathematics 16
EXPERIMENT 2
Department of Mathematics 17
Example 2.1: Find the radius of curvature of the curve y =logsec(x).
Syntax
clear all;
clc;
syms x y
y = input('Enter the value of the function \n');
R = (1+diff(y,x)^2)^(3/2)/diff(y,x,2);
fprintf('Radius of curvature of the curve is given by\n');
R
Output:
Enter the value of the function
log(sec(x))
Department of Mathematics 18
Example 2.2: Find the radius of curvature of the curve y = x 2 + 3x + 1.
Syntax
clear all;
clc;
syms x y
y = input('Enter the value of the function \n');
R = (1+diff(y,x)^2)^(3/2)/diff(y,x,2);
fprintf('Radius of curvature of the curve is given by \n');
R
Output:
Enter the value of the function
𝑥^2+ 3*x + 1
Department of Mathematics 19
Example 2.3: Find the radius of curvature of the curve r =2/(1 − cos()).
Syntax
clear all;
clc;
syms r theta
r = input('Enter the value of the function \n’);
r1 = diff(r, theta);
r2 = diff(r, theta, 2);
R = (r^2 + r1^2)^(3/2)/(r^2 + 2*r1^2 - r* r2);
fprintf('Radius of curvature of the curve is given by \n');
R
Department of Mathematics 20
Output:
Enter the value of the function
𝟐/(𝟏 − 𝐜𝐨𝐬 𝐭𝐡𝐞𝐭𝐚 )
Department of Mathematics 21
EXPERIMENT 3
Department of Mathematics 22
Example 3.1 Find the first order partial derivative of
f = sin2x + cos2y w.r.t. x and y.
Syntax :
clear all;
clc;
syms x y
f = sin(x)^2 + cos(y)^2;
p1 = diff(f,x)
subplot(1,2,1)
fplot(p1,[-1,1],’r’);
title(‘Graph of partial deriv. of f w.r.t. x’);
xlabel(‘x’);ylabel(‘p1’);
p2 = diff(f,y)
subplot(1,2,2)
fplot(p2,[-1,1],’r’);
title(‘Graph of partial deriv. of f w.r.t. y’);
xlabel(‘x’);ylabel(‘p2’);
Department of Mathematics 23
Output :
Department of Mathematics 24
Example 3.2 Find the second order partial derivative
of f = sin2x + cos2y w.r.t. x .
Syntax :
clear all;
clc;
syms x y
f = sin(x)^2 + cos(y)^2;
p = diff(f,x,2)
fplot(p,[-1,1],’r’);
title(‘Graph of second order partial deriv. of f w.r.t. x’);
xlabel(‘x’);ylabel(‘p’);
Department of Mathematics 25
Output :
Department of Mathematics 26
Example 3.3 Find the second order partial derivative
of f = sin x + cos y +xy w.r.t. x .
Syntax :
clear all;
clc;
syms x y
f = sin(x) + cos(y) + x*y;
p = diff(f,x,2)
fplot(p,[-1,1],’r’);
title(‘Graph of second order partial deriv. of f w.r.t. x’);
xlabel(‘x’);ylabel(‘p’);
Department of Mathematics 27
Output :
Department of Mathematics 28
EXPERIMENT 4
Finding Jacobian
Department of Mathematics 29
Example 4.1 Find the Jacobian of u = x(1-y), v = xy.
Syntax :
clear all;
clc;
syms x y
u = x*(1-y); v = x*y;
j = jacobian([u v],[x y])
J = det(j)
Department of Mathematics 30
Output :
Department of Mathematics 31
Example 4.2 Find the Jacobian of u = x 3 (1-y), v = xy 2 .
Syntax :
clear all;
clc;
syms x y
u = x^3*(1-y); v = x*y^2;
j = jacobian([u v],[x y])
J = det(j)
Department of Mathematics 32
Output :
Department of Mathematics 33
Example 4.3 Find the Jacobian of x = rcosθ, y = rsinθ .
Syntax :
clear all;
clc;
syms r theta
x = r*cos(theta); y = r*sin(theta);
j = jacobian([x y],[r theta])
J = det(j)
Department of Mathematics 34
Output :
Department of Mathematics 35
EXPERIMENT 5
Department of Mathematics 36
−𝑥 2 −𝑦 2
Example 5.1 Find the gradient vector of f(x,y) = x𝑒 . Plot
the contour lines and vectors in the same figure.
Syntax :
clear all;
clc;
syms x y
f = x* exp(-x^2 - y^2);
X=[x y];
fprintf(‘Gradient of f is given by’)
gradient(f, X)
Department of Mathematics 37
Example 5.1 (Continued……)
Syntax :
%Plotting the contour lines and vectors in the same figure.
x = -2:0.2:2;
y = x’;
the matrix is square and the power is a
z = x .* exp(-x.^2 - y.^2); scalar. To operate on each element of the
[px py] = gradient(z); matrix individually, use POWER (.^) for
%Figure elementwise power.
contour(x, y, z, ‘b’)
hold on
quiver(x, y, px, py, 2, ‘m’)
hold off
Department of Mathematics 38
Output :
Department of Mathematics 39
Example 5.2 Find the gradient of a function f(x,y) = (sinx+sin y)^2,
and plot it as a quiver (velocity) plot.
Syntax :
clear all;
clc;
syms x y
f = (sin(x)+sin(y))^2;
X=[x y];
fprintf(‘Gradient of f is given by’)
gradient(f, X)
Department of Mathematics 40
Example 5.2 (Continued……)
Syntax :
%Plotting the vectors
x = -1:0.1:1;
y = x’;
z = (sin(x)+sin(y))^2;
[px py] = gradient(z);
%Figure
quiver(x, y, px, py, ‘m’)
Department of Mathematics 41
Output :
Department of Mathematics 42
Example 5.3 Find the gradient of the scalar field f =
2yzsinx + 3xsinzcosy.
Syntax :
clear all;
clc;
syms x y z
f(x,y,z) = 2*y*z*sin(x) + 3*x*sin(z)*cos(y);
X = [x y z];
fprintf(‘Gradient of the scalar field f is’);
gradient(f, X)
Department of Mathematics 43
Output :
Department of Mathematics 44
Example 5.4 Find the gradient of a function f(x,y) = (sinx+sin y)^2,
and plot it as a quiver (velocity) plot.
Syntax :
clear all;
clc;
syms x y
f = (sin(x)+sin(y))^2;
v=[x y];
fprintf(‘Gradient of f is given by’); meshgrid is used to create a
g=gradient(f, v) rectangular grid out of an array of x
[X, Y]=meshgrid(-1:0.1:1,-1:0.1:1); values and an array of y values
G1=subs(g(1), v, {X,Y});
G2=subs(g(2), v, {X,Y});
quiver(X, Y, G1, G2)
Department of Mathematics 45
Output :
Department of Mathematics 46
EXPERIMENT 6
Department of Mathematics 47
Example 6.1 Find the divergence of the vector field
V = xiƸ + 2y 2 jƸ + 3z 3 k.
Syntax :
clear all;
clc;
syms x y z
V = [x 2*y^2 3*z^3];
X=[x y z];
fprintf(‘Divergence of the vector field V is given by’);
divergence(V, X)
Department of Mathematics 48
Output :
Department of Mathematics 49
Example 6.2 Find the curl of the vector field
V = zy 2 𝑥 3 iƸ + xy 3 z 2 jƸ + y𝑥 2 z 3 k.
Syntax :
clear all;
clc;
syms x y z
V = [z*y^2*x^3 x*y^3*z^2 y*x^2*z^3];
X=[x y z];
fprintf(‘Curl of the vector field V is given by’);
curl(V, X)
Department of Mathematics 50
Output :
Department of Mathematics 51
Example 6.3 Show that the divergence of the curl of the vector field
V = xiƸ + 2y 2 jƸ + 3z 3 k is 0.
Syntax :
clear all;
clc;
syms x y z
V = [x 2*y^2 3*z^3];
X=[x y z];
fprintf(‘Curl of the vector field V is given by’);
curl(V, X)
fprintf(‘Divergence of curl of the vector field V is given by’);
divergence(curl(V, X), X)
Department of Mathematics 52
Output :
Department of Mathematics 53
EXPERIMENT 7
Department of Mathematics 54
Example 7.1 Solve the first order differential equation
dy/dx = xy and plot the graph.
Syntax :
clear all;
clc;
syms y(x)
ode=diff(y,x)==x*y;
fprintf('Solution of given DE is');
dsolve(ode)
ezplot(exp(x^2/2),[-10,10]);
title('Graph of Solution y=exp(x^2/ 2) is');
xlabel('x');
ylabel('y');
Department of Mathematics 55
Example 7.2 Solve the first order differential equation
2 𝑦
dy/dx = 𝑦 + and plot the graph.
𝑥
Syntax :
clear all;
clc;
syms y(x)
ode=diff(y,x)==y^2 + y/x;
fprintf('Solution of given DE is');
dsolve(ode)
ezplot(-2/x,[-10,10]);
title('Graph of Solution y= - 2/x is');
xlabel('x');
ylabel('y');
Department of Mathematics 56
Example 7.3 Solve the first order differential equation
(−2𝑥𝑦−3𝑦)
dy/dx = and plot the graph.
(𝑥 2 +3𝑥)
Syntax :
clear all;
clc;
syms y(x)
ode=diff(y,x)==(- 2*x*y - 3*y)/(x^2 + 3*x);
fprintf('Solution of given DE is');
dsolve(ode)
ezplot(1/x*(x+3),[-10,10]);
title('Graph of Solution y=1/x*(x+3) is');
xlabel('x');
ylabel('y');
Department of Mathematics 57