How can I convert a function with multivariable input to a function with single vector input?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi, I want to use fmincon, and fmincon can deal with only function with single vector input.
When my formula is as below, it works well.
fun = @(x) x(1)^2+x(2)^2;
fmincon(fun,[1,1])
However, if the function form has multiple input as below, it does not work.
Is there any way to convert the fun2 below to the function with single vector input as the example above?
fun2 = @(x,y) x^2+y^2;
fmincon(fun2,[1,1]) % this does not work
0 件のコメント
回答 (1 件)
Star Strider
2022 年 3 月 31 日
Do essentially the same thing, creating it with another anonymous function around it —
fun2 = @(x,y) x^2+y^2;
fmincon(@(b)fun2(b(1),b(2)),[1,1]) % now it works!
.
2 件のコメント
Star Strider
2022 年 3 月 31 日
My pleasure!
I am not aware of any. The ‘shell’ anonymous function would have to be writen specifically for each funciton.
For example, if the function needed to have extra parameters passed to it:
fun2 = @(x,y,a,b) a*x^2+b*y^2;
a = 3;
b = 11;
fmincon(@(b)norm(fun2(b(1),b(2),a,b)),[1,1]) % now it works!
There could not possibly be a general rule that would apply to all functions.
.
参考
カテゴリ
Help Center および File Exchange で Symbolic Math Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!