Borrar filtros
Borrar filtros

nonlinear equality constraint with symbolic variables

2 visualizaciones (últimos 30 días)
ali akbar
ali akbar el 17 de Oct. de 2020
Editada: John D'Errico el 17 de Oct. de 2020
Here are some bits of my code which I am trying to run.
sigma1=[1 0;0 1];sigma2=[0 1;1 0];sigma3=-1i*[0 -1i;1i 0];sigma4=[1 0;0 -1];
P={sigma1 sigma2 sigma3 sigma4}
syms t [1 16] real
tt=[t1 0 0 0;t5+1i*t6 t2 0 0;t11+1i*t12 t7+1i*t8 t3 0;t15+1i*t16 t13+1i*t14 t9+1i*t10 t4];
tt2=[t1 0 0 0;t5-1i*t6 t2 0 0;t11-1i*t12 t7-1i*t8 t3 0;t15-1i*t16 t13-1i*t14 t9-1i*t10 t4];
chi1=simplify(transpose(tt2)*tt)
My nonlinear constraint saved in a different m file is
function [c,ceq]=preserve(t)
c=[];
ceq=t1.^2 + t2.^2 + t3.^2 + t4.^2 + t5.^2 + t6.^2 + t7.^2 + t8.^2 + t9.^2 + t10.^2 + t11.^2 + t12.^2 + t13.^2 + t14.^2 + t15.^2 + t16.^2-1;
end
Now when I call this non-linear constraint with my optimization code
problem = createOptimProblem('fmincon','x0',X0,'objective',g,'lb',[],'ub',UB,'nonlcon',@preserve,'options',opts)
The error says
unrecognizable variable t1
n etc. How one would go around this? when I placed t1....t16 in brackets like t(1)....t(16), the code works however the process is quite tedious this way. If any matlab Guru can help?

Respuestas (1)

John D'Errico
John D'Errico el 17 de Oct. de 2020
Editada: John D'Errico el 17 de Oct. de 2020
Numerical optimizers CANNOT use symbolic variables.
That is, this is meaningless when using a tool like fmincon.
syms t [1 16] real
Yes. I know you don't know the value of those variables, so you think they need to be symbolic. Not true.
Some people build their problem using symbolic tools, then convert things into a numerical form, using a tool like matlabFunction. While that can work, it is a terribly slow way to write code. It is a slow working crutch that has no purpose if you understand how to use and write functions. So just learn how to use functions.
For example, suppose I create the function:
fun = @(t) sum(t.^2) - 1;
Do I need to know the value of the variable t when I create that function? OF COURSE NOT! t can be anything. fun operates on the variable t. I can minimize fun. I can look for roots, perhaps. Whatever. But there is ABSOLUTELY NO NEED that t be symbolic.

Categorías

Más información sobre Nonlinear Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by