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

조회 수: 20 (최근 30일)
annona
annona 2012년 9월 6일
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.
  댓글 수: 4
annona
annona 2012년 9월 8일
Thank you very much.
I have seen many questions like mine but I did not see any clear answer like yours.
using the 'strrep' does not convert F1 to a function handle suitable to be passed to 'fsolve' so I have to do some manual modification while running the program but it is much better than before.
Star Strider
Star Strider 2012년 9월 8일
편집: Star Strider 2012년 9월 8일
It is my pleasure to help!
I would appreciate it if you would accept my answer. That way it will be easier for others to find, who will search for it as you did.

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

추가 답변 (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.

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

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by