Not enough input arguments Been over the code so much need fresh eyes
이전 댓글 표시
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
2023년 10월 17일
0 개 추천
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.
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)
% %% 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
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!