MATLAB - 2nd Sem
MATLAB - 2nd Sem
MATLAB
>> syms x y z
>>d = det(J)
𝑦𝑧 𝑥𝑧 𝑥𝑦
Out Put: J= 0 2𝑦 0
1 0 1
d = 2*y^2*z - 2*x*y^2
Finding Jacobian Continuation:
If 𝑢 = 𝑥 2 − 2𝑦, 𝑣 = 𝑥 + 𝑦 + 𝑧, 𝑤 = 𝑥 − 2𝑦 + 3z, then evaluate the
Jacobian J.
>> syms x y z
>>d = det(J)
d =10*x + 4
Finding Jacobian Continuation:
>> x=r*cos(theta);
>> y=r*sin(theta);
>> d = det(J)
>> simplify(d)
Finding Jacobian Continuation:
Out Put:
d=
12*r^3*cos(theta)*sin(theta)^3 + 12*r^3*cos(theta)^3*sin(theta)
ans =
6*r^3*sin(2*theta)
Finding Jacobian Continuation:
>> syms u v
>> inv(J)
>> J*inv(J)
>> det(J*inv(J))
Finding Jacobian Continuation:
Out Put:
J =[ 1 - v, -u] [ v, u]
ans = [ 1, 0] [ 0, 1]
ans =1
Evaluation of Double Integral :
1 1−𝑥 1
Evaluate 0 0 𝑑𝑦𝑑𝑥
𝑥+𝑦 1+𝑥+𝑦 2
2𝜋 𝜋
Evaluate −𝜋 0 𝑦𝑠𝑖𝑛 𝑥 + 𝑥𝑐𝑜𝑠(𝑦) 𝑑𝑦𝑑𝑥.
>>fun = @(x,y) y.*sin(x)+x.*cos(y);
>>q = integral2(fun,-pi,2*pi,0,pi);
Out put:
q=
-9.8696
Evaluation of Double Integral Continuation:
5 𝑥2
Evaluate 0 0 𝑥 𝑥 2 + 𝑦 2 𝑑𝑦𝑑𝑥 .
Out put:
q = 1.8880e+04
Evaluation of Double Integral Continuation:
1 𝑥
Evaluate 0 0 𝑥𝑦𝑑𝑦𝑑𝑥
q=
0.1667
Evaluation of Double Integral Continuation:
𝜋Τ 1ൗ 𝑟
2 𝑠𝑖𝑛𝜃+𝑐𝑜𝑠𝜃
Evaluate 0 0 𝑑𝑟𝑑𝜃
𝑟𝑐𝑜𝑠𝜃+𝑟𝑠𝑖𝑛𝜃((1+𝑟𝑐𝑜𝑠𝜃+𝑟𝑠𝑖𝑛𝜃)2
q = 0.2385
Evaluation of Triple Integral:
3 2 1
Evaluate 0 0 0 𝑥 + 𝑦 + 𝑧 𝑑𝑧𝑑𝑥𝑑𝑦
>> fun = @(x,y,z) x+y+z
>> q = integral3(fun,0,3,0,2,0,1)
Output: q = 18
Evaluation of Triple Integral Continuation:
1 1−𝑥 2 1−𝑥 2 −𝑦 2
Evaluate −1 − 1−𝑥 2 − 1−𝑥 2 −𝑦2 ( 𝑥𝑐𝑜𝑠 𝑦 + 𝑥 2 cos 𝑧 )𝑑𝑧𝑑𝑦𝑑𝑥
Out put:
q = 0.7796
Evaluation of Triple Integral Continuation:
1 𝑧 𝑥+𝑧
Evaluate −1 0 𝑥−𝑧 𝑥 + 𝑦 + 𝑧 𝑑𝑥𝑑𝑦𝑑𝑧
Output: q = -1.1102e-16
Evaluation of Triple Integral Continuation:
𝑙𝑜𝑔2 𝑥 𝑥+𝑙𝑜𝑔𝑦
Evaluate 0 0 0 𝑒 𝑥+𝑦+𝑧 𝑑𝑥𝑑𝑦𝑑𝑧. Given, log2=0.3010
Output: q = -0.0533
THANK YOU
ENGINEERING MATHEMATICS-II
MATLAB
▪ MATLAB has the rand and randn functions for generating uniformly and
normally distributed random variables, respectively.
▪ X=rand returns a random scalar drawn from the uniform distribution on the
open interval (0,1)
▪ X=rand(n) returns an 𝑛 -by- 𝑛 matrix of uniformly distributed random
numbers on the open interval (0,1)
▪ X=randn returns a random scalar drawn from the standard normal
distribution
▪ X=randn(n) returns an 𝑛 -by-𝑛 matrix of normally distributed random
numbers
Examples
>> X=rand(1,4)
>> X=rand(1,4)
>> X=rand(2,2)
X = 0.1576 0.9572
0.9706 0.4854
Examples
>> X=rand(3)
>> X=randn(1,2)
X = 1.0933 1.1093
>> X=randn(3)
Bernoulli Distribution:
Use binopdf to compute the pdf of the Bernoulli distribution with a probability
success of 0.90.
>> p = 0.90;
>> x = 0:1;
>> y=binopdf(0:1,1,p)
y = 0.1000 0.9000
Bernoulli Distribution PDF
>> figure
bar(x,y,1)
xlabel('Observation’)
ylabel('Probability')
Bernoulli Distribution CDF
>> p = 0.90;
>> y = binocdf(-1:2,1,p)
Plotting of CDF:
>> figure
stairs(-1:2,y)
xlabel('Observation’)
ylabel('Cumulative Probability')
Binomial Distribution
Compute the PDF of the binomial distribution with 15 trails and the probability
of success 0.5.
>> x = 0:15
>> y = binopdf(x,15,0.5)
Compute the CDF of the binomial distribution with 15 trails and the probability
of success 0.5.
>> x = 0:15
>> y = binopdf(x,15,0.5)
Plotting of CDF:
>> figure
stairs(x,y)
xlabel('Observation’)
ylabel('Cumulative Probability')
Poisson Distribution and its PDF
>> x = 0:15
>> y = poisspdf(x,5)
Plotting of CDF:
>> figure
stairs(x,y)
xlabel('Observation’)
ylabel('Cumulative Probability')
Normal Distribution and its PDF
Plotting of PDF
>> plot(x,y)
Normal Distribution CDF
>> pd = makedist('Normal’)
pd = NormalDistribution
Normal distribution
mu = 0
sigma = 1
>> x = -3:.1:3;
>> p = cdf(pd,x)
Plotting of CDF
>> plot(x,p)
THANK YOU
ENGINEERING MATHEMATICS-II
MATLAB
>> syms t s a b
>> f= exp(a*t)*sin(b*t);
>> laplace(f, t, s)
Output: b/(b^2+(a-s)^2)
Examples
>> T= laplace(k*exp(-a*t)*cos(w*t), t, s)
>> simplify(T)
Note that:
Heaviside’s Functions also called Unit step function
It is denoted as 𝑢 𝑡 − 𝑎 𝑜𝑟 𝐻 𝑡 − 𝑎
Using unit step function, find the Laplace transform of 𝑓 𝑡 = 𝑡 − 𝑎
>> syms t s
>> syms a positive
>> laplace(heaviside(t-a),t,s)
Output: exp(-a*s)/s
Laplace transformation of Dirac Delta and Heaviside’s Functions
Using unit step function, find the Laplace transform of 𝑓 𝑡 = 𝑠𝑖𝑛2𝑡 − 𝑠𝑖𝑛3𝑡
>> syms t s;
>> laplace(heaviside(t)*sin(2*t)-sin(3*t))
Note that:
It is denoted as 𝛿(𝑡 − 𝑎)
Using unit impulse function, find the Laplace transform of 𝑓 𝑡 = 𝑡 − 𝑎
>> syms t s
>> laplace(dirac(t-a), t, s)
Output: exp(-a*s)
Dirac delta Functions
>> syms t s
>> laplace(dirac(t-1),t,s)
Output: exp(-s)
Dirac delta Functions
𝜋
Using unit impulse function, find the Laplace transform of 𝑓 𝑡 = 𝑡 −
4
>> syms t s
>> syms 𝑎 positive
>> laplace(dirac(t-pi/4),t,s)
Output: exp(-(pi*s)/4)
Plot a 3D graph of a given Laplace Transform of a function:
>> [x,y]=meshgrid(-10:0.1:10);
>> s=x+i*y;
>> z= abs((s+3)./((s+3).^2+25));
>> mesh(x,y,z)
Plot a 3D graph of a given Laplace Transform of a function continu:
Output:
Inverse Laplace Transform
The command for ILT ilaplace.
𝑠−5
Find the inverse Laplace transforms of 𝐹 𝑠 =
𝑠(𝑠+2)2
>> syms t s
>> F= (s-5) / (s*(s+2)^2);
>> ilaplace (F)
>> simplify(ans)
Output: -5/4+ 7/2* t*exp(-2*t)+5/4*exp(-2*t)
Inverse Laplace Transform
10(𝑠+2)
Find the inverse Laplace transforms of 𝐹(𝑠) =
𝑠(𝑠 2 +4𝑠+5)
>> syms t s
>> F = 10*(s+2)/(s*(s^2+4*s+5));
>> ilaplace(F)
Output: -4*exp(-2*t)*cos(t)+2*exp(-2*t)*sin(t)+4
Inverse Laplace Transform
4
𝑠
Find the inverse Laplace transforms of 𝐹 𝑠 =
4+𝑠 2
>> syms s t
>> f=(4/s)/(4+s^2);
>> ilaplace(f,s,t)
Output: 1-cos2*t
Inverse Laplace Transform
𝑎
Find the inverse Laplace transforms of 𝐹 𝑠 =
(𝑠 2 −𝑎2 )
>> syms s a t
>> f= a/(s^2-a^2);
>> r=ilaplace(f,s,t)
>> syms s t
>> f= (s+2)/(s^2-4*s+13);
>> r=ilaplace(f,s,t)
This approach works only for linear differential equations with constant
coefficients.
>> syms s t Y
Define the right-hand side function and find its Laplace transform:
>> f = exp(-t)
>> Y1=s*Y-4
Solution of ODE’s using Laplace transform
>> Y2=s*Y1-5
Set the Laplace transform of the left hand side minus the right hand side to
zero and solve for Y:
1 𝛼+2𝜋
where, 𝑎0 = 𝛼 𝑓 𝑥 𝑑𝑥;
𝜋
1 𝛼+2𝜋
𝑎𝑘 = න 𝑓 𝑥 𝑐𝑜𝑠𝑘𝑥𝑑𝑥; 𝑘 = 1,2, …
𝜋 𝛼
1 𝛼+2𝜋
𝑏𝑘 = න 𝑓 𝑥 𝑠𝑖𝑛𝑘𝑥𝑑𝑥; 𝑘 = 1,2, …
𝜋 𝛼
Problem
>> B=simplify(b(f,x,k,pi))
B = (2*(sin(k*pi) - k*pi*cos(k*pi)))/(k^2*pi)
>> pretty(B)
The nth partial sum
>> fs = @(f,x,n,pi) a(f,x,0,pi)/2 + ...
symsum(a(f,x,k,pi)*cos(k*x) + b(f,x,k,pi)*sin(k*x),k,pi,n);
>> pretty(fs(f,x,3,pi))
Problem
>> B=simplify(b(f,x,k,pi))
B=0
>> fs = @(f,x,n,pi) a(f,x,0,pi)/2 + ...
symsum(a(f,x,k,pi)*cos(k*x) + b(f,x,k,pi)*sin(k*x),k,pi,n);
>> pretty(fs(f,x,2,pi))
Fourier Series over an arbitrary interval
The Fourier series expansion of a function 𝑓(𝑥) over the interval −𝐿, 𝐿 is
𝑎0 𝑘𝜋𝑥 𝑘𝜋𝑥
given by 𝑓 𝑥 = + σ∞
𝑛=1 𝑎𝑘 𝑐𝑜𝑠 + 𝑏𝑘 𝑠𝑖𝑛 , where
2 𝐿 𝐿
1 𝐿
𝑎0 = −𝐿 𝑓 𝑥 𝑑𝑥;
𝐿
1 𝐿 𝑘𝜋𝑥
𝑎𝑘 = න 𝑓 𝑥 𝑐𝑜𝑠 𝑑𝑥; 𝑘 = 1,2, …
𝐿 −𝐿 𝐿
1 𝐿 𝑘𝜋𝑥
𝑏𝑘 = න 𝑓 𝑥 𝑠𝑖𝑛 𝑑𝑥; 𝑘 = 1,2, …
𝐿 −𝐿 𝐿
Problem
>> B=simplify(b(f,x,k,1))
B=0
>> fs = @(f,x,n,L) a(f,x,0,L)/2 + ...
symsum(a(f,x,k,L)*cos(k*pi*x/L) + b(f,x,k,L)*sin(k*pi*x/L),k,1,n);
>> pretty(fs(f,x,2,1))
Here are the plots of the partial sums for n=2,5,10. The plot also shows
the function 𝑓.
>> ezplot(fs(f,x,2,1),-1,1)
>> hold on
>> ezplot(f,-1,1)
>> hold off
>> title('Partial sum with n=2')
>> ezplot(fs(f,x,5,1),-1,1)
>> hold on
>> ezplot(f,-1,1)
>> hold off
>> title('Partial sum with n=5')
>> ezplot(fs(f,x,10,1),-1,1)
>> hold on
>> ezplot(f,-1,1)
>> hold off
>> title('Partial sum with n=10')
THANK YOU
ENGINEERING MATHEMATICS-II
MATLAB
>> clf
>> clear all
>> f=-4:0.01:4;
>> syms t
>> F=int(1*exp(-i*2*pi*f*t),t,-0.5,0.5);
>> F1=double(F);
>> subplot(211)
>> plot(f,abs(F1))
>> subplot(212)
>> plot(f,angle(F1))
>> grid on
Plotting of Fourier transform of 𝑓 𝑡
Plotting of Fourier transform of 𝑓 𝑡 = 2𝑡
>> f=-4:0.01:4;
>> syms t
>> F=int((2*t)*exp(-i*2*pi*f*t),t,-0.5,0.5);
>> F1=double(F);
>> subplot(211)
>> plot(f,abs(F1))
>> subplot(212)
>> plot(f,angle(F1))
>> grid on
Plotting of Fourier transform of 𝑓 𝑡
Plotting of Fourier transform of 𝑓 𝑡 = 𝑒 −𝑡
>> f=-4:0.01:4;
>> syms t
>> F=int(exp(-t)*exp(-i*2*pi*f*t),t,-0.5,0.5);
>> F1=double(F);
>> subplot(211)
>> plot(f,abs(F1))
>> subplot(212)
>> plot(f,angle(F1))
>> grid on
Plotting of Fourier transform of 𝑓 𝑡
The command ``fourier`` and ``ifourier``
− 𝑡 2 +𝑥 2
Compute the Fourier transform of 𝑓 𝑡 = 𝑒
>> syms t x
>> f = exp(-t^2-x^2);
>> fourier(f)
ans = pi^(1/2)*exp(- t^2 - w^2/4)
Problems:
−𝑡 2
Compute the Fourier transform of 𝑓 𝑡 = 𝑡𝑒
>> syms t x
>> f = t*exp(-t^2);
>> fourier(f)
ans = -(w*pi^(1/2)*exp(-w^2/4)*1i)/2
Problems:
𝜔2
−
Compute the inverse Fourier transform of 𝑒 4
>> syms w
>> F = exp(-w^2/4);
>> ifourier(F)
ans = exp(-x^2)/pi^(1/2)
Note: By default, the inverse transform is in terms of 𝑥. To get the result in
terms of 𝑡, we need to use the following code:
>> syms w t
>> F = exp(-w^2/4);
>> ifourier(F, t). Here, the output is: ans=exp(-t^2)/pi^(1/2)
Problems:
− 𝜔 2 +𝑎 2
Compute the Fourier transform of 𝑓 𝑡 = 𝑒
>> syms a w
>> F = exp(-w^2-a^2);
>> ifourier(F)
ans = exp(- a^2 - x^2/4)/(2*pi^(1/2))
THANK YOU