Assigning a symbolic value in a vector

20 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 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 Comments
Sebastian Daneli
Sebastian Daneli on 3 Nov 2019
Gonna see if there is a better way to keep track on the vectors in the link you posted

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!