0

here is my work. i cannot figure out why when i make n=3 in the Fourier series, maple spits out a division by zero error.. i need some suggestions to get by this. The fourier series for n=1 and n=2 graph perfectly. This only starts to become a problem when n=3 and on. Im thinking it may be an issue with the way i define f(x), but ive tried numerous ways to define it, and i get the same result. New to maple, so go easy on me.

here is the link to some pictures of my work:

page1: http://tinypic.com/r/34iqmfa/6    
page2: http://tinypic.com/r/2rzruvm/6 
user1757273
  • 71
  • 1
  • 2
  • 9

1 Answers1

0

All the forget() calls in the code are just there to try and clear previously stored results, so that the timing comparisons might be more fair. You can remove all the calls to forget() and time(), if you wish.

I figured that you wanted your various ffsN procedures to add from 1 to N, rather than just add the Nth term (from N to N). So I made the last argument of ffs be i=1..N rather than i=N similar to what you had.

You can approach this in a purely exact symbolic way, or you can compute the coefficients numerically. And you can also try to re-use coefficients (exact symbolic, or floats) computed in previous calls that added fewer terms. This makes a measurable performance difference if you are going to compare the plots using "the sum of first 2 terms", "the sum of the first 5 terms", etc. One way to get such re-use is by using a recursive procedure with option remember, which is done below for both exact and float coefficient approaches.

I applied simplify to the exact symbolic coefficients, which collapses them to something more expected. (Think of orthogonality.) I also used value and inert Int * Sum rather than active int and sum or add, but that was just so I could also get float coefficients without doing any symbolic work.

This runs in Maple 15. Shout if it doesn't run, and then please state your version.

restart:
f:=x->sin(3*x)/x:
an:=1/Pi*Int(f(t)*cos(n*t),t=-Pi..Pi):
bn:=1/Pi*Int(f(t)*sin(n*t),t=-Pi..Pi):
ffs:=unapply(Sum(an*cos(n*x)+bn*sin(n*x),n=1..N),[x,N]):

a0:=1/(2*Pi)*int(f(t),t=-Pi..Pi):

a0+simplify(value(ffs(x,1)));
evalf(%);

seq(a0+simplify(value(ffs(x,i))),i=0..3);

seq(evalf(a0+ffs(x,i)),i=0..3);

forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
G5:=[seq(a0+simplify(value(ffs(x,i))),i=0..5)]:
plot(G5,x=-Pi..Pi);
time()-st;

forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
H5:=[seq(evalf(a0+ffs(x,i)),i=0..5)]:
plot(H5,x=-Pi..Pi);
time()-st;

ffsrecursive:=proc(x,N) option remember, system;
  if N<=0 then return evalf(a0);
  else
     return procname(x,N-1)+evalf(eval(an*cos(N*x)+bn*sin(N*x),n=N));
  end if;
end proc:

ffsrecursive(x,3);

ffsrecursiveexact:=proc(x,N) option remember, system;
  if N<=0 then return a0;
  else
     return procname(x,N-1)+simplify(value(eval(an*cos(N*x)+bn*sin(N*x),n=N)));
  end if;
end proc:

ffsrecursiveexact(x,3);

forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
S5exact:=[seq(ffsrecursiveexact(x,i),i=0..5)]:
plot(S5exact,x=-Pi..Pi);
time()-st;

forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
S5:=[seq(ffsrecursive(x,i),i=0..5)]:
plot(S5,x=-Pi..Pi);
time()-st;

# difference w.r.t. f(x)
forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
plot([seq(ffsrecursive(x,i)-f(x),i=15..19)],x=-Pi..Pi);
time()-st;

# difference w.r.t. f(x)
forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
plot([seq(a0+simplify(value(ffs(x,i)))-f(x),i=15..19)],x=-Pi..Pi);
time()-st;

# difference w.r.t. f(x)
forget(evalf): forget(int): forget(`evalf/int`):
forget(Si): forget(Ci): forget(ffsrecursive): forget(value):
st:=time():
H15_19diff:=[seq(a0+evalf(ffs(x,i))-f(x),i=15..19)]:
plot(H15_19diff,x=-Pi..Pi);
time()-st;

[addendum] The submitter asked for just one simple approach. Here is the exact, non-recursive approach (of the 4 above).

restart:

f:=x->sin(3*x)/x:
an:=1/Pi*Int(f(t)*cos(n*t),t=-Pi..Pi):
bn:=1/Pi*Int(f(t)*sin(n*t),t=-Pi..Pi):

ffs:=unapply(Sum(an*cos(n*x)+bn*sin(n*x),n=1..N),[x,N]):

a0:=1/(2*Pi)*int(f(t),t=-Pi..Pi):

G5:=seq(a0+simplify(value(ffs(x,i))),i=0..5);

plot([f(x), G5],x=-Pi..Pi,
     legend=[f(x),"n=0","n=1","n=2","n=3","n=4","n=5"],
     color=[black,gold,cyan,green,blue,magenta,red]);

enter image description here

acer
  • 6,671
  • 15
  • 15
  • Woah. alot of this is over my head with Maple.. i dont know if this makes a huge difference on your suggestion, but i did want the Sums to be only of the nth term i called for. i dont want the sum from 1 to n. i want to graph the fourier series sum when n=1, n=2, n=3, n=4, and n=5. not the fourier series from 1 to 5. im trying to show how the higher the nth term in the sum, the more accurately the fourier series resembles the original function. – user1757273 Feb 05 '13 at 18:54
  • Your original only had 1 term in each so-called sum. They only had the highest term. My code plots each of the sums: a0+first, a0+first+second, etc. Your original just had things like a0+5th term, instead of a0+all_next_five_terms. Your original wasn't actually summing anything, it was just taking single terms. – acer Feb 05 '13 at 20:06
  • Please just try running the code and seeing whether the plots do as you expect, producing a sequence of curves where each is a better approximant (since each sum has one term more than the previous). – acer Feb 05 '13 at 20:08
  • Well since your code is so complex for me, i cannot distinguish what is what. it seems you started the sum from n=0? and i do not know if the original function is on any of the graphs either. The first four graphs look like they may be correct, but the last three are look completely different. also, i do not know why there are so many graphs, i am only looking for one graph with the original function, the fourier series when n=1, n=2, n=3, n=4, and n=5. there should be a much simpler way to do this since this problem was design for users that are not very familiar with Maple. – user1757273 Feb 05 '13 at 20:36
  • I did it in four ways, as an illustration. Exact and floating-point. Recursive, and not. The last three plots show the difference between the fourier series approximants and original f(x), by subtraction. Ie. computed error. The earlier plots do not include f(x) itself, but you could add it in. My series are each of them: a0 plus n terms, for n=0, n=1, n=2,...,n=5. Pick which of the four approach you prefer. Or study the differences if you wish. – acer Feb 05 '13 at 21:44
  • That simple way you added was just what i was looking for. thank you for dumbing it down a bit :) – user1757273 Feb 05 '13 at 22:16