Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should do it.</p> <pre><code>m = 10; c = 2; k = 5; F = 12; fun[f_?NumericQ] := Module[ {x, t}, First[x /. NDSolve[ {m*x''[t] + c*x'[t] + (k*Sin[2*Pi*f*t])*x[t] == F*Sin[2*Pi*f*t], x[0] == 0, x'[0] == 0}, x, {t, 0, 30} ] ] ] ContourPlot[fun[f][t], {f, 0, 5}, {t, 0, 30}] </code></pre> <p>Important points:</p> <ul> <li><p>The pattern _?NumericQ prevents <code>fun</code> from being evaluated for symbolc arguments (think <code>fun[a]</code>), and causing <code>NDSolve::nlnum</code> errors.</p></li> <li><p>Since <code>NDSolve</code> doesn't appear to localize its function variable (<code>t</code>), we needed to do this manually using <code>Module</code> to prevent conflict between the <code>t</code> used in <code>NDSolve</code> and the one used in <code>ContourPlot</code>. (You could use a differently named variable in <code>ContourPlot</code>, but I think it was important to point out this caveat.)</p></li> </ul> <hr> <p>For a significant speedup in plotting, you can use <a href="http://en.wikipedia.org/wiki/Memoization" rel="nofollow">memoization</a>, as pointed out by Mr. Wizard.</p> <pre><code>Clear[funMemo] (* very important!! *) funMemo[f_?NumericQ] := funMemo[f] = Module[{x, t}, First[x /. NDSolve[{m*x''[t] + c*x'[t] + (k*Sin[2*Pi*f*t])*x[t] == F*Sin[2*Pi*f*t], x[0] == 0, x'[0] == 0}, x, {t, 0, 30}]]] ContourPlot[funMemo[f][t], {f, 0, 5}, {t, 0, 30}] (* much faster than with fun *) </code></pre> <hr> <p>If you're feeling adventurous, and willing to explore Mathematica a bit more deeply, you can further improve this by limiting the amount of memory the cached definitions are allowed to use, <a href="http://web.ift.uib.no/~szhorvat/mmatricks.php" rel="nofollow">as I described here</a>.</p> <p>Let's define a helper function for enabling memoization:</p> <pre><code>SetAttributes[memo, HoldAll] SetAttributes[memoStore, HoldFirst] SetAttributes[memoVals, HoldFirst] memoVals[_] = {}; memoStore[f_, x_] := With[{vals = memoVals[f]}, If[Length[vals] &gt; 100, f /: memoStore[f, First[vals]] =.; memoVals[f] ^= Append[Rest[memoVals[f]], x], memoVals[f] ^= Append[memoVals[f], x]]; f /: memoStore[f, x] = f[x]] memo[f_Symbol][x_?NumericQ] := memoStore[f, x] </code></pre> <p>Then using the original, non-memoized <code>fun</code> function, plot as</p> <pre><code>ContourPlot[memo[fun][f][t], {f, 0, 5}, {t, 0, 30}] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload