Replace many symbolic values with numerical values all at once
조회 수: 6 (최근 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
댓글 수: 1
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!