how to replace a symbolic variable with a real one after computation?

I have supposed to variable as symbolic and after some calculation I want to remove those symbolic variables and replace them with other real variable to that the output can be calculated for those real variables. Attached is the problem:
Here after syms I3 and I4. I applied some operators to a which is composed of symbolic varaibles I3 and I4. to the end I get symbolic variable d which is like "xyz * I3 - abc" here I want to create a new variable z which is composed of d but the it shouldn't be symbolic and I3 has to be replaced by I5(real variable). I have tried to alot but couldn't find the accurate solution to my problem. Many thanks

 Accepted Answer

Use subs or symfun objects and covert to double are two options
syms I3 I4
d = I3 + I4;
double(subs(d,[I3 I4],[1.1 2.2]))
ans = 3.3000
d(I3,I4) = I3 + I4;
double(d(1.1,2.2))
ans = 3.3000

More Answers (1)

Perhaps straight conversion of the symbolic expressions to functions (function-handles, dynamical functions) with the matlabFunction-function:
d_fcn = matlabFunction(d)
Since you presented your code as an image it is impossible for me to check that this actually returns a function with all the input-parameters you want - without typing in your code from that image, and that's not going to happen. Hopefully you can adapt or use this solution. That function will allow you to plug in whatever values you want for the different input parameters.
HTH

5 Comments

matlabFunction is usally a good option, but because it uses all floating point I keep in mind that there can be cases where it's susceptible to round-off that can be mitigated by staying in the sym world as long as possible and converting to double at the end
syms x y real
d = (x + y)/x - x;
df = matlabFunction(d)
df = function_handle with value:
@(x,y)-x+(x+y)./x
df (1,eps/4)
ans = 0
Doing it all in sym and then converting to double at the end
double(subs(d,[x y],[1 eps/4]))
ans = 5.5511e-17
That is a fair point, but as far as I understand limited to example-sized problems.
I think I've been bit by that or something similar, in actual work, maybe for overflow or something, I don't recall, so just thought I'd mention it. I use matlabFunction quite a bit, oftentimes because it's a lot faster than staying in sym world.
So something like: damned if you do or cursed if you don't? Drowning in a half-full glass, or dehydration in a half-empty glass?
Well, I wouldn't go so far as to being damned and cursed. How could such things ever occur when using Matlab? :)

Sign in to comment.

Asked:

on 13 Oct 2022

Commented:

on 13 Oct 2022

Community Treasure Hunt

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

Start Hunting!