Instructions For Plotting Fourier Series In Matlab Wolfram Alpha PDF

Title Instructions For Plotting Fourier Series In Matlab Wolfram Alpha
Course Mathematical Methods
Institution Murdoch University
Pages 5
File Size 247.5 KB
File Type PDF
Total Downloads 23
Total Views 132

Summary

Download Instructions For Plotting Fourier Series In Matlab Wolfram Alpha PDF


Description

Plotting functions and Fourier series You can either use matlab (which is commercial software but installed on university computers) or you can use octave (which is a free software and easy to install).

Use of matlab/octave to evaluate Fourier coefficients. If you know the Fourier coefficients for a given function, you can use matlab or octave (a free equivalent of matlab with the same syntax) to evaluate their values. Take example 3 as an example. The Fourier coefficients are

Then you can use this little matlab/octave program to evaluate the first N values of the Fourier coefficients an and bn. a0=1; N=9; ns=zeros(N,1); ans=zeros(N,1); bns=zeros(N,1); for n=1:N ns(n)=n; ans(n)=2*((-1)^n-1)/(n^2*pi^2); bns(n)=-2*(-1)^n/(n*pi); end disp(' n an bn'); disp([ns,ans,bns]); The program is attached as EvaluateFourierCoefficientsExample3.m Enter this program in the ‘Editor’ window of matlab/octave and save as a file. Then run it. The ‘command window’ of matlab/octave should then show you output like this: >> EvaluateFourierCoefficientsExample3 n an bn 1.00000 -0.40528 0.63662

2.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000

0.00000 -0.04503 0.00000 -0.01621 0.00000 -0.00827 0.00000 -0.00500

-0.31831 0.21221 -0.15915 0.12732 -0.10610 0.09095 -0.07958 0.07074

>> These are the values of n, an, bn for the numbers n=1...9. Some comments:  The command zeros creates a column vector of length N where all entries are 0.  [ns,ans,bns] concatenates the column vectors ns,ans,bns into a matrix which ‘disp’ then displays as a table.

Use of matlab/octave to plot Fourier coefficients The way of plotting functions in matlab is to create a vector (say xs) representing the x-values and a vector (say ys) representing the corresponding y-values. Then you can plot these, by plot(xs,ys). So, the question is, how to generate these vectors. Let’s say we want to plot the Fourier series above, from x=-3 to x=+5. The following code (which needs to be used with the above code) shows this: xs=-3:0.01:5;

(1)

ys=zeros(1,length(xs));

(2)

ys=ys+a0/2;

(3)

for n=1:N

(4)

ys=ys+ans(n)*cos(n*pi*xs/2);

(5)

ys=ys+bns(n)*sin(n*pi*xs/2);

(6)

end

(7)

plot(xs,ys,'-')

(8)

Comments: 

The code is provided in file PlotFourierSeriesExample3.m



Line (1) creates a vector of x-values, starting at -3, incrementing by 0.01, and not exceeding 5. Hint: just run this command (without the semicolon ‘;’ in the command window. You’ll see what it produces)



Line (2) creates an empty vector for the y-values, with all values initially set to 0



Line (3) adds the a0 term. Note that this is ‘funny’ matlab syntax. The operation “vector+number” means that you add the number to each term of the vector.



The for loop in line (4) now deals with each term n=1, n=2, n=3, …. and adds both the sin and the cos terms. In each loop, we add the terms an*cos(n*pi*x/2) and bn*sin(n*pi*x/2) to the solution vector (called ‘ys’). Note, again, we are using the vector addition functionality of matlab, that is: xs is the vector of x values. cos(xs) is the vector representing the cos values of each x value in xs.



Line (8) plots the x-values xs against the y-values ys, using a line-style graph (rather than points).

The output of this should be a graph such as this:

Hint: it should be fairly straightforward to also integrate the original function f(t). Simply create a vector xs1 which represents the interval [-2,0] and a vector ys2 which corresponds to the function value (constant to 0) in that interval. Then create vectors xs2 and ys2 for the interval [0,2]. Then use the command plot(xs,ys,xs1,ys1,xs2,ys2) to plot all three.

Use of wolframAlpha to plot piecewise-defined function WolframAlpha allows you to plot piecewiese functions in the following way Plot[Piecewise[{{0,-2...


Similar Free PDFs