필터 지우기
필터 지우기

Not enough input arguments Been over the code so much need fresh eyes

조회 수: 1 (최근 30일)
Taylor Millett
Taylor Millett 2023년 10월 17일
댓글: Walter Roberson 2023년 10월 17일
clear; close all; clc;
x1 = linspace(-15,0); % Ao
x2 = linspace(-15,0); % Am
[X1,X2] = meshgrid(x1,x2);
% Variables
rho = 2710; % Density kg/m^3
beta = 1/12;
E = 69 * 10^6; % Gpa
sigmayield = 110 * 10^6; % MPa
Amin = 0.0001; % m^2
l = 0.5; % m
theta = 55 * pi/180; % degrees to radians
P = 500 * 10^-3; % kN
M = rho*(l*(2*sqrt(2)*X1+X2));
contour(X1,X2,M,75)
xlabel('x1')
ylabel('x2')
hold on
x0 = [0,0];
options = optimoptions(@fmincon,'Display','iter');
fun = @(x)Mass(rho,l,x);
[xstar,fstar] = fmincon(fun,x0,[],[],[],[],[],[],@Constraints,options)
Not enough input arguments.
Error in Prob5_13_Millett>Constraints (line 63)
c(1) = Amin-x(1);
Error in fmincon (line 650)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
% Plot minimum point
plot(xstar(1),xstar(2),'r.','MarkerSize',20)
hold on
% Constraints
IneqCon1 = Amin-X1;
contourf(X1,X2,-IneqCon1,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon2 = Amin-X2;
contourf(X1,X2,-IneqCon2,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon3 = (1/sqrt(2))*((P*cos(theta))./X1)+((P*sin(theta))./(X2+sqrt(2).*X2))-sigmayield;
contourf(X1,X2,-IneqCon3,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon4 = (sqrt(2)*P*sin(theta))/(X1+sqrt(2)*X2)-sigmayield;
contourf(X1,X2,-IneqCon4,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon5 = (1/sqrt(2))*((P*sin(theta))/(X1+sqrt(2)*X2))-((P*cos(theta))/X1)-sigmayield;
contourf(X1,X2,-IneqCon5,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon6 = -((1/sqrt(2))*((P*cos(theta))/X1)+((P*sin(theta))/(X1+sqrt(2)*X2)))-(pi^2*E*beta*X1)/(2*l^2);
contourf(X1,X2,-IneqCon6,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon7 = -((sqrt(2)*P*sin(theta))/(X1+sqrt(2)*X2))-(pi^2*E*beta*X2)/(2*l^2);
contourf(X1,X2,-IneqCon7,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold on
IneqCon8 = -((1/sqrt(2))*((P*sin(theta))/(X1+sqrt(2)*X2))-((P*cos(theta))/X1))-(pi^2*E*beta*X1)/(2*l^2);
contourf(X1,X2,-IneqCon8,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
hold off
% Functions
function M = Mass(rho,l,x)
M = rho*(l*(2*sqrt(2)*x(1)+x(2)));
end
function [c,ceq] = Constraints(x,P,theta,Amin,sigmayield,E,beta)
c = zeros(8,1);
c(1) = Amin-x(1);
c(2) = Amin-x(2);
c(3) = (1/sqrt(2))*((P*cos(theta))/x(1))+((P*sin(theta))/(x(1)+sqrt(2)*x(2)))-sigmayield;
c(4) = (sqrt(2)*P*sin(theta))/(x(1)+sqrt(2)*x(2))-sigmayield;
c(5) = (1/sqrt(2))*((P*sin(theta))/(x(1)+sqrt(2)*x(2)))-((P*cos(theta))/x(1))-sigmayield;
c(6) = -((1/sqrt(2))*((P*cos(theta))/x(1))+((P*sin(theta))/(x(1)+sqrt(2)*x(2))))-(pi^2*E*beta*x(1))/(2*l^2);
c(7) = -((sqrt(2)*P*sin(theta))/(x(1)+sqrt(2)*x(2)))-(pi^2*E*beta*x(2))/(2*l^2);
c(8) = -((1/sqrt(2))*((P*sin(theta))/(x(1)+sqrt(2)*x(2)))-((P*cos(theta))/x(1)))-(pi^2*E*beta*x(1))/(2*l^2);
ceq = [];
end

답변 (2개)

Steven Lord
Steven Lord 2023년 10월 17일
How does fmincon call the nonlinear constraint function? Specifically, how many inputs does it pass into that function? From that documentation page:
"nonlcon is a function that accepts a vector or array x and returns two arrays, c(x) and ceq(x)."
How many inputs are in the signature of your nonlinear constraint function?
"function [c,ceq] = Constraints(x,P,theta,Amin,sigmayield,E,beta)"
You're going to need to treat your constraint function like you did the objective function to pass the additional parameters into it.

Sam Chak
Sam Chak 2023년 10월 17일
The code for the fmincon part is now fixed and it returns a local minimum solution.
% Constants
rho = 2710; % Density kg/m^3
beta = 1/12;
E = 69 * 10^6; % Gpa
sigmayield = 110 * 10^6; % MPa
Amin = 0.0001; % m^2
l = 0.5; % m
theta = 55 * pi/180; % degrees to radians
P = 500 * 10^-3; % kN
% % contour plot
% x1 = linspace(-15,0); % Ao
% x2 = linspace(-15,0); % Am
% [X1,X2] = meshgrid(x1,x2);
% M = rho*(l*(2*sqrt(2)*X1+X2));
% contour(X1,X2,M,75)
% xlabel('x1')
% ylabel('x2')
% hold on
% Calling fmincon to save me!
x0 = [1,1];
options = optimoptions(@fmincon,'Display','iter');
fun = @(x)Mass(rho,l,x);
[xstar,fstar] = fmincon(fun,x0,[],[],[],[],[],[],@Constraints,options)
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 3 5.187519e+03 0.000e+00 9.581e+02 1 6 5.186710e+03 0.000e+00 9.581e+02 2.067e-04 2 9 5.029949e+03 0.000e+00 5.672e+02 4.013e-02 3 12 7.074181e+02 0.000e+00 2.697e+00 1.167e+00 4 15 4.193843e+00 0.000e+00 4.291e+00 2.090e-01 5 19 6.251653e-01 0.000e+00 2.071e+02 1.076e-03 6 27 7.925200e-01 0.000e+00 1.600e-04 9.099e-05 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
xstar = 1×2
1.0e-03 * 0.1149 0.2600
fstar = 0.7925
% %% Plot minimum point
% plot(xstar(1),xstar(2),'r.','MarkerSize',20)
% hold on
%
% %% Constraints
% IneqCon1 = Amin-X1;
% contourf(X1,X2,-IneqCon1,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon2 = Amin-X2;
% contourf(X1,X2,-IneqCon2,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon3 = (1/sqrt(2))*((P*cos(theta))./X1)+((P*sin(theta))./(X2+sqrt(2).*X2))-sigmayield;
% contourf(X1,X2,-IneqCon3,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon4 = (sqrt(2)*P*sin(theta))./(X1+sqrt(2)*X2)-sigmayield;
% contourf(X1,X2,-IneqCon4,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon5 = (1/sqrt(2))*((P*sin(theta))./(X1+sqrt(2)*X2))-((P*cos(theta))./X1)-sigmayield;
% contourf(X1,X2,-IneqCon5,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon6 = -((1/sqrt(2))*((P*cos(theta))/X1)+((P*sin(theta))/(X1+sqrt(2)*X2)))-(pi^2*E*beta*X1)/(2*l^2);
% contourf(X1,X2,-IneqCon6,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon7 = -((sqrt(2)*P*sin(theta))/(X1+sqrt(2)*X2))-(pi^2*E*beta*X2)/(2*l^2);
% contourf(X1,X2,-IneqCon7,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold on
% IneqCon8 = -((1/sqrt(2))*((P*sin(theta))/(X1+sqrt(2)*X2))-((P*cos(theta))/X1))-(pi^2*E*beta*X1)/(2*l^2);
% contourf(X1,X2,-IneqCon8,[0 0],'r-','LineWidth',1.5','FaceAlpha',0.25)
% hold off
%% Functions
% Cost function
function M = Mass(rho,l,x)
M = rho*(l*(2*sqrt(2)*x(1)+x(2)));
end
% Constraint function
function [c,ceq] = Constraints(x)
% Constants
rho = 2710; % Density kg/m^3
beta = 1/12;
E = 69 * 10^6; % Gpa
sigmayield = 110 * 10^6; % MPa
Amin = 0.0001; % m^2
l = 0.5; % m
theta = 55 * pi/180; % degrees to radians
P = 500 * 10^-3; % kN
% Constraints
c = zeros(8,1);
c(1) = Amin-x(1);
c(2) = Amin-x(2);
c(3) = (1/sqrt(2))*((P*cos(theta))/x(1))+((P*sin(theta))/(x(1)+sqrt(2)*x(2)))-sigmayield;
c(4) = (sqrt(2)*P*sin(theta))/(x(1)+sqrt(2)*x(2))-sigmayield;
c(5) = (1/sqrt(2))*((P*sin(theta))/(x(1)+sqrt(2)*x(2)))-((P*cos(theta))/x(1))-sigmayield;
c(6) = -((1/sqrt(2))*((P*cos(theta))/x(1))+((P*sin(theta))/(x(1)+sqrt(2)*x(2))))-(pi^2*E*beta*x(1))/(2*l^2);
c(7) = -((sqrt(2)*P*sin(theta))/(x(1)+sqrt(2)*x(2)))-(pi^2*E*beta*x(2))/(2*l^2);
c(8) = -((1/sqrt(2))*((P*sin(theta))/(x(1)+sqrt(2)*x(2)))-((P*cos(theta))/x(1)))-(pi^2*E*beta*x(1))/(2*l^2);
ceq = [];
end

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by