Thanks so much! that helped alot :)so far i got this
x=linspace(0,2*L,500);
y=4/pi;
maxn=500;
for n=0:(2*L)/(500-1):2*L;
y=(4/pi)*(1/n)*sin(n*pi*x/L); (((i tried putting y+ in the beginning but that gave me no graph at all. i dont know why))))
end
if L<=0
error('L must be a positive integer');
end
if n~=n
error('n must be an integer');
end
if ~mod(x,2)
error('n must be odd');
end
plot(x,y)
but i know its wrong because my professor showed us how the graph is supposed to look like and im not getting that :/ Do you know where I messed up? Or any advice at all? thanks!
"Xha" wrote in message <kfupro$bvs$1@newscl01ah.mathworks.com>...
> "laura " <lola_angelgirl@hotmail.com> wrote in message <kful02$qdn$1@newscl01ah.mathworks.com>...
> > 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!
>
> Look up some of these functions in matlab help--
>
> % here are some if statements to catch erroneous input
> if L <= 0
> error('L must be a positive number');
> end
>
> if round(n)~=n
> error('n must be an integer');
>
> if ~mod(x,2)
> error('n must be odd');
> end
>
> %get 500 points
> x = linspace(0,2*L,500);
>
> %you could try a for loop to sum the series
> f_x = zeros(1,numel(x));
>
> for n=1:2:maxN
> f_x = f_x + (4/pi) ?((1/n)sin(npix/L));
> end
>
> all your code should go in a function which accepts L,maxN as an input
> ex..
> function [f_x] = fourier_square_blah(maxN,L)
> %code
>
> end
x=linspace(0,2*L,500);
y=4/pi;
maxn=500;
for n=0:(2*L)/(500-1):2*L;
y=(4/pi)*(1/n)*sin(n*pi*x/L); (((i tried putting y+ in the beginning but that gave me no graph at all. i dont know why))))
end
if L<=0
error('L must be a positive integer');
end
if n~=n
error('n must be an integer');
end
if ~mod(x,2)
error('n must be odd');
end
plot(x,y)
but i know its wrong because my professor showed us how the graph is supposed to look like and im not getting that :/ Do you know where I messed up? Or any advice at all? thanks!
"Xha" wrote in message <kfupro$bvs$1@newscl01ah.mathworks.com>...
> "laura " <lola_angelgirl@hotmail.com> wrote in message <kful02$qdn$1@newscl01ah.mathworks.com>...
> > 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!
>
> Look up some of these functions in matlab help--
>
> % here are some if statements to catch erroneous input
> if L <= 0
> error('L must be a positive number');
> end
>
> if round(n)~=n
> error('n must be an integer');
>
> if ~mod(x,2)
> error('n must be odd');
> end
>
> %get 500 points
> x = linspace(0,2*L,500);
>
> %you could try a for loop to sum the series
> f_x = zeros(1,numel(x));
>
> for n=1:2:maxN
> f_x = f_x + (4/pi) ?((1/n)sin(npix/L));
> end
>
> all your code should go in a function which accepts L,maxN as an input
> ex..
> function [f_x] = fourier_square_blah(maxN,L)
> %code
>
> end