Why can't I put unassigned variables in a 2 x 1 matrix?

2 vues (au cours des 30 derniers jours)
Eduardo Gallegos
Eduardo Gallegos le 11 Nov 2022
I want to solve a linear algebra problem.
coefficients = [2 -1; 3 2]
constants = [4; 13]
variables = [x; y]
Error code -----> Unrecognized function or variable 'x'.
Error in untitled3 (line 3)
variables = [x; y]

Réponse acceptée

Star Strider
Star Strider le 11 Nov 2022
One option would be to use the Symbolic Math Toolbox —
syms x y
coefficients = [2 -1; 3 2]
constants = [4; 13]
variables = [x; y]
I have no idea what you want to do after that, however this will allow you to use the ‘variables’ vector as posted.
.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 11 Nov 2022
In MATLAB, at each point in order to use a variable, it must be assigned something at the time it is used. Any variable that is not assigned something at the time it is used is undefined and that will generate an error message.
If you use the Symbolic Toolbox, you can define symbolic variables. At the MATLAB level, when you define, for example,
syms x
then that is equivalent at the MATLAB level to
x = sym('x')
In turn, sym('x') invokes the Symbolic Engine (named MuPAD) to create a symbolic variable inside the symbolic variable, and to return a reference to the variable out of the Symbolic Engine. So the result of sym('x') might be something like
x = struct('digits', 32, 's', '__sym[32134,13534]')
except marked as class sym and with the details a little different from that internally. At the MATLAB level, MATLAB just knows x as having been assigned an object of class sym and the MATLAB level has no idea what the variable contains. If you were to define
y = x + 1
then the MATLAB interface would end up calling into the symbolic engine with commands similar to
feval(symengine, '_plus', '__sym[32134,13534]', 1)
which tells the symbolic engine to look up whatever it has stored for variable __sym[32134,13534] and add 1 to it, and to return a reference to the result, which might be something like
y = struct('digits', 32, 's', '__sym[32181,9315]')
and that would be what the MATLAB level would store. Notice that the MATLAB level has no information about what the symbolic result is: the MATLAB level trusts that whatever the result was, that the Symbolic Engine computed it correctly.
but again at the MATLAB level, the variables x and y would be assigned something (and the MATLAB level does not know what they are assigned in mathematical terms.) When you use the Symbolic Toolbox properly, variables at the MATLAB level are defined and can be processed without MATLAB itself understanding what is happening. Not unlike Searle's Chinese Room https://plato.stanford.edu/entries/chinese-room/
The other way in MATLAB to handle variables that are not assigned at the time you set up the program, is to name the variables as parameters to a true function or to an anonymous function.
y = @(x) x+1
does not require that x is defined at the time you define y : instead y is defined as a function that will accept a single parameter and at the time of execution will add one to whatever parameter it received. The result is nearly exactly the same as if you had written
y = @(varargin) varargin{1}+1
so in nearly all situations, the name of the parameter is unimportant, and the parameter name just acts like a "nick-name" for "whatever value was passed in".
These can be used to set up problems such as
coefficients = [2 -1; 3 2]
constants = [4; 13]
fun = @(xy) coefficients*xy(:) - constants
XY = fsolve(fun, INITIAL_GUESS)
x = XY(1); y = XY(2)
so in at least some situations, you do not need to have the Symbolic Toolbox. In this block of code, at execution time fsolve will pass particular numeric pairs of values to fun, and fun will compute with those particular values, and return the result, and fsolve() will keep changing the values it passes in looking for a vector of values that returns close to [0;0]
Of course, what you would normally do instead is
XY = coefficients \ constants
which would be much more efficient and accurate.

Jan
Jan le 11 Nov 2022
You cannot use not existing variables on the right side of an assigment in Matlab. I cannot imagine what you want to achieve with this expression or how this should be useful to solve a linear system.

Community Treasure Hunt

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

Start Hunting!

Translated by