Using function of multiple variables in fmincon

23 visualizaciones (últimos 30 días)
Shrobona Bagchi
Shrobona Bagchi el 17 de Sept. de 2018
Comentada: Shrobona Bagchi el 21 de Sept. de 2018
I define a function in a separate .m file as:
function [y]=myfun(r,t,m,n,p)
syms r,t,p;
y=limit(diff((p.*exp(1+r.*cos(t)),r,n)),p,1);
end
I want to use this function in fmincon to minimize over the variables 'r' and 't'.Comments on this problem are welcome. Thanks in advance.

Respuesta aceptada

Matt J
Matt J el 17 de Sept. de 2018
Editada: Matt J el 17 de Sept. de 2018
You want to re-implement myfun as
function y=myfun(unknowns, n)
r=unknowns(1);
t=unknowns(2);
y = (cos(t).^n) * exp(1+r.*cos(t));
end
and then
fmincon(@(x) myfun(x,n), [r0,t0], ...other arguments...)
  3 comentarios
Matt J
Matt J el 17 de Sept. de 2018
Editada: Matt J el 17 de Sept. de 2018
Following my example or the examples here, you can pass whatever known parameters you want to the function, in addition to the unknowns.
You can also always use matlabFunction to convert a symbolic expression to a non-symbolic Matlab function for use with fmincon, or whatever.
Shrobona Bagchi
Shrobona Bagchi el 21 de Sept. de 2018
Thank you for link on additional parameters. It solved the problem.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming 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