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

5 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Star Strider
Star Strider il 6 Set 2012
Modificato: Star Strider il 6 Set 2012
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 Commenti
annona
annona il 8 Set 2012
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 il 8 Set 2012
Modificato: Star Strider il 8 Set 2012
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.

Accedi per commentare.

Più risposte (1)

Seth DeLand
Seth DeLand il 6 Set 2012
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 Commento
annona
annona il 7 Set 2012
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.

Accedi per commentare.

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by