Automatic substitution of a symbolic variable with a numerical value

21 visualizaciones (últimos 30 días)
IN KWON PARK
IN KWON PARK el 6 de Oct. de 2023
Editada: Dyuman Joshi el 9 de Oct. de 2023
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.

Respuestas (2)

Torsten
Torsten el 6 de Oct. de 2023
Editada: Torsten el 6 de Oct. de 2023
The a in the symbolic equation
exp = str2sym('c = a + b');
is not associated with the
a = 1
in the first line.
This would work, however:
syms a b
expr = str2sym('c == a + b');
double(subs(rhs(expr),[a b],[1 2]))
ans = 3
  1 comentario
IN KWON PARK
IN KWON PARK el 6 de Oct. de 2023
Yes, your solution would work. The expression with the label 'no error' in my code tried to show that. Meanwhile, my question is when MATLAB symbolics can take in the pre-existing numerical value, 'a = 1' in my case, into the corresponding symbol inside another symbolic arithmatics definition, 'a' in my question. In my attempt that was not possible obviously. Thus I wanted to check whether that is really the case or perhaps there might be a way to do that.

Iniciar sesión para comentar.


Dyuman Joshi
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.
Also, it's not a good idea to use built-in functions as variables names - exp in your case.
% 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
Your symbolic variables are: exp1 exp2
whos
Name Size Bytes Class Attributes cmdout 1x33 66 char exp1 1x1 8 sym exp2 1x1 8 sym
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])
xx1 = 
%Can use double() on a symbolic number
xx2 = double(subs(rhs(exp1), [a b], [2.0 2.0]))
xx2 = 4
xx3 = double(subs(rhs(exp2), [aa b], [2.0 2.0]))
xx3 = 4
Solution - Define a as a symbolic variable and do not over-write it.
  6 comentarios
Walter Roberson
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
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.
"Or you may want to show me how to 'define' an expression, instead of using 'str2sym'."
syms a b c
eqn = c == a+b
eqn = 
xx = double(subs(rhs(eqn),[a b],[1 2]))
xx = 3
"Meanwhile, if 'str2sym' does evaluate an expression,"
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by