Replace many symbolic values with numerical values all at once

5 views (last 30 days)
Hi,
How do I replace many symbolic values with numerical values all at once?
I am aware of subs
clear
syms x y
func=x*y
func = 
xnew=3
xnew = 3
ynew=2
ynew = 2
func=subs(func,[x y],[xnew,ynew])
func = 
6
I am aware of functions with dependent variables
clear
syms x y
func(x,y)=x*y
func(x, y) = 
func=func(3,2)
func = 
6
I am aware of eval which is the best method I have found. I have been told that eval is a bad function and I should not use it because it is slow, but it is very quick to convert a symbolic function into numerical values which is awesome.
clear
syms x y
func=x*y
func = 
x=3
x = 3
y=2
y = 2
func=eval(func)
func = 6
The functions I use are enormously long and depend on ridiculous numbers of variables which makes the func(x,y) syntax very slow to use due to the amount of typing it requires and it also is very cluttered. The eval syntax is very nice for these long equations, but I have been told it is slow to calculate.
What is the correct way to do symbolic to numerical substitutions without having to type ridiculous amounts of variables? If I am wrong and the eval function is fine to use, please correct me and I will use it as my primary method without any guilt.
Thank you,
Luck

Accepted Answer

Steven Lord
Steven Lord on 2 Nov 2022
If you're generating this list of "many symbolic values" from something like a call to solve, call solve with one output (so you get a struct array with one field per variable) then call subs with that struct array as input. If you're generating the list manually you could still create a struct manually and call subs.
syms x y
f = [x^2+y^2==1; x+y == 0.5]
f = 
sol = solve(f, [x y])
sol = struct with fields:
x: [2×1 sym] y: [2×1 sym]
check = subs(f, sol)
check = 
simplify(check) % symtrue means the equation is satisfied
ans = 
possibleSolution = struct('x', 0.25, 'y', 0.25)
possibleSolution = struct with fields:
x: 0.2500 y: 0.2500
checkPossibleSolution = subs(f, possibleSolution) % This is not a solution
checkPossibleSolution = 
  1 Comment
Luck Haviland
Luck Haviland on 9 Nov 2022
Edited: Luck Haviland on 9 Nov 2022
Hi Steven,
Thank you for the interesting solution that is not quite as quick to type as eval, but is computationally faster. I would say, although this is not the perfect solution, it is sufficient.
This solution can also be used even when not using it from a solve function as you can also call your inputs as a struct. Take the following example:
Using the original method of eval that I used to use for comparison:
syms beta alpha gamma I_0 I_2 a A_11 D_11 B_11 m
%calculate the natural frequency of a simply supported laminate plate in
%cylindrical bending.
tic
omega_m=(beta+sqrt((beta^2-4*alpha*gamma)/(2*alpha)))^(1/2)
omega_m = 
alpha=I_0*(I_0+I_2*(m*pi/a)^2)
alpha = 
beta=A_11*(I_0+I_2*(m*pi/a)^2)*(m*pi/a)^2+D_11*I_0*(m*pi/a)^4
beta = 
gamma=D_11*A_11*(m*pi/a)^6-B_11^2*(m*pi/a)^6
gamma = 
%input alpha, beta, and gamma into omega_m
omega_manal=eval(omega_m)
omega_manal = 
toc
Elapsed time is 0.988774 seconds.
Using Steven's suggested method:
clear
syms beta alpha gamma I_0 I_2 a A_11 D_11 B_11 m
tic
omega_m2=(beta+sqrt((beta^2-4*alpha*gamma)/(2*alpha)))^(1/2)
omega_m2 = 
%make a struct
ok.alpha=I_0*(I_0+I_2*(m*pi/a)^2)
ok = struct with fields:
alpha: I_0*(I_0 + (I_2*m^2*pi^2)/a^2)
ok.beta=A_11*(I_0+I_2*(m*pi/a)^2)*(m*pi/a)^2+D_11*I_0*(m*pi/a)^4
ok = struct with fields:
alpha: I_0*(I_0 + (I_2*m^2*pi^2)/a^2) beta: (D_11*I_0*m^4*pi^4)/a^4 + (A_11*m^2*pi^2*(I_0 + (I_2*m^2*pi^2)/a^2))/a^2
ok.gamma=D_11*A_11*(m*pi/a)^6-B_11^2*(m*pi/a)^6
ok = struct with fields:
alpha: I_0*(I_0 + (I_2*m^2*pi^2)/a^2) beta: (D_11*I_0*m^4*pi^4)/a^4 + (A_11*m^2*pi^2*(I_0 + (I_2*m^2*pi^2)/a^2))/a^2 gamma: (A_11*D_11*m^6*pi^6)/a^6 - (B_11^2*m^6*pi^6)/a^6
omega_manal2=subs(omega_m2,ok)
omega_manal2 = 
toc
Elapsed time is 0.583339 seconds.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!