www.pudn.com > Fourier_function.zip > Fourier_function.m, change:2012-12-08,size:2188b


%% Vardan Semerjyan Oct 17 2011 
% Plot the Fourier function from 1.108 
clear all; 
clc; 
t = 0:0.01:2;       %Time  
A = 2;              %Value for A 
w = 2*pi;           %f = 1 Hz         
x = A/pi + A/2*sin(w *t); 
n = 4; 
for i = 2:2:n 
    x = x - 2*A/pi *cos(i*w*t)/(i^2 - 1);  
end 
subplot(2,1,1); 
plot(t,x); 
hold on; 
title('Function and its Furier Series Representation'); 
ylabel('function'); 
tau = 1; 
x_orig  = A *sin(2*pi*t); 
x_size =  size(x_orig);  
for j =1:x_size(2); 
    if(x_orig(j) < 0) 
        x_orig(j) =0; 
    end 
end 
plot(t,x_orig,'r'); 
 
subplot(2,1,2); 
plot(t,x_orig - x ); 
xlabel('time'); 
ylabel('error'); 
hold off; 
 
%% Plot the frequency spectrum of problem 1.108 
figure(2); 
b(1) = A/2; 
a(1) = 0; 
omega(1) = 1; 
for i = 2:16 
    omega(i) = i; 
    b(i) = 0; 
    if  rem(i,2) == 0 
        a(i) = 2*A/pi*1/(i^2 - 1); 
    else 
        a(i) = 0; 
    end 
     
end 
d = (a.^2 + b.^2).^0.5; 
fin = atan(b./a); 
hold on; 
subplot(2,1,1); 
bar(omega,d,'b'); 
title('Frequency spectrum of a function of time'); 
xlabel('Frequency n*omega'); 
ylabel('Dn'); 
subplot(2,1,2); 
bar(omega,fin,'b'); 
xlabel('Frequency n*omega'); 
ylabel('fi_n'); 
 
%% Plot the Fourier function from 1.111 
clear all; 
clc; 
figure(3); 
t = 0:0.01:1;       %Time  
A = 2;              %Value for A 
w = 2*pi;           %f = 1 Hz         
x = A/2;  
n = 16; 
for i = 1:n 
    x = x + A/pi *sin(i*w*t)/i; 
end 
t3 =[t,1+t,2+t]; 
x3 =[x,x,x]; 
subplot(2,1,1); 
plot(t3,x3); 
hold on; 
title('Function and its Furier Series Representation'); 
ylabel('function'); 
tau = 1; 
x_orig  = A *(1-t/tau); 
x_size =  size(x_orig);  
x_orig3 = [x_orig ,x_orig,x_orig]; 
plot(t3,x_orig3,'r'); 
subplot(2,1,2); 
plot(t3,x_orig3 - x3 ); 
xlabel('time[s]'); 
ylabel('error'); 
hold off; 
 
%% Plot the frequency spectrum of problem 1.111 
figure(2); 
n = 1:1:20; 
w =2*pi;    %f = 1 Hz 
b = A./(n*pi); 
a = 0; 
d = (a.^2 + b.^2).^0.5; 
fin = atan(b./a); 
hold on; 
subplot(2,1,1); 
bar(n,d,'b'); 
title('Frequency spectrum of a function of time'); 
xlabel('Frequency n*omega'); 
ylabel('Dn'); 
subplot(2,1,2); 
bar(n,fin,'b'); 
xlabel('Frequency n*omega'); 
ylabel('fi_n');