How to supply different syms values for coefficients of an anonymous function?

Hello everyone
I have a function as f(x) = Ax^2 + Bx, in which A and B are defined as syms. Now I want to re-write/obtain the f(x) function with given A and B, however MATLAB does not recognize A and B as numbers, and recognize them as syms still!
Note that I cannot re-write (create a new function handle) f(x) as suggested here, because in my program this f(x) function is a result of a bunch of other function inversions, integrations, and combinations, so I do not want to stop the program and re-write the function handle again, then give A and B.
I appreicate your suggestions.
Regards.

5 Comments

If your functions are all polynominals, it would be much easier to use poly functions and maintain coefficients in a matrix. If you provide some example code, we might be better able to help.
Thank you dear David. Yes, my functions are also in polynomial functions. Here you can see initial functions for T(x) and the final function s_fs which will be a function of A, B, and C as well as fs.
syms x l fs
syms A B C
E=200;
T(x) = A*x
% T(x) = A*x.^2 + B*x
% T(x) = A*x.^3 + B*x.^2 + C*x
f(x) = int(4*T(x),0,l)
l_fs(l) = finverse(f(x),l)
L(fs) = l_fs(fs)
eps = f(x)/E
s_l(l) = eps*int(1,0,l)
s_fs(fs) = s_l(L)
after obtaining s_fs, I want to give A, B, or C different values and solve it for i = [0.1;0.2;0.3].
I appreciate your help in advance.
subs(s_fs(fs), A, 5:8)
for example. s_fs is not anonymous function as you discussed before, it is symbolic function
This simply solves the issue. I didn't know the command subs in MATLAB.
Also we could have a vector of syms and subs with values like:
subs(s_fs(fs), [A,B,C], [1,2,3])
Thank you.
subs(s_fs(fs), {A,B,C}, {newA, newB, newC} )
is what you need sometimes, if the replacements are non-scalar

Sign in to comment.

Answers (1)

This is expected behavior.
a = 2
b = a*5 + 1
a = 3
What value do you expect b to have now? Do you expect b to remember the formula used for its creation and update its value as the variables involved change? Or do you expect that b will use the current values of the variables as of the time b was assigned to and to not update when the variables change?
sym a
b = a*5 + 1
a = 2
What value does b have now? Do you expect (etc)?
When you do
syms a
then a in the MATLAB workspace gets assigned a definite value, with that value being "reference to a variable that lives in the symbolic engine". When you use that a to construct b, the value "reference to a variable that lives in the symbolic engine" is what gets copied, not the name of the matlab variable. So if the matlab variable changes, b does not change and still refers to the symbolic engine variable.
Your mistake was in using @ with an expression that involved symbolic variables. You should use matlabFunction instead, and make sure that you use the 'vars' option. This will give something like
f(x, A, B)
and you then pass in numeric values for A and B

1 Comment

subs(s_fs(fs), A, 5:8)
for example. s_fs is not anonymous function as you discussed before, it is symbolic function

Sign in to comment.

Asked:

on 8 Dec 2020

Commented:

on 11 Dec 2020

Community Treasure Hunt

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

Start Hunting!