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

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 Comments

It just says "Error in Constraints7_8 (line 7) g(1)=axial-150000000;"
No, it definitely says more than that. Please show us the full error messages.
Also, your constraints all appear to be linear, so you should use the A,b,Aeq,beq,ub,lb input arguments to specify those, rather than formulating them as non-linear constraints.
So based on what I researched about your answer it seems like you are saying my inequalities should be in matrices A and B. Since I have no equality constraints aeq and beq can be skipped. Then I turn my upper bound and lower bound into matrices as well.
function [f] = Objective7_8(A,B,lb,ub)
Is that correct? Also I'm not home at my computer so I can't show you the full error codes just yet but I wI'll as soon as possible.
No, you don't pass A,B, lb, ub to your Objective function. You enter them in the "Linear inequalities" and "Bounds" section of the app screen.
So then you are saying don't use a script for the constraints? Just enter the values below into the optimtool app screen?
A=[axial; P];
B=[150000000;(Pcr/2)];
lb=[0.5, 50];
ub=[50 500]
Matt J,
Please see the attached snapshot of the error I get when I run my current constraints file. Also I reset the Optimtool based on what we mentioned before and I'm still getting an error. The error is about me not having any equality constraints. Please see that attached also and provide an explanation as to what I'm doing wrong.
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?
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?

Sign in to comment.

Answers (1)

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 Comments

So I read the example and I feel like I have what they have there. The difference is that I have more than one variable in my function (D,H). Even though they used X1 and X2 it looked like they tried to call x like a matrix but I don't see the matrix called out anywhere. So how would I do the same with my variables?
As to your other comment, I'm wondering why I get the not enough inputs error if there are too many variables?
"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.
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;

Sign in to comment.

Categories

Products

Release

R2018a

Asked:

on 21 Sep 2018

Edited:

on 22 Sep 2018

Community Treasure Hunt

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

Start Hunting!