# Range of plot function does not accept Dynamic output [duplicate]

Below is some code to produce a Fourier series. I want to be able to adjust L to change not only the function, but also the plot limits.

I am getting the error :

"Limiting value -0.536 in {x, -0.536, 0.536} is not a machine-sized real number."

Slider[Dynamic[L]]
Plot[Dynamic[L]/2 +
Sum[(2*Dynamic[L]/(n*Pi)^2)*((-1)^n - 1)*(Cos[
n*Pi*x/Dynamic[L]]), {n, 3}], {x, Minus[Dynamic[L]],
Dynamic[L]}]


I am inexperienced with Mathematica, any help is appreciated

• Welcome to Mathematica! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Oct 7 '15 at 17:10

m = 1; Slider[Dynamic[m]]
Dynamic[
Plot[
m/2 + Sum[
(2*m/(n*Pi)^2)*((-1)^n - 1)*(Cos[n*Pi*x/m])
, {n, 3}]
, {x, -m, m}
]
]


EDIT

Follow @Amir's comments and read the documentation for Dynamic

• Beware of using uppercase variables, as they can collide with built in functions in Mma. – rhermans Oct 7 '15 at 17:15
• Thank you, this works as I had wanted. What exactly is the implication of wrapping all of Plot in Dynamic[]? When does it update exactly? Thank you again for your quick answer – Amir Oct 7 '15 at 17:28
• @Amir see the docs on Dynamic[ ] under Examples\Scope\Placing Dynamic – Dr. belisarius Oct 7 '15 at 17:39
• @Amir in short, the problem is, that Dynamic[x] is not internally considered a number, even if x is a number. It is closer to a "display form". Wrapping Plot in Dynamic let's the entire plot be dynamically updated, while Plot itself needs numerical arguments, which Dynamic things are not. It's like doing a = ... //MatrixForm; b = ...//MatrixForm; a.b (* Error *) Instead one does (a = ...)//MatrixForm. But Dynamic is much much more than a comment can contain, so please do read up on it. – LLlAMnYP Oct 8 '15 at 6:25

Another approach is to use Manipulate. It is designed to handle the problem you have presented and takes care of all that Dynamic stuff internally.

Manipulate[
Plot[m/2 +
Sum[
(2*m/(n*Pi)^2)*((-1)^n - 1)*(Cos[n*Pi*x/m]), {n, 3}
],
{x, -m, m}
],
{{m, 1}, 0.1, 5.0, Appearance -> "Labeled"}
]