1
$\begingroup$

I have the following code:

Mat = DiagonalMatrix @ {α, β, χ}; 
vec = Diagonal @ Mat; 
Manipulate[
  ListLinePlot[vec], 
  {α, 1, 20}, {β, 1, 20}, {χ, 1, 20}
]

but it doesn't work? How can I solve this problem?

$\endgroup$
2
  • 2
    $\begingroup$ You can move Mat and vec inside or convert them to functions to pass parameter values later. Possible duplicate: mathematica.stackexchange.com/questions/10604/… $\endgroup$
    – Kuba
    Commented Jul 19, 2019 at 11:03
  • $\begingroup$ My original code is more complicated, I can't simply move "{α, β, χ}" in the plot because I have to use "vec" $\endgroup$
    – Gae P
    Commented Jul 19, 2019 at 11:09

2 Answers 2

1
$\begingroup$

If you do not want to move the functions inside, then the alternative is to use substitution.

ClearAll[a0, b0, c0];
Mat = DiagonalMatrix[{a0, b0, c0}];
vec = Diagonal[Mat];

Manipulate[
 ListLinePlot[vec /. {a0 -> a, b0 -> b, c0 -> c}],
 {a, 1, 20},
 {b, 1, 20},
 {c, 1, 20},
 TrackedSymbols :> {a, b, c}
 ]

This was discussed as Kuba mentioned in the linked to question. It is also a good idea to add TrackedSymbols option all the time.

$\endgroup$
1
$\begingroup$
Mat = DiagonalMatrix @ {α, β, χ};
vec = Diagonal @ Mat;

Initialization + Function + Evaluate

You can define a function v using the option Initialization and use it in the first argument of ListLinePlot:

Manipulate[ListLinePlot[v[α, β, χ]], {α, 1,  20}, {β, 1, 20}, {χ, 1, 20}, 
 Initialization -> {v = Function[{α, β, χ}, Evaluate[vec]]}] 

enter image description here

With + Evaluate

You can replace vec with With[{α = α, β = β, χ = χ}, Evaluate @ vec] in the the first argument of ListLinePlot (as an alternative way to force injection of parameter values into vec):

Manipulate[ListLinePlot[With[{α = α, β = β, χ = χ}, Evaluate @ vec]], 
   {α, 1, 20}, {β, 1, 20}, {χ, 1, 20}]

same picture

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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