Group Abstract Group Abstract

Message Boards Message Boards

Mathematica Sum in a function

GROUPS:
I'm having getting errors with my code an I'm not sure how to fix the problem.

(*Define the even function*)
feven[t_] := 1-t

(*Fourier coefficients*)
a[0] = 2/\ Integrate[feven, {t, 0, 1}];
a[n_] := (4/\) Integrate[feven Cos[n \ t], {t, 0, +\/2}];

(*Fourier approximation function*)
fapp[t_, n_] :=
  a[0] + Sum[a[2 x - 1] Cos[(2 x - 1) \ t], {x, 1, n}];

(*Plot the function over a time interval *)
Plot[{fapp[t, 6], f}, {t, 0, 1}]




I'm getting error functions:
Integrate::ilim: "Invalid integration variable or limit(s) in {0.0000204286,0,1.}."
NIntegrate::itraw: Raw object 0.000020428571428571424` cannot be used as an iterator.

However when I try things like fapp[1,6] I get accurate and real values.  I'm not sure how to fix this problem.
POSTED BY: Leah Peker
Answer
7 months ago
The forum posting process will often mangle code unless you position the cursor in an empty spot and then either click on the <> icon or the "spikey star" icon that are on the right end of the second row of buttons in the post or edit window. This click should result in an empty box that is one line high. Then highlight the contents of your notebook, copy to your clipboard, click inside the one line high empty box and paste. Hopefully that will replace the backslashes in your post with the actual special characters so someone can try to reverse this and execute your code. Without the code in a box with all the characters preserved it is really difficult to try to guess what your code was and try to fix it.
POSTED BY: Bill Simpson
Answer
7 months ago
I came up with some simple modifications to address the error message, and now plots. The reason for the invalid integration limits is that "t" becomes a number downstream in the program, and then the integration does not see it as a variable anymore. 
 ClearAll["Global'*"];
 
 (*Define the even function*)
 feven[tt_] := 1 - tt (* Changed t to tt to avoid t becoming a number when called from downstream. *)
 
 (*Fourier coefficients*)
 a[0] = 2 Integrate[feven[tt], {tt, 0, 1}]; (* Changed t to tt , feven to feven[tt] *)
 a[n_] := (4) Integrate[feven[tt] Cos[n tt], {tt, 0, 2}];(* Changed t to tt, feven to feven[tt] *)
 

(*Fourier approximation function*)
fapp[t_, n_] := a[0] + Sum[a[2 x - 1] Cos[(2 x - 1)  t], {x, 1, n}];

(*Plot the function over a time interval *)
Plot[{fapp[t, 6], f}, {t, 0, 1}]
POSTED BY: Isaac Abraham
Answer
7 months ago