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

MATLAB - 2nd Sem

Uploaded by

snigenigmatic972
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

MATLAB - 2nd Sem

Uploaded by

snigenigmatic972
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

ENGINEERING MATHEMATICS-II

MATLAB

Department of Science and Humanities


Finding Jacobian:

If 𝑢 = 𝑥𝑦𝑧, 𝑣 = 𝑦 2 , 𝑤 = 𝑥 + 𝑧, then evaluate the Jacobian J.

>> syms x y z

>>J=jacobian([x*y*z, y^2, x + z], [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

>> J=jacobian([x^2-2*y, x+y+z, x-2*y+3*z], [x, y, z])

>>d = det(J)

Out Put: j =[ 2*x, -2, 0][ 1, 1, 1][ 1, -2, 3]

d =10*x + 4
Finding Jacobian Continuation:

If 𝑢 = 𝑥 2 − 2𝑦 2 , 𝑣 = 2𝑥 2 − 𝑦 2 , where 𝑥 = 𝑟𝑐𝑜𝑠𝜃, 𝑦 = 𝑟𝑠𝑖𝑛𝜃, then


evaluate the Jacobian J.

>> syms x y r theta

>> x=r*cos(theta);

>> y=r*sin(theta);

>> J=jacobian([x^2-2*y^2, 2*x^2-y^2 ], [r, theta])

>> d = det(J)

>> simplify(d)
Finding Jacobian Continuation:

Out Put:

j =[ 2*r*cos(theta)^2 - 4*r*sin(theta)^2, -6*r^2*cos(theta)*sin(theta)]

[ 4*r*cos(theta)^2 - 2*r*sin(theta)^2, -6*r^2*cos(theta)*sin(theta)]

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:

If 𝑢 = 𝑥 2 − 𝑦 2 , 𝑣 = 2𝑥𝑦, where 𝑥 = 𝑟𝑐𝑜𝑠𝜃, 𝑦 = 𝑟𝑠𝑖𝑛𝜃, then evaluate


the Jacobian J.
>>syms x y r theta
>>x=r*cos(theta);
>>y=r*sin(theta);
>>j=jacobian([x^2-y^2, 2*x*y, ], [r, theta])
>>d = det(j)
>>simplify(d)
Finding Jacobian Continuation:
Out Put:
j=
[ 2*r*cos(theta)^2 - 2*r*sin(theta)^2, -4*r^2*cos(theta)*sin(theta)]
[ 4*r*cos(theta)*sin(theta), 2*r^2*cos(theta)^2 - 2*r^2*sin(theta)^2]
d=
4*r^3*cos(theta)^4++4*r^3*sin(theta)^4+8*r^3*cos(theta)^2*sin(theta)^2
ans =4*r^3
Finding Jacobian Continuation:

If x = 𝑢 1 − 𝑣 , 𝑦 = 𝑢𝑣, prove that 𝐽𝐽′ = 1

>> syms u v

>> J=jacobian([u*(1-v), u*v,], [u, v,])

>> inv(J)

>> J*inv(J)

>> det(J*inv(J))
Finding Jacobian Continuation:

Out Put:

J =[ 1 - v, -u] [ v, u]

ans = [ 1, 1] [ -v/u, -(v - 1)/u]

ans = [ 1, 0] [ 0, 1]

ans =1
Evaluation of Double Integral :

1 1−𝑥 1
Evaluate ‫׬‬0 ‫׬‬0 𝑑𝑦𝑑𝑥
𝑥+𝑦 1+𝑥+𝑦 2

>> fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 ); (Here, ‘dot’ is necessary)


>> ymax = @(x) 1 - x;
>> q = integral2(fun,0,1,0,ymax)
Out put:
q = 0.2854
Evaluation of Double Integral Continuation:

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 𝑑𝑦𝑑𝑥 .

>>fun = @(x,y) x.*(x.^2+y.^2);


>>ymax = @(x) x.^2;
>>q = integral2(fun,0,5,0,ymax)

Out put:
q = 1.8880e+04
Evaluation of Double Integral Continuation:
1 𝑥
Evaluate ‫׬‬0 ‫׬‬0 𝑥𝑦𝑑𝑦𝑑𝑥

>>fun = @(x,y) x.* y


>>ymax = @(x) sqrt(x);
>>q = integral2(fun,0,1,0,ymax)

q=
0.1667
Evaluation of Double Integral Continuation:
𝜋Τ 1ൗ 𝑟
2 𝑠𝑖𝑛𝜃+𝑐𝑜𝑠𝜃
Evaluate ‫׬‬0 ‫׬‬0 𝑑𝑟𝑑𝜃
𝑟𝑐𝑜𝑠𝜃+𝑟𝑠𝑖𝑛𝜃((1+𝑟𝑐𝑜𝑠𝜃+𝑟𝑠𝑖𝑛𝜃)2

>> polarfun = @(r, theta)


r./(sqrt(r.*cos(theta)+r.*sin(theta)).*((1+r.*cos(theta)+r.*sin(theta)).^2))
>>rmax = @(theta) 1./(sin(theta) + cos(theta))
>>q = integral2(polarfun,0,pi/2,0,rmax)

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 𝑧 )𝑑𝑧𝑑𝑦𝑑𝑥

>>fun = @(x,y,z) x.*cos(y) + x.^2.*cos(z)


>>xmin = -1;
>>xmax = 1;
>>ymin = @(x)-sqrt(1 - x.^2);
>>ymax = @(x) sqrt(1 - x.^2);
>>zmin = @(x,y)-sqrt(1 - x.^2 - y.^2);
>>zmax = @(x,y) sqrt(1 - x.^2 - y.^2);
>>q = integral3(fun,xmin,xmax,ymin,ymax,zmin,zmax)

Out put:
q = 0.7796
Evaluation of Triple Integral Continuation:
1 𝑧 𝑥+𝑧
Evaluate ‫׬‬−1 ‫׬‬0 ‫𝑥׬‬−𝑧 𝑥 + 𝑦 + 𝑧 𝑑𝑥𝑑𝑦𝑑𝑧

>>fun = @(x,y,z) x+y+z


>>xmin= 0;
>>xmax = @(z) z;
>>ymin = @(x,z) x-z;
>>ymax = @(x,z) x+z;
>>zmin = -1;
>.>zmax = 1;
>>q = integral3(fun,zmin,zmax,xmin,xmax,ymin,ymax)

Output: q = -1.1102e-16
Evaluation of Triple Integral Continuation:
𝑙𝑜𝑔2 𝑥 𝑥+𝑙𝑜𝑔𝑦
Evaluate ‫׬‬0 ‫׬‬0 ‫׬‬0 𝑒 𝑥+𝑦+𝑧 𝑑𝑥𝑑𝑦𝑑𝑧. Given, log2=0.3010

>>fun = @(x,y,z) exp(x+y+z)


>>xmin = 0;
>>xmax = 0.3010;
>>ymin = 0;
>>ymax = @(x) x;
>>zmin = 0;
>>zmax = @(x,y) x+log(y);
q = integral3(fun,xmin,xmax,ymin,ymax,zmin,zmax)

Output: q = -0.0533
THANK YOU
ENGINEERING MATHEMATICS-II
MATLAB

Department of Science and Humanities


Random Number Generation

▪ 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 = 0.8147 0.9058 0.1270 0.9134

>> X=rand(1,4)

X =0.6324 0.0975 0.2785 0.5469

>> X=rand(2,2)

X = 0.1576 0.9572

0.9706 0.4854
Examples

>> X=rand(3)

X = 0.8003 0.9157 0.6557

0.1419 0.7922 0.0357

0.4218 0.9595 0.8491


Examples

>> X=randn(1,2)

X = 1.0933 1.1093

>> X=randn(3)

X = 0.0774 -0.0068 0.3714

-1.2141 1.5326 -0.2256

-1.1135 -0.7697 1.1174


Probability Distribution in MATLAB

Bernoulli Distribution:

▪ The Bernoulli distribution is a discrete probability distribution with only two


possible values for the random variable.

▪ Each instance of an event with a Bernoulli distribution is called a Bernoulli


trial.
▪ Clearly, 𝑋 is a discrete random variable, with probability mass function 𝑝(𝑥)
defined by 𝑝 0 = 𝑃 𝑋 = 0 = 1 − 𝑝
𝑝 1 =𝑃 𝑋=1 =𝑝
𝑝 𝑥 = 0 for any value of 𝑥 other than 0 and 1
Bernoulli Distribution PDF

▪ 𝑏𝑖𝑛𝑜𝑝𝑑𝑓 𝑥, 𝑛, 𝑝 computes the binomial probability density function at


each of the values in 𝑥 using the corresponding number of trails 𝑛 and
probability of success for each trail in 𝑝.
Bernoulli Distribution PDF

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

Now, plot the PDF with bars of width 1.

>> figure

bar(x,y,1)

xlabel('Observation’)

ylabel('Probability')
Bernoulli Distribution CDF

>> p = 0.90;

>> y = binocdf(-1:2,1,p)

y = 0 0.1000 1.0000 1.0000

Plotting of CDF:
>> figure
stairs(-1:2,y)
xlabel('Observation’)
ylabel('Cumulative Probability')
Binomial Distribution

▪ The binomial distribution is a two-parameter family of curves.

▪ The binomial distribution is used to model the total number of successes in


a fixed number of independent trials that have the same probability of
success, such as modeling the probability of a given number of heads in ten
flips of a fair coin.
Binomial Distribution PDF

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)

Plotting of PDF with bars of with 1:


>> figure
bar(x,y,1)
xlabel('Observation’)
ylabel('Probability')
Binomial Distribution CDF

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

▪ The Poisson distribution is a one-parameter family of curves that models


the number of times a random event occurs.

Compute the PDF of the Poisson distribution with parameter 𝜆 = 5.


>> x = 0:15
>> y = poisspdf(x,5)
Plotting of PDF with bars of with 1:
>> figure
bar(x,y,1)
xlabel('Observation’)
ylabel('Probability')
Poisson Distribution CDF

>> x = 0:15
>> y = poisspdf(x,5)

Plotting of CDF:
>> figure
stairs(x,y)
xlabel('Observation’)
ylabel('Cumulative Probability')
Normal Distribution and its PDF

▪ The normal distribution, sometimes called the Gaussian distribution, is a


two-parameter family of curves.
Compute the pdf of a standard normal distribution, with parameters
𝜇 = 0; 𝜎 = 1.
>> x = [-3:.1:3];
>> y = normpdf(x,0,1);

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

Department of Science and Humanities


Laplace Transforms

 In order to find the Laplace transform 𝐹(𝑠) of a function 𝑓(𝑡), we first


need to specify the variables 𝑡 and 𝑠 using 𝑠𝑦𝑚𝑠 𝑠 𝑡
 Define the function 𝑓
 Find the Laplace transform 𝑓 𝑡 by using F = 𝑙𝑎𝑝𝑙𝑎𝑐𝑒 𝑓, 𝑡, 𝑠
 To make the expression more readable one can use the commands,
𝑠𝑖𝑚𝑝𝑙𝑖𝑓𝑦 𝐹
Examples
Find the Laplace transforms of 𝑓 𝑡 = −1.25 + 3.5𝑡𝑒 −3𝑡 + 1.25𝑒 −2𝑡
>> syms t s
>> f= -1.25 + 3.5*t*exp(-3*t)+1.25*exp(-2*t);
>> F=laplace(f, t, s)
Output: F= -5/(4*(s+2)) + 7/(2*(s+2)^2)-5/(4*s)
>> simplify(F)
Output: ans =-(- 2*s^2 + 16*s + 45)/(2*s*(s + 2)*(s + 3)^2)
Examples

Find the Laplace transforms of 𝑓(𝑡) = 𝑒 𝑎𝑡 sin(𝑏𝑡)

>> syms t s a b

>> f= exp(a*t)*sin(b*t);

>> laplace(f, t, s)

Output: b/(b^2+(a-s)^2)
Examples

Find the Laplace transforms of 𝑓(𝑡) = 𝑘𝑒 −𝑎𝑡 cos(𝑤𝑡)


>> syms t s k a w

>> T= laplace(k*exp(-a*t)*cos(w*t), t, s)

Output: T = (k*(a + s))/((a + s)^2 + w^2)

>> simplify(T)

ans = (k*(a + s))/((a + s)^2 + w^2)


Laplace transformation of Dirac Delta and Heaviside’s Functions

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))

Output: 2/(s^2 + 4) - 3/(s^2 + 9)


Dirac delta Functions

Note that:

 Dirac delta function also called Unit impulse function

 It is denoted as 𝛿(𝑡 − 𝑎)
Using unit impulse function, find the Laplace transform of 𝑓 𝑡 = 𝑡 − 𝑎

>> syms t s

>> syms 𝑎 positive

>> laplace(dirac(t-a), t, s)

Output: exp(-a*s)
Dirac delta Functions

Using unit impulse function, find the Laplace transform of 𝑓 𝑡 = (𝑡 − 1)

>> syms t s

>> syms 𝑎 positive

>> 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:

 Let’s say I have a function called 𝑓(𝑡) in time domain as:

𝑓(𝑡) = 𝑒 −3𝑡 cos(5𝑡)

 The Laplace transform of 𝑓(𝑡) becomes: 𝐹(𝑠) = 𝑠 + 3 (𝑠 + 3)2 +25

 We want to plot of F(s) as a surface above the s-plane.

>> [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)

Output: (5*exp(-2*t))/4 + (7*t*exp(-2*t))/2 - 5/4

>> 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)

Ouput: exp(a*t)/2 - exp(-a*t)/2


Inverse Laplace Transform
𝑠+2
Find the inverse Laplace transforms of
𝑠 2 −4𝑠+13

>> syms s t

>> f= (s+2)/(s^2-4*s+13);

>> r=ilaplace(f,s,t)

Output: r = exp(2*t)*(cos(3*t) + (4*sin(3*t))/3)


Solution of ODE’s using Laplace transform

 This approach works only for linear differential equations with constant
coefficients.

 Right-hand side functions which are sums and products of polynomials,


exponential functions, sine and cosine functions, Heaviside(unit step)
functions, Dirac (impulse) “functions”, initial conditions given at t=0 .

 The main advantage is that we can handle right-hand side functions


which are piecewise defined, and which contain Dirac impulse
“functions”.
Solution of ODE’s using Laplace transform

Consider the initial value problem 𝑦” + 3𝑦’ + 2𝑦 = 𝑒 −𝑡 , 𝑦(0) = 4, 𝑦’(0) = 5

Define the necessary symbolic variables:

>> syms s t Y

Define the right-hand side function and find its Laplace transform:

>> f = exp(-t)

>> F= laplace(f, t,s)

Find the Laplace transform of 𝑦’(𝑡): 𝑌1 = 𝑠𝑌 − 𝑦(0).

>> Y1=s*Y-4
Solution of ODE’s using Laplace transform

 Find the Laplace transform of 𝑦”(𝑡): 𝑌2 = 𝑠𝑌1 − 𝑦’(0).

>> Y2=s*Y1-5

 Set the Laplace transform of the left hand side minus the right hand side to
zero and solve for Y:

>> Sol = solve(Y2+3*Y1+2*Y-F, Y)

ans = (4*s + 1/(s + 1) + 17)/(s^2 + 3*s + 2)

 Find the inverse Laplace transform of the solution:


ilaplace(ans, s, t)
ans = 12*exp(-t) - 8*exp(-2*t) + t*exp(-t)
THANK YOU
ENGINEERING MATHEMATICS-II
MATLAB

Department of Science and Humanities


Fourier Series

The Fourier series expansion of a function 𝑓(𝑥) over the interval 𝛼, 𝛼 + 2𝜋


𝑎0
is given by 𝑓 𝑥 = + σ∞
𝑛=1 𝑎𝑘 𝑐𝑜𝑠𝑘𝑥 + 𝑏𝑘 𝑠𝑖𝑛𝑘𝑥 ,
2

1 𝛼+2𝜋
where, 𝑎0 = ‫𝛼׬‬ 𝑓 𝑥 𝑑𝑥;
𝜋

1 𝛼+2𝜋
𝑎𝑘 = න 𝑓 𝑥 𝑐𝑜𝑠𝑘𝑥𝑑𝑥; 𝑘 = 1,2, …
𝜋 𝛼

1 𝛼+2𝜋
𝑏𝑘 = න 𝑓 𝑥 𝑠𝑖𝑛𝑘𝑥𝑑𝑥; 𝑘 = 1,2, …
𝜋 𝛼
Problem

Find the Fourier series for the function 𝑓 𝑥 = 𝑥 − 𝑥 2 from 𝑥 = −𝜋 to 𝑥 = 𝜋.


>> syms x k n pi
>> evalin(symengine,'assume(k,Type::Integer)'); The `evalin` command tells
MATLAB that k is an integer.
>> a = @(f,x,k,pi) int(f*cos(k*x)/pi,x,-pi,pi); kth Fourier cosine coefficient of 𝑓
>> b = @(f,x,k,pi) int(f*sin(k*x)/pi,x,-pi,pi); kth Fourier sine coefficient of 𝑓
>> f=x-x^2
>> A=simplify(a(f,x,k,pi))
-(2*(k^2*pi^2*sin(k*pi) - 2*sin(k*pi) + 2*k*pi*cos(k*pi)))/(k^3*pi)
>> pretty(A)

>> 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

Find the Fourier series for the function 𝑓 𝑥 = 𝑥 2 from 𝑥 = −𝜋 to 𝑥 = 𝜋.


>> syms x k n pi
>> evalin(symengine,'assume(k,Type::Integer)');
>> a = @(f,x,k,pi) int(f*cos(k*x)/pi,x,-pi,pi);
>> b = @(f,x,k,pi) int(f*sin(k*x)/pi,x,-pi,pi);
>> f=x^2
>> A=simplify(a(f,x,k,pi))
(2*(k^2*pi^2*sin(k*pi) - 2*sin(k*pi) + 2*k*pi*cos(k*pi)))/(k^3*pi)
>> pretty(A)

>> 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

Find the Fourier series for the function 𝑓 𝑥 = 𝑥 from 𝑥 = −1 to 𝑥 = 1.


>> syms x k n pi
>> evalin(symengine,'assume(k,Type::Integer)');
>> a = @(f,x,k,L) int(f*cos(k*pi*x/L)/L,x,-L,L);
>> b = @(f,x,k,L) int(f*sin(k*pi*x/L)/L,x,-L,L);
>> f=abs(x)
>> A=simplify(a(f,x,k,1))
-(2*(2*sin((k*pi)/2)^2 - k*pi*sin(k*pi)))/(k^2*pi^2)
>> pretty(A)

>> 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

Department of Science and Humanities


Fourier Transform

▪ Fourier transform expresses a signal (or function) 𝑓 𝑡 in the frequency


domain, that is, the signal is described by a function 𝐹 𝜔 . It is written as
𝐹 𝜔 =𝐹 𝑓 𝑡 .
▪ In other words, the Fourier transform of a signal 𝑓 𝑡 is a signal 𝐹 𝜔 . An
𝐹
alternative way of writing this is 𝑓 𝑡 ՜ 𝐹 𝜔 .
▪ The mathematical expression of Fourier transform of 𝑓 𝑡 is

𝐹 𝜔 =𝐹 𝑓 𝑡 = න 𝑓 𝑡 𝑒 −𝑖𝜔𝑡 𝑑𝑡
−∞

▪ It is clear that 𝐹 𝜔 is a complex function of 𝜔.


Fourier Transform
▪ In the case of Fourier transform, 𝑓 𝑡 has to be expressed in the frequency
domain. So, we replace 𝜔 by 2𝜋𝑓. Hence,

𝐹 𝜔 =𝐹 𝑓 𝑡 = න 𝑓 𝑡 𝑒 −𝑖2𝜋𝑓𝑡 𝑑𝑡
−∞

▪ In order to return from frequency domain to time domain, the inverse


Fourier transform is applied. The inverse Fourier transform is defined by
𝐹 −1
𝑓 𝑡 = 𝐹 −1 𝐹 𝜔 ; or alternatively 𝐹 𝜔 𝑓 𝑡 .

▪ The mathematical expression for inverse Fourier transform is



1
𝑓 𝑡 = 𝐹 −1 𝐹 𝜔 = න 𝐹 𝜔 𝑒 𝑖𝜔𝑡 𝑑𝜔
2𝜋 −∞
Plotting of Fourier transform of a function 𝑓 𝑡 =1

>> 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``

▪ The computation of the integral is not always a trivial method.


▪ In MATLAB, there is a possibility to compute the Fourier transform
𝐹 𝜔 of a signal 𝑓 𝑡 using the command ``fourier``.
▪ Correspondingly, the inverse Fourier transform is computed using the
command ``ifourier``.
▪ While executing these two commands, time 𝑡 and frequency 𝜔 must
be declared as symbolic variables.
Problems:

− 𝑡 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

You might also like