0
$\begingroup$

I want to manipulate multiple plots with recurring functions, take for example:

Manipulate[Grid[{
   {Plot[f1 = a*Sin[k*x], {x, 0, 2 Pi}]},
   {Plot[f2 = a*Cos[2 k*x], {x, 0, 2 Pi}]},
   {Plot[f1 + f2, {x, 0, 2 Pi}]},
   {Plot[f1 + 2 f2, {x, 0, 2 Pi}]},
   {Plot[f1 + 3 f2, {x, 0, 2 Pi}]}}],
 {a, 0, 1}, {k, 0, 1}]

The functions of the last three plots are composed of the two functions in the first two plots (which I have named f1 and f2). But when I evaluate in the above way the last three do not turn out correctly. They only do when I enter f1 and f2 in their complete forms, which I believe causes redundant repetitions of f1 and f2, and takes an increasingly heavy toll whence I plot complicated functions. Is there a more efficient way?

$\endgroup$
  • $\begingroup$ See 11772, 6894 and related questions. Basically, don't assign f1 and f2 inside Plot (or use Evaluate/Evaluated->True if you really need to) $\endgroup$ – Lukas Lang Sep 21 '17 at 10:03
  • $\begingroup$ @Mathe172 OK I'll try that thanks! $\endgroup$ – updraft Sep 21 '17 at 10:12
2
$\begingroup$

Your definition of f1 and f2 is wrong, it calculates once at the first. Let's defines them as functions:

f1 = #1*Sin[#2*#3] &;
f2 = #1*Cos[2 #1*#3] &;

Manipulate[Grid[{
   {Plot[f1[k, a, x], {x, 0, 2 Pi}]},
   {Plot[f2[k, a, x], {x, 0, 2 Pi}]},
   {Plot[f1[k, a, x] + f2[k, a, x], {x, 0, 2 Pi}]},
   {Plot[f1[k, a, x] + 2 f2[k, a, x], {x, 0, 2 Pi}]},
   {Plot[f1[k, a, x] + 3 f2[k, a, x], {x, 0, 2 Pi}]}}], {a, 0, 1}, {k,
   0, 1}]

enter image description here

$\endgroup$
  • $\begingroup$ Alright, thank you! $\endgroup$ – updraft Sep 21 '17 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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