1
$\begingroup$

This question already has an answer here:

Edit Edit: It works! as @Kuba has written in This answer, the culprit seems to have been the LocalizeVariables option. The fixed code is below. Many thanks!

Manipulate[Dynamic@Plot[xplus[1, Omegao, t], {t, .01, 1}], 
{k, 100, 10000}, {\[Kappa], 1, 10}, {m, 1, 100},
 LocalizeVariables -> False]

Once again, much thanks for the help!

Edit: I'm going down the list of answers from the duplicate question, and most just plain don't work - I either get an error from mathematica, or an empty plot shows up. There definitely seem to be version differences at play here, considering that question was answered almost six years ago. For example:

Block[] takes only two arguments, and I have multiple variables I want to manipulate. I tried using Block[k = kk1] for example and got an error. Three arguments was also right out

Replacement rules did not work, I think because my variables are dependent variables, and I would think that mathematica didn't attempt to re-evaluate them within the expression

Evaluate@ doesn't work here - it seems that mathematica is ignoring the re-evaluation of the dependent variables

Redefining the functions to include the variables and variables as functions of other variables is non-preferable - this model has ~15 independent variables and ~10 dependent variables, with 3 main functions they slide into. It'd be miraculous if it were possible to avoid having to write that out for every calculation

I've been trying to make a plot of an oscillating function, where the period of this function is a dependent variable, depending on multiple other variables.

I'd like to be able to use Manipulate[] to show how the plot changes when these independent variables change. I'm reluctant to include the variable definitions inside the Manipulate[] statement, as this notebook includes a variety of different plots all based around the same model, and I'd like to define the variables first and avoid clutter if possible. If it is not possible to do it like this, I understand. My current code is:

    Omegao = Sqrt[(k + κ) /m]
    xplus[a_, Omegao_, t_] := Re[a*E^(I*Omegao*t)]

Where the dependent variables here are k, kappa, and m. The plot statement I've come up with so far is:

plot[Omegao_] := Plot[xplus[1, Omegao, t], {t, .01, 1}]

Manipulate[plot[k], {k, 1, 100}]

However, I realized that this was simply manipulating Omegao as k, not with Omegao depending on k as well as the other variables

Similarly,

plot[Omegao_] := Plot[xplus[1, Omegao, t], {t, .01, .05}, PlotRange -> Automatic]

Manipulate[plot[k, κ, m], {k, 100, 10000}, {κ, 1, 100}, {m, 1, 100}]

Just gives three sliders with no plot (as would be expected if Mathematica thought I was just trying to manipulate Omegao directly)

Many Thanks!

$\endgroup$

marked as duplicate by Kuba May 22 '18 at 16:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – Kuba May 24 '18 at 17:53
3
$\begingroup$

The best way to do this is to propagate all of the variables you want to manipulate all the way down as function parameters instead of keeping them as global variables. You also had some other syntax errors in there, such as using i instead of I for the imaginary unit and exp instead of Exp for the exponential function.

The following code should do what you want:

Clear[Omegao, xplus, plot];
Omegao[k_, \[Kappa]_, m_] := Sqrt[(k + \[Kappa])/m];
xplus[a_, t_, k_, \[Kappa]_, m_] := Re[a*Exp[(I*Omegao[k, \[Kappa], m]*t)]];
plot[a_, k_, \[Kappa]_, m_] := Plot[xplus[1, t, k, \[Kappa], m], {t, .01, .05}, 
   PlotRange -> Automatic];
Manipulate[
 plot[1, k, \[Kappa], m], {k, 100, 10000}, {\[Kappa], 1, 100}, {m, 
  1, 100}]
$\endgroup$
  • $\begingroup$ I edited the syntax to make it more readable, sorry about that. Is there any way to keep them as global parameters and instantiate them? As mentioned before, this is a large notebook, and I'd rather like to be able to keep it modularized as much as possible to make it easier to look at and use. This is just the simplest expression in a model that combines 3-6 different functions with at least 10 different dependent variables, so I'd appreciate any possible way to be able to avoid spaghettification of the code $\endgroup$ – Brandon May 22 '18 at 18:12
  • $\begingroup$ @Brandon mathematica.stackexchange.com/q/38416/5478 $\endgroup$ – Kuba May 22 '18 at 18:37
  • $\begingroup$ @Kuba error returned: "xplus[1," cannot be followed by "@Omegao, t]" this is in reference to the code Manipulate[ Dynamic@Plot[ xplus[1, /@ Omegao, t], {t, .01, 1}], {k, 100, 1000}, Initialization :> {Omegao[k_, [Kappa]_, m_] = (k + [Kappa])/m}] . If /@ Omegao is removed, returns a blank plot $\endgroup$ – Brandon May 22 '18 at 18:58
  • $\begingroup$ @Brandon xplus[1, /@ Omegao, t]?? $\endgroup$ – Kuba May 22 '18 at 19:02
  • $\begingroup$ Yes, /@ Omegao, my apologies $\endgroup$ – Brandon May 22 '18 at 19:04

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