Replace many symbolic values with numerical values all at once
2 次查看(过去 30 天)
显示 更早的评论
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
xnew=3
ynew=2
func=subs(func,[x y],[xnew,ynew])
I am aware of functions with dependent variables
clear
syms x y
func(x,y)=x*y
func=func(3,2)
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
x=3
y=2
func=eval(func)
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
0 个评论
采纳的回答
Steven Lord
2022-11-2
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]
sol = solve(f, [x y])
check = subs(f, sol)
simplify(check) % symtrue means the equation is satisfied
possibleSolution = struct('x', 0.25, 'y', 0.25)
checkPossibleSolution = subs(f, possibleSolution) % This is not a solution
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!