2
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$
    – user9660
    Commented Oct 7, 2015 at 17:10

2 Answers 2

4
$\begingroup$
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}
  ]
 ]

Mathematica graphics

EDIT

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

enter image description here

$\endgroup$
4
  • $\begingroup$ Beware of using uppercase variables, as they can collide with built in functions in Mma. $\endgroup$
    – rhermans
    Commented Oct 7, 2015 at 17:15
  • $\begingroup$ 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 $\endgroup$
    – Amir
    Commented Oct 7, 2015 at 17:28
  • $\begingroup$ @Amir see the docs on Dynamic[ ] under Examples\Scope\Placing Dynamic $\endgroup$ Commented Oct 7, 2015 at 17:39
  • 2
    $\begingroup$ @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. $\endgroup$
    – LLlAMnYP
    Commented Oct 8, 2015 at 6:25
6
$\begingroup$

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"}
 ]

Mathematica graphics

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.