MATLAB Merged
MATLAB Merged
MATLAB
Using MATLAB, an image (or any other data like sound, etc.) can
be converted to a matrix and then various operations can be
performed on it to get the desired results and values.
MATLAB is a fourth-generation high-level programming language
and interactive environment for numerical computation,
visualization and programming.
It has numerous built-in commands and math functions that help
in mathematical calculations, generating plots, and performing
numerical methods.
MATLAB's Power of Computational Mathematics
• Data Analysis
• Calculus and Differential Equations
• Numerical Calculations
• Integration
• Transforms
• Curve Fitting
• Special Functions
Uses of MATLAB
• Algorithm development
• Control Systems
• Current Directory
• View folders and m-files
• Workspace
• View program variables
• Double click on a variable
to see it in the Array Editor
• Command History
• view past commands
• save a whole session
using diary
15
Hands on Practice
Addition + 5+3=8
Subtraction - 5-3=2
Multiplication * 5*3=15
Exponentiation ^ 5^3=125
Special Variables and Constants
Another example,
>> format short
>> x = 7 + 10/3 + 5 ^ 1.2
MATLAB will execute the above statement and return the
following result: x = 17.232
The format bank command rounds numbers to two decimal
places.
The format Command, Continued…
For example,
>> format bank
>> daily_wage = 177.45; weekly_wage = daily_wage * 6
MATLAB will execute the above statement and return the
following result: weekly_wage = 1064.70
MATLAB displays large numbers using exponential notation.
The format short e command allows displaying in exponential
form with four decimal places plus the exponent.
The format Command, Continued…
For example,
>> format short e
4.678 * 4.9
MATLAB will execute the above statement and return the
following result: ans = 2.2922e+01
The format long e command allows displaying in exponential
form with four decimal places plus the exponent.
The format Command, Continued…
For example, >> format long e
>> x = pi
MATLAB will execute the above statement and return the
following result: x = 3.141592653589793e+00
The format rat command gives the closest rational expression
resulting from a calculation.
For example, >> format rat
4.678 * 4.9
MATLAB will execute the above statement and return the
following result: ans = 34177/1491
Creating Vectors
7 8 9 10 11
Creating Vectors, Continued…
Another example,
>> X = [7 8 9 10 11]; >> Y = [2, 3, 4, 5, 6]; >> Z = X +Y
MATLAB will execute the above statement and return the
following result:
Z=
9 11 13 15 17
Creating Vectors, Continued…
Data types are those which define the type of data that we are
using.
Some common data types are:
Integers
Floating point numbers
Scalar
Character
Strings
Arrays
Integers
For Example:
>> 2+3
ans =
5
>> 4-5
ans =
-1
Integers, Continued…
>> 2*8
ans =
16
Note that when one integer is divided by another integer, the
result may be an integer or a fraction.
For example:
>> 6/4
ans =
3/2
Integers, Continued…
>> 6/3
ans =
2
Floating Point Numbers
rand() function
Special Types of Arrays, Continued…
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Special Types of Arrays, Continued…
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Special Types of Arrays, Continued…
1 1 1
1 1 1
1 1 1
1 1 1
Special Types of Arrays, Continued…
For example:
a=10; b=10; a==b & a=10; b=12; a==b
ans = ans =
Logical Logical
.
1 0
Relational Operators continu…
For example:
a=15; b=20; a<=b & a=12; b=115; a>=b
ans = ans =
Logical Logical
.
1 0
Relational Operators continu…
For example:
a=15; b=20; a~=b & a=115; b=115; a~=b
ans = ans =
Logical Logical
.
1 0
Logical Operators:
The logical data type represents true or false states using the
numbers 1 and 0, respectively.
The three logical operators are &; |; and ~
The meaning of & operator is AND
.
The meaning of | operator is OR
The meaning of ~ operator is NOT
Logical Operator & truth table:
1 1 1
1 0 0
0 . 1 0
0 0 0
Logical Operator &, Continued…
If the two operands evaluate to true (1) or false (0), then the operator
OR has the following effect.
1 1 1
.
1 0 1
0 1 1
0 0 0
Logical Operator |, Continued…
1 0
0 . 1
Logical Operator ~, Continued…
Consider an example:
Type the following code in the editor:
>> a=25; b=26; c=27; d=a+b+c
After creating and saving the file, you can run it by clicking the Run
button on the editor window.
The command window prompt displays the result: d=78
Function Files
case 'F'
fprintf('Better try again\n' );
otherwise
fprintf('Invalid grade\n' );
end
When we run the file, it displays the following result: Well done
THANK YOU
ENGINEERING MATHEMATICS-I
MATLAB
When we run the file, MATLAB displays the following 3-D map:
MATLAB - Three Dimensional Plots, Continued…
When we run the file, MATLAB displays the following 3-D map:
MATLAB - Three Dimensional Plots, Continued…
𝑥𝑦 𝑥 2 −𝑦 2
the function 𝑧 = 𝑥 2 +𝑦 2
.
When we run the file, MATLAB displays the following 3-D map:
Adding Title and Labels on the Graph
Using MATLAB we can add title, labels along the x-axis and
y-axis of the graph.
The xlabel and ylabel commands generate labels along x-axis
and y-axis.
The title command allows you to put a title on the graph.
The script file is as follows:
>> x = [0:0.01:10];
>> y = sin(x);
>> plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph')
Adding Title and Labels on the Graph
>> syms x y
>> f=(x*y^3)/(x+y)
>> diff(f,x)
>> diff(f,y)
>> diff(f,x,2)
>> diff(f, x, y)
>> diff(f, y, x)
Out put: f = (x*y^3)/(x + y); ans = y^3/(x + y) - (x*y^3)/(x + y)^2
Finding partial derivative of a function, Continued…
>> syms x
>> f = exp(x*sin(x));
Out put:
𝜋
Expand f(x)=log(cosx) about the point x = up to fifth degree terms.
3
>> syms x
>> f = log(cos(x));
Out put:
>> syms x
>> f = log(sec(x));
Out put:
>> syms x
>> f = sin(log(x^2+2*x+1));
Out put:
>> a = sin(t);
>> plot(t,a)
>> a = cos(t);
>> plot(t,a)
Taylor’s and Macluarin’s series expansion of a function of two variables:
x 𝜋
Expand f x, y = e 𝑐𝑜𝑠𝑦 about the point 𝑥 = 1, 𝑦 = up to three degree
4
terms.
>> syms x y
>> f=exp(x)*cos(y);
>> t = taylor(f, [x, y], [1, pi/4], 'Order', 3)
Out put:
T = (2^(1/2)*exp(1))/2 - (2^(1/2)*exp(1)*(y - pi/4)^2)/4 + (2^(1/2)*exp(1)*(x -
1)^2)/4 - (2^(1/2)*exp(1)*(y - pi/4))/2 + (2^(1/2)*exp(1)*(x - 1))/2 -
(2^(1/2)*exp(1)*(y - pi/4)*(x - 1))/2
Taylor’s and Macluarin’s series expansion of a function of two variables:
>> syms x y
>> f=x^3+y^3+x*y^2;
terms.
>> syms x y
>> f=exp(y)*log(1+x);
Out put:
terms.
>> syms x y
>> f=exp(x)*tan(y);
Out put:
𝒅𝒚 𝒙𝟑 +𝒚𝟑
Solve the differential equation: 𝒅𝒙 = 𝒚𝟐
, given 𝑦 0 = 2.
2 𝑑𝑦
Solve the differential equation: 𝑥 − 1 𝑑𝑥
+ 2𝑥𝑦 = 1, given 𝑦 0 =
1.
>> y0=1;
>> x0=0;
>> xend=[1,5,10];
>> xspan=[x0,xend];
>> [x,y]=ode45(@(x,y)(1-2*x*y)/(x^2-1),xspan,y0);
>> disp([x,y])
>> plot(x,y)
Plot the graph of solution of first order differential equations, Continued
𝑑𝑦
Solve the differential equation: = 3𝑡, given 𝑦 0 = 1. Find
𝑑𝑡
syms y(t)
ode = (diff(y,t)+y)^2 == 1;
ySol(t) = dsolve(ode)
Output: ySol(t) = C1*exp(-t) + 1; C2*exp(-t) – 1
To find the values of C1 and C2, we use:
>> cond = y(0) == 0;
>> ySol(t) = dsolve(ode,cond)
Output: ySol(t) =exp(-t) – 1; 1 - exp(-t)
Plot the graph of solution of first order differential equations
𝑑𝑥
Solve the differential equation: 𝑑𝑡 = 𝑥 + 𝑡, given 𝑥 0 = 0.
𝒅𝒚 𝒙𝟑 +𝒚𝟑
Solve the differential equation: 𝒅𝒙 = 𝒚𝟐
, given 𝑦 0 = 2.
2 𝑑𝑦
Solve the differential equation: 𝑥 − 1 𝑑𝑥
+ 2𝑥𝑦 = 1, given 𝑦 0
= 1.
>> y0=1;
x0=0;
xend=[1,5,10];
xspan=[x0,xend];
[x,y]=ode45(@(x,y)(1-2*x*y)/(x^2-1),xspan,y0);
disp([x,y])
plot(x,y)
Plot the graph of solution of first order differential equations, Continued
𝑑𝑦
Solve the differential equation: = 3𝑡, given 𝑦 0 = 1. Find
𝑑𝑡
Solve: 2𝑦 ′′ + 3𝑦 ′ − 2𝑦 = 0.
>> syms y(x)
ode = 2*diff(y,x,2)+3*diff(y,x,1)-2*y == 0;
ySol(x) = dsolve(ode)
Output:
ySol(x) =C1*exp(-2*x) + C2*exp(x/2)
Higher order differential equations, Continued…
Solve: 𝑦 ′′ − 3𝑦 ′ + 2𝑦 = 0, y 0 = −1, 𝑦 ′ 0 = 0.
>> syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y - 3*Dy + 2*y == 0;
ySol = dsolve(ode, y(0) == -1, Dy(0) == 0)
% Define The Initial >> Condition For ‘Dy(0)’ To Be ‘Some Value’
figure
ezplot(ySol)
Higher order differential equations, Continued…
Solve: 4𝑦 ′′′ + 4𝑦 ′′ + 𝑦 ′ = 0.
>> syms y(x)
>> ode = 4*diff(y,x,3)+4*diff(y,x,2)+diff(y,x,1) == 0;
>> ySol(x) = dsolve(ode)
Output:
ySol(x) =C1 + C2*exp(-x/2) + C3*x*exp(-x/2)
Higher order differential equations, Continued…
Solve: 𝑦 ′′′ − 4𝑦 ′′ + 𝑦 ′ + 6𝑦 = 0.
>> syms y(x)
>> ode = diff(y,x,3)-4*diff(y,x,2)+diff(y,x,1)+6*y == 0;
>> ySol(x) = dsolve(ode)
Output:
ySol(x) =C1*exp(-x) + C2*exp(2*x) + C3*exp(3*x)
Higher order differential equations, Continued…
figure
ezplot(ySol)
Output: ySol =exp(3*x) - 2*exp(2*x) + exp(x)
Higher order differential equations, Continued…
Solve: 𝑦 ′′′′ = 0.
>> syms y(x)
>> ode = diff(y,x,4) == 0;
>> ySol(x) = dsolve(ode)
Output:
ySol(x) =(C1*x^3)/6 + (C2*x^2)/2 + C3*x + C4
Higher order differential equations, Continued…
𝑑4 𝑦 𝑑2 𝑦
Solve: + 8 𝑑𝑥2 + 16𝑦 = 0.
𝑑𝑥 4
𝑑4 𝑦
Solve: + 4𝑦 = 0.
𝑑𝑥 4
>> figure
>> ezplot(ySol)
Output:
ySol =
2*exp(x) - 2*x - 2
Non-Homogeneous differential equations
𝑑2 𝑦 𝑑𝑦
Solve: −5 + 6𝑦 = 𝑒 5𝑥 .
𝑑𝑥 2 𝑑𝑥
𝑑2 𝑦 𝑑𝑦
Solve: −4 + 4𝑦 = 2𝑒 2𝑥 .
𝑑𝑥 2 𝑑𝑥
Solve: 𝐷3 + 𝐷2 − 𝐷 − 1 𝑦 = 𝑐𝑜𝑠2𝑥.
>> syms y(x)
>> ode= diff(y,x,3)+diff(y,x,2)-diff(y,x,1)-1*y == cos(2*x);
>> ySol(x) = dsolve(ode)
Output:
ySol(x) =
C3*exp(x) - (5^(1/2)*cos(2*x - atan(2)))/25 + C1*exp(-x) + C2*x*exp(-x)
Non-Homogeneous differential equations, Continued…
Solve: 𝐷2 − 4𝐷 + 13 𝑦 = 8𝑠𝑖𝑛3𝑥, 𝑦 0 = 0, 𝑦 ′ 0 = 2.
>> syms y(x)
>> Dy = diff(y);
>> D2y = diff(y,2);
>> ode = D2y - 4*Dy + 13*y == 8*sin(3*x);
>> ySol = dsolve(ode, y(0) == 1, Dy(0) == 2)
>> figure
>> ezplot(ySol)
Non-Homogeneous differential equations, Continued…
Output: ySol =
cos(3*x)*(sin(6*x)/5 - cos(6*x)/15 + 2/3) - sin(3*x)*(cos(6*x)/5 +
sin(6*x)/15) + (2*cos(3*x)*exp(2*x))/5 + (sin(3*x)*exp(2*x))/5
Non-Homogeneous differential equations, Continued…
Solve: 𝑦 ′′ − 𝑦 = 1 + 𝑥 5
syms y(x)
ode= diff(y,x,2)-y ==1+ x^5;
ySol(x) = dsolve(ode)
Output:
ySol(x) = C2*exp(x) - 120*x - 20*x^3 - x^5 + C1*exp(-x) - 1
Non-Homogeneous differential equations, Continued…
Solve: 𝑦 ′′ + 2𝑦 ′ + 𝑦 = 2𝑥 + 𝑥 2 .
>> syms y(x)
>> ode= diff(y,x,2)+2*diff(y,x,1)+y == 2*x+x^2;
>> ySol(x) = dsolve(ode)
Output:
ySol(x) =x^2 - 2*x + C1*exp(-x) + C2*x*exp(-x) + 2
Non-Homogeneous differential equations, Continued…
𝑑2 𝑦 𝑑𝑦
Solve: +2 − 3𝑦 = 𝑒 𝑥 𝑐𝑜𝑠𝑥.
𝑑𝑥 2 𝑑𝑥
𝑑2 𝑦
Solve: + 2𝑦 = 𝑒 3𝑥 𝑥 2 .
𝑑𝑥 2
syms y(x)
ode=diff(y,x,2)+2*y == exp(3*x)*x^2;
ySol(x) = dsolve(ode)
Output: ySol(x) = C1*cos(2^(1/2)*x)-C2*sin(2^(1/2)*x)+
(exp(3*x)*cos(2^(1/2)*x)*(100*cos(2^(1/2)*x) - 18*2^(1/2)*sin(2^(1/2)*x) -
264*x*cos(2^(1/2)*x)+242*x^2*cos(2^(1/2)*x)363*2^(1/2)*x^2*sin(2^(1/2)
*x)+154*2^(1/2)*x*sin(2^(1/2)*x)))/2662+(exp(3*x)*sin(2^(1/2)*x)*+……..
THANK YOU
ENGINEERING MATHEMATICS-I
MATLAB
of the curve.
>> syms x y a
>> F(x,y)=x^(2/3)+y^(2/3)-a^(2/3);
>> dy_dx = - diff(F,x)/diff(F,y)
Out put: dy_dx(x, y) =-y^(1/3)/x^(1/3)
>> G(x,y)=-y^(1/3)/x^(1/3);
>> a=diff(G,x);
>> b=diff(G,y);
>> c=a+b*G(x,y)
Out put: c(x, y) =1/(3*x^(2/3)*y^(1/3)) + y^(1/3)/(3*x^(4/3))
Radius Of Curvature Continued…
>> simplify(c)
Out put: (x, y) =(x^(2/3) + y^(2/3))/(3*x^(4/3)*y^(1/3))
>> d=(1+G(x,y)^2)^(3/2)
Out put: d =(y^(2/3)/x^(2/3) + 1)^(3/2)
>> rho=d/c
Out put: rho(x, y) =(y^(2/3)/x^(2/3) + 1)^(3/2)/(1/(3*x^(2/3)*y^(1/3)) +
y^(1/3)/(3*x^(4/3)))
>> simplify(rho(x,y))
Out put: (y^(2/3)/x^(2/3) + 1)^(3/2)/(1/(3*x^(2/3)*y^(1/3)) +
y^(1/3)/(3*x^(4/3)))
Radius Of Curvature Continued…
Find the radius of curvature of the curve 𝑥𝑦 = 𝑐 2 at any point (x,y) of the
curve.
>> syms x y a
F(x,y)=x*y-a^2;
dy_dx = - diff(F,x)/diff(F,y)
Out put: dy_dx(x, y) =-y/x
G(x,y)=-y/x;
a=diff(G,x);
b=diff(G,y);
c=a+b*G(x,y)
Out put: c(x, y) =(2*y)/x^2
Radius Of Curvature Continued…
>> d=(1+G(x,y)^2)^(3/2)
Out put: d =(y^2/x^2 + 1)^(3/2)
>> rho=d/c
Out put: rho(x, y) =(x^2*(y^2/x^2 + 1)^(3/2))/(2*y)
>> simplify(rho)
Out put: (x, y) =(x^2*(y^2/x^2 + 1)^(3/2))/(2*y)
Radius Of Curvature Continued…
Find the radius of curvature of the curve 𝑟 = 𝑒 2𝜃 at any point on the curve.
>> syms theta
>> r=exp(2*theta);
>> r1=diff(r,theta);
>> r2=diff(diff(r,theta));
>> a=(r^2+r1^2)^(3/2)
Out put: a =(5*exp(4*theta))^(3/2)
>> b=r^2+2*r1^2-r*r2
Out put: b =5*exp(4*theta)
>> rho=a/b
Out put: rho =(exp(-4*theta)*(5*exp(4*theta))^(3/2))/5
Radius Of Curvature Continued…
>> simplify(rho)
Out put: 5^(1/2)*exp(4*theta)^(1/2)
Radius Of Curvature Continued…
>> c=(x1*y2)-(y1*x2)
Out put: c =48*t*(- 12*t^3 + 12*t) + 24*t^2*(36*t^2 - 12)
>> d=simplify(c)
Out put: d =288*t^2*(t^2 + 1)
>> e=b^(3/2)
Out put: e =(144*t^2*(t^2 + 1)^2)^(3/2)
>>rho=e/d
Out put: rho =(144*t^2*(t^2 + 1)^2)^(3/2)/(288*t^2*(t^2 + 1))
>>simplify(rho)
Out put: rho= (6*(t^2*(t^2 + 1)^2)^(3/2))/(t^2*(t^2 + 1))
THANK YOU
ENGINEERING MATHEMATICS - I
syms x y a b p q
z=x^2/(2*a^2)+y^2/(2*b^2)
eq1=p==diff(z,x)
c1=solve(eq1,a)
eq2=q==diff(z,y)
c2=solve(eq2,b)
pde=subs(z,a,c1)
pde=subs(pde,b,c2)
ENGINEERING MATHEMATICS - I
3 7 1 3
1. Evaluate: Γ 0.5 , Γ 1 , Γ ,Γ ,Γ Γ
2 2 4 4
MATLAB CODE:
gamma(0.5)
gamma(1)
gamma(3/2)
gamma(7/2)
gamma(1/4)* gamma(3/4)
ENGINEERING MATHEMATICS - I
MATLAB CODE:
x = -3.5:3.5;
y = gamma(x)
MATLAB OUTPUT:
y = 1×8
fplot(@gamma)
hold on
fplot(@(x) 1./gamma(x))
hold off
legend('\Gamma(x)','1/\Gamma(x)')
grid on
Gamma function
Geometrical representation of Gamma function:
Gamma function
(Or) Geometrical representation of Gamma function: Γ 𝑛 =
∞ −𝑥 𝑛−1
0 𝑒 𝑥 𝑑𝑥; 𝑛 >0
fplot(@gamma)
hold on
fplot(@(x) 1./gamma(x))
ylim([-10 10])
legend('\Gamma(x)','1/\Gamma(x)')
hold off
grid on
Evaluation of Beta function
4. Compute the beta function for integer arguments n=3 and m=1,...,10.
MATLAB CODE:
format rat
B = beta((1:10)',3)
THANK YOU
Dr. G K Jagatheswari
Department of Science & Humanities
[email protected]
ENGINEERING MATHEMATICS-I
MATLAB
4. Compute the beta function for integer arguments w=3 and z=1,...,10.
>> format rat
>> B = beta((1:10)’,3) Output:
THANK YOU