MATLAB function for fourier series

Posted on at


my task is to plot fourier series for a square wave using following function:

    function [t, f_series]= sq_wave(har,tmax)
    f0=1/(2*pi);
    w0=2*pi*f0;
    t=-tmax:0.01:tmax;
    n=1:har;
    a0=1/4;
    an=(1./(n.*pi).*sin(n.*(pi/2)));
    bn=(1./(n.*pi).*-cos(n.*(pi/2))+1);
    y=zeros(har,length(t));
    for n=1:har
    y(n,:)= (an(n).*cos(n.*w0.*t))+(bn(n).*sin(n.*w0.*t));
    end
    f_series = a0+sum(y,1);
    end

but this code is not returning the variable f_series, it gives no variable named f_series in workspace. I am the function like this:

       tmax=2*pi;
       [t1,f1]=sq_wave(200,tmax)
       plot(t1,f1)
       figure
       [t2,f2]=sq_wave(400,tmax)
        plot(t2,f2)

but it gives the same graph for both also I am not allowed to use symbolic variables here so can't use symsum, that's why I can't use square wave function in this code. Please help


TAGS:


About the author

160