Hello All .I want a solution with a simple explanation

3 views (last 30 days)
Ahmed  Ali
Ahmed Ali on 30 Oct 2021
Commented: William Rose on 17 Nov 2021
  5 Comments

Sign in to comment.

Accepted Answer

William Rose
William Rose on 1 Nov 2021
Since I'm not good at symbolic math in Matlab, I'll give an example of how to do this problem non-symbolically. The code below shows off Matlab's great ability to be smart about adding things up without using for loops.
a0=0.5; A=1; T=2; %mean value; peak2peak amplitude; period
t=(0:.001:2)*T; %time vector for two cycles
b=(2*A/pi)./(1:2:9999); %coefficients for sines (odd only)
w=2*pi*(1:2:9999)/T; %frequencies for sines (odd only)
f=a0-A/2+A*(mod(t,T)<T/2); %exact square wave function
f1=a0+b(1)*sin(w(1)*t); %Fourier series, n=1
f9=a0+b(1:5)*sin(w(1:5)'*t); %Fourier series, n=1:9 odd
f99=a0+b(1:50)*sin(w(1:50)'*t); %Fourier series, n=1:99 odd
f999=a0+b(1:500)*sin(w(1:500)'*t); %Fourier series, n=1:999 odd
f9999=a0+b(1:5000)*sin(w(1:5000)'*t); %Fourier series, n=1:9999 odd
plot(t,f,'-k',t,f1,'-r',t,f9,'-g',t,f99,'-b',t,f999,'-c',t,f9999,'-m');
legend('f(t)','f1','f9','f99','f999','f9999');
Try code above.
  3 Comments
William Rose
William Rose on 1 Nov 2021
@Ahmed Ali , you're welcome.
@Walter Roberson is really good at math and Matlab, so when he gives a hint, as he did in this case, I pay attention.

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 31 Oct 2021
Fixing your syntax, but not attempting to fix the logic.
Question: why do you calculate ao, an, bn, if you are not going to use them?
Note: the values for an and bn are correct for those expressions and that integration interval.
syms t n
T = 2 ;
w0 = 2 * pi / T ;
n = [1, 10, 100, 1000, 10000];
ao = ( 1 / T ) * int ( sym(1) , t , 0,2 )
ao = 
1
an = ( 2 / T ) * int ( sym(1) * cos ( n * w0 * t ) , t , 0,2 )
an = 
bn = ( 2 / T ) * int ( sym(1) * sin ( n * w0 * t ) , t , 0,2 )
bn = 
ft = (0.5 + 2/pi ) * sum ((1./n) .* sin(n*pi*t))
ft = 
fplot(ft, [-10 10])
  4 Comments
Walter Roberson
Walter Roberson on 1 Nov 2021
syms t n
T = 2 ;
w0 = 2 * pi / T ;
n = [1, 10, 100, 1000, 10000];
ao = ( 1 / T ) * int ( sym(1) , t , 0,2 )
ao = 
1
an = ( 2 / T ) * int ( sym(1) * cos ( n * w0 * t ) , t , 0,2 )
an = 
bn = ( 2 / T ) * int ( sym(1) * sin ( n * w0 * t ) , t , 0,2 )
bn = 
ft = (0.5 + 2/pi ) * sum ((1./n) .* sin(n*pi*t))
ft = 
fth = matlabFunction(ft);
fplot(fth, [-10 10])

Sign in to comment.


Ahmed  Ali
Ahmed Ali on 17 Nov 2021
clc
clear all
S=0;
H=0;
N=input('input N=');
for k=1:N
for I=1:40000
f=I/10000;
H(I)=(2/(pi*(2*k-1)))*sin((2*k-1)*pi*f);
end
S=S+H;
end
S=S+0.5;
plot(S)

William Rose
William Rose on 1 Nov 2021
@Walter Roberson said he is correcting your syntax, but not your logic. @Walter Roberson sees that there are logical errors in your code, in addition to syntax errors. Therefore it will not compute the Fourrier series correctly, even when you solve the syntax errors.
I assume the figure you provided represents an infinite pulse train.
Since the function f(t) is odd, except for the mean value, the Fourier series will involve odd terms with sine only, and not any cosine components. In other words, when you solve the integral for the Fourier series coefficients, you will find that all the cosine coefficients are zero, and the sine coefficients are zero when n is even. Therefore only the odd sine coefficients, and the coefficient for the mean value, are non-zero. The Fourier series is therefore
where
(n odd)
where is the mean value, A is the peak-to-peak amplitude, T is the period. In this case, =0.5, A=1, and T=2. Your code should do the summation above, stopping after n=1, 9, 99, 999, 9999.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!