Azzera filtri
Azzera filtri

Working with function handles to solve optimization problems for multiple equations: Basic questions

8 visualizzazioni (ultimi 30 giorni)
Sorry for the many question lately, but somehow I'm running into a lot of problems trying to use Matlab to solve multiple equations. I'm also not finding much online and in the Matlab help about this. So if someone can recommend a nice tutorial, I'm thankful. So here is my question:
I created a set of equations with variable names that make sense to me:
Eq =
X11 + X12*v1_2 + X13*v1_3 - 1
X12*v2_2 - 2*X11 + X13*v2_3 + 4
3*X11 + X12*v3_2 + X13*v3_3 + 9
X21 - v1_2 + X22*v1_2 + X23*v1_3
X22*v2_2 - 2*v2_2 - 2*X21 + X23*v2_3
3*X21 + 3*v3_2 + X22*v3_2 + X23*v3_3
X31 - v1_3 + X32*v1_2 + X33*v1_3
X32*v2_2 - 2*v2_3 - 2*X31 + X33*v2_3
3*X31 + 3*v3_3 + X32*v3_2 + X33*v3_3
for this I used:
X = sym('X%d%d',3,'real');
V = sym('v',[C,C],'real');
for example which automatically creates numbered symbolic variables. After some operations I get the above equations. So far so good. Now I want to find values for which all the equations are (close to) zero.
Now I can turn these equations into a function handle with matlabFunction which gives me a handle of the form:
However, when trying to use fmincon or other solvers, this does not work, since they seem to only accept function handles of the form
fun=@(x) f(x(1),...,x(n))
(or do they?). Ok, well..then I thought to replace the original variables with x(1),...x(n) and then try to convert it into a function handle.
newVars = sym(['[',sprintf('x(%d) ',1:length(allVars)),']']);
Eq2 = subs(Eq,allVars,newVar);
which gives:
Eq2 =
x(1) + x(2)*x(10) + x(3)*x(11) - 1
x(2)*x(12) - 2*x(1) + x(3)*x(13) + 4
3*x(1) + x(2)*x(14) + x(3)*x(15) + 9
x(4) - x(10) + x(5)*x(10) + x(6)*x(11)
x(5)*x(12) - 2*x(12) - 2*x(4) + x(6)*x(13)
3*x(4) + 3*x(14) + x(5)*x(14) + x(6)*x(15)
x(7) - x(11) + x(8)*x(10) + x(9)*x(11)
x(8)*x(12) - 2*x(13) - 2*x(7) + x(9)*x(13)
3*x(7) + 3*x(15) + x(8)*x(14) + x(9)*x(15)
I was quite happy after this step. But when I then try to convert this into a function handle it doesn't work either, since it gives me something like this:
Along with a warning:
Warning: Function 'x' not verified to be a valid MATLAB function.
Alternatively, I could specify the function handle as file/function like it is mentioned in another post by me, but this also does not seem to be a solution since I have to specify the equations manually then and loops do not work.
In short, I seem to have no idea what I'm doing, so any help appreciated.

Risposta accettata

Alan Weiss
Alan Weiss il 18 Dic 2018
Perhaps consulting the example Symbolic Math Toolbox Calculates Gradients and Hessians or Using Symbolic Math with Optimization Toolbox Solvers will help. Note that those examples use the 'vars' argument to matlabFunction to ensure that the resulting function file accepts a single argument.
I think that you would do well to follow the setup in those examples and define your variables beforehand as a single vector. In other words, define your variables as you have done, then create a new symbolic variable that is a vector or array of your existing variables, maybe
invar = [X(:);V(:)];
Then use something like
fun = matlabFunction(eq,'vars',{invar});
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by