Effacer les filtres
Effacer les filtres

Assigning a symbolic value in a vector

10 vues (au cours des 30 derniers jours)
Sebastian Daneli
Sebastian Daneli le 3 Nov 2019
I have a vector with symbolic variables, x=[x1 x2]. Now I need to assign alues to these variables, not to the positions in the matrix.
I want to assign x1 with, say 1, and have x1=1, not x=[1 x2]. So I need to extract the variables from the matrix, and then assign them, it that makes any sense.

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Nov 2019
That is possible, but not recommended.
My experience suggests that it is nearly always a bad idea to assign a definite value to a symbolic variable after the variable has been used in an expression, except possibly when you are going to overwrite that other expression (and everythign derived from it.) There gets to be too much confusion about whether the name should represent the unresolved variable or should represent the replacement. For example,
syms x
y = x^2
x = 1
symvar(y)
will show x, and if you display x you will get 1, but y is not 1^2, it is symbolic-x squared, with there being no connection between the symbolic-x and the x you assigned numeric 1 to.
In nearly every case, it is better to use subs(), such as
syms x
y = x^2
xvals = 1
subs(y, x, xvals)
  2 commentaires
Walter Roberson
Walter Roberson le 3 Nov 2019
subs(Xb, x, [0, 1, 13])
Sebastian Daneli
Sebastian Daneli le 3 Nov 2019
Gonna see if there is a better way to keep track on the vectors in the link you posted

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Tags

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by