Problem with Minimizing a Function of Several Variables

2 ビュー (過去 30 日間)
Mohammad
Mohammad 2015 年 11 月 9 日
コメント済み: Star Strider 2015 年 11 月 9 日
Hi there, I want to find a minimum for this function:
g = 0.04 - (32*P/(9*sqrt(3)*E*MOI));
and I've written this code to do so:
clc;
clear;
function b = three_var(v)
P = v(1);
E = v(2);
MOI = v(3);
b = 0.04 - (32*P/(9*sqrt(3)*E*MOI));
end;
v = [0.12 -2.0 -0.35];
a = fminsearch(@three_var,v)
but I get the error "Function definitions are not permitted in this context". I've been stuck with this for a day and don't know what to do. I would be grateful if you could help.

採用された回答

Star Strider
Star Strider 2015 年 11 月 9 日
A function declaration as you coded it is not permitted in a script file. You have to save it it its own function file, named three_var.m However, you can easily code it as an anonymous function, avoiding that problem:
three_var = @(v) 0.04 - (32*v(1)/(9*sqrt(3)*v(2)*v(3))); % Anonymous Function
v = [0.12 -2.0 -0.35];
a = fminsearch(three_var,v)
a =
218.3938e-003 -1.8055e+000 -18.3576e-030
  7 件のコメント
John D'Errico
John D'Errico 2015 年 11 月 9 日
But you cannot solve this problem with fminsearch. Sigh.
Star Strider
Star Strider 2015 年 11 月 9 日
‘Sigh’ indeed.
See my previous comment.

サインインしてコメントする。

その他の回答 (2 件)

John D'Errico
John D'Errico 2015 年 11 月 9 日
As both Matt and Star have pointed out, you need to define the function properly to use a solver like fminsearch, but they have shown you how to do so.
More importantly, on this particular problem, there is NO solution. You can choose sets of values that will yield arbitrarily large negative values, as close to -inf as you wish to go. The minimization problem will be divergent for this objective function.

Matt J
Matt J 2015 年 11 月 9 日
編集済み: Matt J 2015 年 11 月 9 日
You cannot define functions (like three_var) inside a script. Make your mfile a function file.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by