Problem with Minimizing a Function of Several Variables

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.

 Accepted Answer

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 Comments

Note that the solution you have obtained is not unique. In fact, this function has no unique minimum. You can choose a solution that will yield any arbitrarily large negative value.
True, especially since ‘E’ and ‘MOI’ as a product, and not being defined independently elsewhere in the function, are not individually unique.
Since this could be a ‘proxy problem’ (there are myriad examples of such on MATLAB Answers), I simply suggested an alternative solution to the stated problem in the Question, and to demonstrate that it worked with fminsearch. QED!
True, especially since ‘E’ and ‘MOI’ as a product, and not being defined independently elsewhere in the function, are not individually unique.
In fact, the whole second term could be replaced by a single parameter, x, reducing the function to
b = 0.04 - x
Exactly. You cannot minimize the expression that Matt shows any more than you can find a minimum for the three variable expression given by the OP. Were the OP to try to solve it using fminsearch, they will eventually get a meaningless result, much as did Star. Fminsearch did not "converge" there, it merely terminated due to running out of gas.
However, the Question was not ‘why does my function produce meaningless parameter estimates’ but ‘how can I get my function to work with fminsearch’.
Focus!
But you cannot solve this problem with fminsearch. Sigh.
‘Sigh’ indeed.
See my previous comment.

Sign in to comment.

More Answers (2)

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 on 9 Nov 2015
Edited: Matt J on 9 Nov 2015
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!