im trying to plot a graph in matlab for a fourier series
f(t) = 1 - t^2
-1
you state that term = cos(pi*x*n)*(2)*((-1)^(n-1))/(pi*n)^2; but x is not defined So Matlab has no idea what to do with it
you probably mean term = cos(pi*t*n)*(2)*((-1)^(n-1))/(pi*n)^2;
oh and t is a vector so t^2 doesn't make any sense
type t.^2 instead to tell Matlab to square each element of this vector separately
i added the '.' and changed the variable to 't' and it now plots a parabola :) the question also says to plot on the same axis when N=6 but when i change the value of N the graph stays the same
that's because the first graph doesn't depend on N
notice how you have sumCos, but it's defined by itself but there's nothing defined for it beforehand
how does sumCos start?
i added some code so now its like this, N=3; t=-1:.01:1; y=1-(t.^2); plot(t,y) axis([-1 1 0 1]) hold on kmax = floor((N-1)/2); sumCos = 0; sumSin = 0; for n=1:N term = cos(pi*t*n)*(2)*((-1)^(n-1))/(pi*n)^2; sumCos = sumCos + term; end f = (2/3) + sumCos; plot(t,f) i dont know what the bit i added means though (sorry im not very good at matlab) they just included that in one of the examples so i thought i should try it
ok, that does compile and run correctly (or at least a plot comes up)
this is what comes up
yeh that is what i got also, should N=6 give a closer approximation?
it looks like it's approaching 1-t^2, but I'm not really familiar with fourier series
thats ok, thank you so much for all of your help :)
np
Join our real-time social learning platform and learn together with your friends!