FMINCON requires all values returned by functions to be of data type double
2 次查看(过去 30 天)
显示 更早的评论
Hello, I'm coding a project for optimization in probability engineering but I got an error with the function fmincon. I'm a newbie using Matlab so don't really know what the error is all about.
Here is my main function:
% Initialize parameters
d0 = [0.2,0.2,3]; %starting point, b=h=0.1, l=2
lb = [0.1,0.1,2]; %lower bounds for design variables
ub = [0.4,0.8,20]; %upper bounds for design variables
option = optimset('Display','iter'); %set options to show the optimization history
d = fmincon('Obj_fun',d0,[],[],[],[],lb,ub,'constr_fun',option); %call the optimizer
% Analysis the optimal point
t = d(1);
h = d(2);
l = d(3);
obj = t*h*l;
c = constr_fun(d); %calculate the constraint function
disp(['The optimal point = ',num2str(d)]);
disp(['The objective function = ',num2str(obj)]);
disp(['The constraint functions = ',num2str(c)]);
And here are my 2 others functions for using fmincon:
% Construction function
function [c,ceq]=constr_fun(d)
b = d(1);
h = d(2);
l = d(3);
Pf_mean = 300; Pf_std = 30; Pt_mean = 450; Pt_std = 50; Ps_mean = 100e4; Ps_std = 10e4;
syms u1 u2 u3
Pf_sample = Pf_mean+u1*Pf_std;
Pt_sample = Pt_mean+u2*Pt_std;
Ps_sample = Ps_mean+u3*Ps_std;
c = Ps_sample - ((6*Pf_sample*l/b*h^2).^2 + (Pt_sample/h*b^2*(3+1.8*b/h)).^2).^0.5; %constraint equation
ceq = []; %no equality constraint
% Objective function
function obj = Obj_fun(d)
b = d(1);
h = d(2);
l = d(3);
obj = b*h*l;
0 个评论
回答(1 个)
Alan Weiss
2021-7-26
Generally, in order for us to be able to help you need to report the entire error message that you receive, everything in red. Additionally we would need to see your function call and function definitions.
However, there are some obvious programming errors here. You declare some symbolic variables. Don't do that. You don't need them. Most likely, symbolic variables are variables that you want to optimize. They should be the variables in your d vector.
Also, you need to write a separate function for your nonlinear constraints. Something like this:
function [c,ceq] = confun(d)
b = d(1);
h = d(2);
l = d(3);
% Give an expression in d for Ps_sample, etc. Then
c = Ps_sample - ((6*Pf_sample*l/b*h^2).^2 + (Pt_sample/h*b^2*(3+1.8*b/h)).^2).^0.5; %constraint equation
ceq = []; %no equality constraint
end
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!