How do you properly write a function to use fmincon from optimization tool?

14 visualizzazioni (ultimi 30 giorni)
I have been asked to use 'optimtool' to minimize a non linear problem. I was told to write a constraints script and an objective script. I also previously heard that the function called out in the script is supposed to be the same as the name of the script. But sometimes I get errors when I do that saying that the function has a duplicate name. My main issues are as follows:
1. Optimtool, my objective function, and my constraints function keeps giving the error "Not Enough Input Arguments" 2. My constraints function will not recognize my constraint lines. It just says "Error in Constraints7_8 (line 7) g(1)=axial-150000000;" Below I'm showing what I have for each script.
%Objective7_8
function [f] = Objective7_8(D,H)
% f returns value of objective function
% Evaluate objective function
f = 6.60*D.^2*(H.^2+4800).^0.5;
end
%Constraints7_8
function [g,h] = Constraints7_8(axial,P,Pcr,H,D)
% g returns inequality constraints
% h returns equality constraints
% Inequality constraints
g(1) =axial-150000000;
g(2) =P-(Pcr/2);
g(3) =50-H;
g(4) =H-500;
g(5) =0.5-D;
g(6) =D-50;
% Equality constraints
h = [];
end
%Problem7_8
D=0.5:0.11:50;
H=50:1:500;
%Constants%
W=60000;
B=120;
sigma_a=150000000;
E=7500000;
rho=2.8;
FS=2;
%Parameters%
I=(pi/64)*D.^4;
l=(H.^2 +(1/3)*B^2).^0.5;
Pcr=(pi^2*E*I)/I.^2;
P=(W*l/3.*H);
axial=P/D;
  7 Commenti
Matt J
Matt J il 22 Set 2018
Modificato: Matt J il 22 Set 2018
Why do you have constraints on axial, P, and Pcr when they are not unknowns? Your unknowns are H and D.
If they are unknowns, why do you have only 2 initial values [25,75] instead of 5? Similarly, why only two lower and upper bounds?
Terrance Griffin
Terrance Griffin il 22 Set 2018
Modificato: Terrance Griffin il 22 Set 2018
Because the problem lists them as constraints. It is my understanding that any constraints listed in the problem have to be shown here. And yes there is a value on axial and Pcr but P is a matrix so there are many values. Why wouldn't those be constraints. If it helps, here is the original problem. As you can see the problem gives three constraint equations and if you are asking what I think you are, I don't know why the constraints aren't only in terms of D and H. Is it my job to convert them to be variables of D and H only?

Accedi per commentare.

Risposte (1)

Alan Weiss
Alan Weiss il 21 Set 2018
Take a look at the Getting Started example. Your main problem is that you have too many variables; all your variables need to be put into one, typically called x. See also Writing Scalar Objective Functions for instructions on how to do this.
Alan Weiss
MATLAB mathematical toolbox documentation
  3 Commenti
Stephen23
Stephen23 il 21 Set 2018
Modificato: Stephen23 il 21 Set 2018
"So how would I do the same with my variables?"
Just like Alan Weiss wrote: put all of your variables into one array, e.g.: if you have three scalar input arguments, then your array will have three elements. Don't think of your "variables" as being separate input arguments, think of them as being elements of one array. Use indexing to access them.
"I'm wondering why I get the not enough inputs error if there are too many variables?"
fmincon calls the function handle with one input argument (i.e. x, an array). MATLAB gives that error message because you wrote your function to require multiple input arguments, so when it is only provided with one input argument then it complains that it is not getting all of the input arguments that it expects!
If it makes you any happier, you could do something like this:
fun = @(x) Constraints7_8(x(1),x(2),x(3),x(4),x(5));
and use fun in fmincon. Adjust the indices to suit the sizes of the input arguments.
Terrance Griffin
Terrance Griffin il 21 Set 2018
Modificato: Walter Roberson il 21 Set 2018
Ok I can play around with that because o thought that it may be necessary. So are you thinking for the objective function would require a dummy matrix
X=[D,H]
Then
function [f] = Objective7_8(X)
D=X(1); H=X(2);
f = 6.60*D.^2*(H.^2+4800).^0.5;

Accedi per commentare.

Categorie

Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by