how can I change symbols in symbolic equations with x(1), x(2), ...

조회 수: 10 (최근 30일)
annona
annona 2012년 9월 6일
댓글: Star Strider 2025년 3월 14일
Hello all
I have a nonlinear system of equations F1(p,q) = 0, F2(p,q) = 0, with F1 and F2 represented in MATLAB as symbolic expressions. I want to solve these equations using fsolve.
Now the problem is that fsolve() wants the variables to solve for to be in a vector X = [x(1) x(2) ... x(n)],so I should transform p and q into x(1) and x(2) and this transformation is giving me the problem.
As a simple example, suppose my system to be solved is defined by:
syms p q
F = [sin(p)*sin(q); p^2+q^2-1]
how can I transform these equations to something like: F = [sin(x(1)).*sin(x(2)); x(1).^2 + x(2).^2-1]
Note: this is a small example of the problem. The real program has 81 equation in 81 unknowns . So I cannot do this transformation manually and I need something intelligent to do this.
How can I do this transformation Or maybe there's a better approach to deal with the problem (say, directly in the symbolic domain?)I tried solve for my 81 equations but it takes infinite time.
Any help will be appreciated ... thank you!

채택된 답변

Star Strider
Star Strider 2012년 9월 6일
편집: Star Strider 2012년 9월 6일
I suggest this approach:
syms p q
F = [sin(p)*sin(q); p^2+q^2-1]
F0 = subs(F, {'p', 'q'}, {'x(1)', 'x(2)'})
F1 = vectorize(F0)
The results:
F =
sin(p)*sin(q)
p^2 + q^2 - 1
F0 =
sin(x(1))*sin(x(2))
x(1)^2 + x(2)^2 - 1
F1 =
matrix([[sin(x(1)).*sin(x(2))], [x(1).^2 + x(2).^2 - 1]])
It's likely not as neat a solution as you would like because MATLAB insists on the matrix designation in F1. Even so, I definitely suggest using vectorize.
If you want them as functions you can use matlabFunction, but it probably easier to create and edit the functions yourself from the results of vectorize, for example:
F1 = @(x) [[sin(x(1)).*sin(x(2))]; [x(1).^2 + x(2).^2 - 1]];
because matlabFunction interpets the array elements x(n) as functions, creating more problems than it solves. (NOTE that I also inserted the ‘;’ separating the submatrices.) The Symbolic Math Toobox won't do subs on a function created by matlabFunction.
This likely falls short of what you would like to do, but in my experience it is the best the Symbolic Math Toolbox is able to do.
  댓글 수: 6
SSK
SSK 2025년 3월 14일
편집: SSK 2025년 3월 14일
When I tried this with my code is got the follwing error:
Error using sym/subs (line 156)
''Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.''
Then I tried a with a simpler code, still getting the same error.
When I tried using str2sym(x(1)) , it says 'unknown variable x'
Would love some help as I am trying to find some really complex equations and have to run a differnt live script obtain the expressions, paste the output in notepad, replace my variables with x(1) and x(2) and then inser the newly replaced expression in another code where it is needed. And now the expressions are getting so long the I am getting memory error and it's taking forever to even copy paste them.
Code:
syms b c
a = b+c;
F0 = subs(a, {'c', 'b'}, {'x(1)', 'x(2)'})
Star Strider
Star Strider 2025년 3월 14일
@SSK Try this instead —
syms b c
x = sym('x', [2 1]) % <— NEWLY ADDED
x = 
a = b+c;
F0 = subs(a, {c, b}, {x(1), x(2)})
F0 = 
There have been significant changes in MATLAB in the last 12 years.
The Symbolic Math Toolbox has not perrmitted string variables for a while. It requries them in the sym call because ‘x’ has not been defined before then, however the ‘x’ array can be defined in the syms declaration in recent releases.
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Seth DeLand
Seth DeLand 2012년 9월 6일
I believe the matlabFunction function is what you are looking for: http://www.mathworks.com/help/toolbox/symbolic/matlabfunction.html
It allows you to convert symbolic equations to MATLAB functions. In the example you gave above, the syntax would look like:
myFcn = matlabFunction(F,'vars',{[p q]})
This will create a function handle myFcn that can be passed to fsolve. Note that the variables are passed in as a vector [p q]. This results in the function that is created having a single vector input, which is what you're going to want if you're passing it to an optimization solver like fsolve.
  댓글 수: 1
annona
annona 2012년 9월 7일
Thank you very much but my version of matlab does not contain 'matlabFunction'. I guess I will get a latter version within 10 days. If it happens I will definitely try this.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by