Automatic substitution of a symbolic variable with a numerical value
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello there,
I noticed that a variable inside symbolic expression is not automatically substituted with its numerical value.
Here is an example:
% syms a; % does not affect the error(s)
a = 1.0;
syms aa b c d;
exp = str2sym('c = a + b');
exp2 = str2sym('d = aa + b');
xx = double(subs(rhs(exp), [b], [2.0])); % error 1
% xx = double(subs(rhs(exp), [a b], [2.0 2.0])); % error 2
% xx = double(subs(rhs(exp2), [aa b], [2.0 2.0])); % no error
disp(xx);
if a variable a which is predefined by the value assignment is automatically applied at the expression definition labeled as 'error 1', the expression would yield the result of 3.0. Instead, MATLAB generated error message.
Thus, I'm wondering if a) if there is any mistake I did in this regard b) if there is an available way to do (or enforce) so.
0 comentarios
Respuestas (2)
Dyuman Joshi
el 6 de Oct. de 2023
When you define expressions/variables using str2sym, it does not create the variables defined in the input.
% syms a; % does not affect the error(s)
%a = 1.0;
%syms aa b c d;
exp1 = str2sym('c = a + b');
exp2 = str2sym('d = aa + b');
syms
whos
As you can see, none of a, aa, b, c or d have been defined.
In order to use sub(), the variables to be subsituted must have been defined as symbolic variables.
In your code, aa and b have been defined as such but not a. Thus the substitution for aa and b is succesfull, but it does not take place for a, and that expression does not resolve to a symbolic number. And you can use double() only on a symbolic number.
Now you might ask - Why does defining variable a as a symbolic variables does not affect the error?
Because you over-write it by assinging a numerical value to it.
If you remove that particular line, then you will not get an error -
syms a; % does not affect the error(s)
%a = 1.0;
syms aa b c d;
exp1 = str2sym('c = a + b');
exp2 = str2sym('d = aa + b');
%Can't use double() on a symbolic variable
xx1 = subs(rhs(exp1), [b], [2.0])
%Can use double() on a symbolic number
xx2 = double(subs(rhs(exp1), [a b], [2.0 2.0]))
xx3 = double(subs(rhs(exp2), [aa b], [2.0 2.0]))
Solution - Define a as a symbolic variable and do not over-write it.
6 comentarios
Walter Roberson
el 6 de Oct. de 2023
You are mistaken about the operation of Maple.

a has a pre-defined value, but the result of converting the string "a + 5" to an expression does not involve evaluating a . In Maple, parse("a + 5") gives you the same as if you had entered 'a + 5' -- with delay quotes. If you were to assign the output of parse() to a variable and then change a and then evaluate the variable, you would get the result with the changed a not with the a as-was at the time you did the parse()
Dyuman Joshi
el 7 de Oct. de 2023
Editada: Dyuman Joshi
el 9 de Oct. de 2023
"I used it for define the expression, not for evalaution. "
You are misunderstanding here. It does not matter what you intend to do, or what you expect it to do.
Given an input, str2sym will evaluate it. If the expression evaluates to a symbolic number, the output will be the evaluated symbolic number, if not, it will return the input as it is.
That is how the function is defined and that is how it works.
"the body shows plenty of examples presenting the role of the function call, define an arithmatic expression"
As I said before, if str2sym can not evaluate the input to a symbolic number, it will return the input as it is.
Now, in your case, the input happens to be an arithmatic expression, thus so is the output.
syms a b c
eqn = c == a+b
xx = double(subs(rhs(eqn),[a b],[1 2]))
The use of "if" is misplaced here, as evaluation is what it does.
"my expectation is to bring in the pre-existing numerical value when it evaluates the given expression."
I showed you how to do that.
"Other software already offers such convenience."
Can you show an example?
@Walter Roberson has already (comprehensively) showed you that your example of Maple is wrong.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
