Using function of multiple variables in fmincon

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.

 채택된 답변

Matt J
Matt J 2018년 9월 17일
편집: Matt J 2018년 9월 17일

1 개 추천

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

Shrobona Bagchi
Shrobona Bagchi 2018년 9월 17일
편집: Matt J 2018년 9월 17일
Hi Matt J, many thanks for your input.Actually I need the function in more general setting like:
function [y]=myfun(r,t,m,n,p)
syms r,t,p;
y=limit(diff(((p.^m).*exp(1+r.*cos(t)),r,n)),p,1);
end
So it will be better if we could keep it in that form otherwise depending on 'm', the expression becomes quite big and cumbersome to write.
Will the method suggested by you work in this case too.
Thanks!
Matt J
Matt J 2018년 9월 17일
편집: Matt J 2018년 9월 17일
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.
Thank you for link on additional parameters. It solved the problem.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2018년 9월 17일

댓글:

2018년 9월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by