Date: Feb 20, 2013 9:47 PM
Author: Derek Goring
Subject: Re: help with writing a short program using square waves, fourier<br> series expansion
On Tuesday, February 19, 2013 2:37:06 PM UTC+13, laura wrote:

> A square wave of period 2L is a step function which is equal to 1 for x from 0 to L, then is equal to -1 for x from L to 2L.

>

>

>

> A square wave can be approximated by this Fourier Series expansion:

>

> infinity

>

> f(x )=(4/pi) ?((1/n)sin(npix/L))

>

> n=1,2,3,5....

>

> I need to write a program that prompts the user for a maximum value for n and a value for L. Using those values, I have to compute the Fourier Series expansion for a square wave and plot the result.

>

> L must be a positive number, and n must be a positive odd integer. If the user does not provide an acceptable number, you should detect the error, display an error message, and stop your script.

>

> You should evaluate the function for 500 values of x, evenly distributed from 0 to 2L.

>

>

>

> I know this is alot but I have no idea how to even start it! If you can at least start me off that would be great!

>

> I know i need to get an input, detect an error, stop the script, and evenly distrubute 500 points im just not sure how. thanks again!

It's almost impossible to follow this thread because you have top posted.

Put your replies UNDERNEATH.

The first line is correct, but just about everything after that is wrong I'm afraid.

You have defined x as a vector, now y must be a vector also.

Why did you define it as a scalar 4/pi ?

The equation is a summation over n=1,2,3...

yet you are looping over some other complicated variable.

Why not simply:

for n=1:nmx

At each step, you calculate the sine and put it into y, then in the next step you overwrite y. You need to add each to the previous.

y=y + (4/pi) etc

but to do this, you must first define y as a vector filled with zeros:

y=zeros(1,length(x));

Why are you checking the value of L after you've used it?

Shouldn't this go before you use it?