Error using fmincon Too many input arguments

3 Ansichten (letzte 30 Tage)
Elmira Jafari
Elmira Jafari am 15 Dez. 2021
Kommentiert: Walter Roberson am 16 Dez. 2021
Hello , I have an emargency problem as the due date of my project is close. while every things is correct I dont know why when I run my code, it gives me an error of too many input arguments.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Wheelchair
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Wheelchair
% clc;
clear all; close all;
dp=design_parameters; % Generate structure variable containing all design parameters
f=@(x)objective_function(x,dp); % Objective function handle
nonlcon=@(x)nonlinear_constraints(x,dp); % Nonlinear constraint function handle
lb=[dp.X1_min;dp.X2_min;dp.X3_min;dp.X4_min];% Design variable bounds
ub=[dp.X1_max;dp.X2_max;dp.X3_max;dp.X4_max]; % Design variable bounds
x0=[25e-3;1.5e-3;20e-3;1e-3]; % Initial design point
A=[0,-(0.68*dp.db*dp.sigmau)/(0.3*dp.mb*dp.g),0,0;-1/dp.X1_min,0,0,0;1/dp.X1_max,0,0,0;0,-1/dp.X2_min,0,0;0,1/dp.X2_max,0,0;0,0,-1/dp.X3_min,0;0,0,1/dp.X3_max,0;0,0,0,-1/dp.X4_min;0,0,0,1/dp.X4_max];
b=[-1,-1,1,-1,1,-1,1,-1,1];
% Minimization using fmincon and the specified options:
options=optimoptions('fmincon','Display','iter','OptimalityTolerance',1e-12,'ConstraintTolerance',1e-6,'StepTolerance',1e-12);
[x,fval]=fmincon(f,x0,A,b,[],[],lb,ub,nonlcon,options);
% Display of the optimal design results:
disp(['Optimal value of d1: ',num2str(x(1)),' mm']);
disp(['Optimal value of t1: ',num2str(x(2)),' mm']);
disp(['Optimal value of d2: ',num2str(x(3)),' mm']);
disp(['Optimal value of t2: ',num2str(x(4)),'mm']);
disp(['Optimal value of f: ',num2str(fval),' kg']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dp=design_parameters
dp.L1=0.82; %(m)
dp.L2=0.691; %(m)
dp.L3=0.270; %(m)
dp.L4=0.04; %(m)
dp.L5=1.59; %(m)
dp.L6=0.627; %(m)
dp.L7=0.568;
dp.Lad=0.601;
dp.Lss=0.4;
dp.mb=150;
dp.Rho=2700;
dp.g=10;
dp.db=6e-3;
dp.sigmau=110e6;
dp.E=70e9;
dp.K=2.1;
dp.deltamax=1e-3;
dp.X1_min=20e-3; dp.X1_max=30e-3; %
dp.X2_min=1e-3; dp.X2_max=2e-3; %
dp.X3_min=1e-3; dp.X3_max=22e-3; %
dp.X4_min=0.8e-3; dp.X4_max=1.5e-3;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=objective_function(x,dp)
A1=(x(1)/2)^2-(x(1)/2-x(2))^2;
A2=(x(1)/2-x(2))^2-(x(1)/2-2*x(2))^2;
A3=((x(1)+2*x(2))^2-(x(1))^2)/4;
A4=(x(3)/2)^2-(x(3)/2-x(4))^2;
f=2*((pi*A1*dp.L1*dp.Rho)+(pi*A1*dp.L2*dp.Rho)+(pi*A2*dp.L3*dp.Rho)+(6*pi*A3*dp.L4*dp.Rho)+(0.097))+((pi*A4*dp.L5*dp.Rho)+(pi*A4*dp.L6*dp.Rho)+(pi*A4*dp.L7*dp.Rho)); %
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [c,ceq]=nonlinear_constraints(x,dp)
A5=(x(1))^4-(x(1)-2*x(2))^4;
c(10,1)=0.2*dp.mb*dp.g-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
c(11,1)=((dp.mb*dp.g/2)*(dp.Lss)^3/(48/64)*pi*A5)-dp.deltamax; %
ceq=[]; % There are no nonlinear equality constraints
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6 Kommentare
Torsten
Torsten am 16 Dez. 2021
Note that the inequality constraints in the function "nonlinear_constraints" must be indexed starting with 1:
c(1)=0.2*dp.mb*dp.g-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
c(2)=((dp.mb*dp.g/2)*(dp.Lss)^3/(48/64)*pi*A5)-dp.deltamax; %
Walter Roberson
Walter Roberson am 16 Dez. 2021
While indexing from 1 is a good idea for clarity, it is not necessary. As usual when you first store into a matrix starting at an index other than 1, all elements from 1 to just before where you wrote are filled with 0. But for the purposes of constraints, 0 is fine: 0 satisfies the <= 0 test for nonlinear inequalities, and statisfies the == 0 for nonlinear equalities. So those entries c(1:9) being 0 are a small waste of time but do not affect convergence.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

KSSV
KSSV am 15 Dez. 2021
You may use scatter3
figure(1);
scatter3(x(1),x(2),x(3),[],x(4),'filled');
  3 Kommentare
KSSV
KSSV am 15 Dez. 2021
Then may you are expecting:
plot(x(1),x(2),'.b',x(3),x(4),'.r');
Elmira Jafari
Elmira Jafari am 15 Dez. 2021
Bearbeitet: Walter Roberson am 15 Dez. 2021
would you run this code?
wheel()
Could not find a feasible initial point.
Index exceeds the number of array elements. Index must not exceed 0.

Error in solution>wheel (line 43)
disp(['Optimal value of d1: ',num2str(x(1)),' mm']);
function wheel
clc; clear all; close all;
global ccc
rng('shuffle');
dp=design_parameters; % Generate structure variable containing all design parameters
f=@(x)objective_function(x,dp); % Objective function handle
g=@(x)nonlinear_constraints(x,dp); % Nonlinear constraint function handle
nvars=4; % Number of design variables
lb=[dp.X1_min;dp.X2_min;dp.X3_min;dp.X4_min];% Design variable bounds
ub=[dp.X1_max;dp.X2_max;dp.X3_max;dp.X4_max]; % Design variable bounds
A=[0,-(0.68*dp.db*dp.sigmau)/(0.3*dp.mb*dp.g),0,0;-1/dp.X1_min,0,0,0;1/dp.X1_max,0,0,0;0,-1/dp.X2_min,0,0;0,1/dp.X2_max,0,0;0,0,-1/dp.X3_min,0;0,0,1/dp.X3_max,0;0,0,0,-1/dp.X4_min;0,0,0,1/dp.X4_max];
b=[-1,-1,1,-1,1,-1,1,-1,1];
figure(1); hold on; grid on; box on; view(3);
axis([dp.X1_min dp.X1_max dp.X2_min dp.X2_max dp.X3_min dp.X3_max]);
xlabel('d1'); ylabel('t1'); zlabel('d2');
options=optimoptions('ga','PopulationSize',200,...
'EliteCount',5,...
'MaxGenerations',200,...
'MutationFcn',@mutationadaptfeasible,...
'CrossoverFcn',{@crossoverintermediate,1.5},...
'FunctionTolerance',1e-12,...
'ConstraintTolerance',1e-6,...
'HybridFcn','fmincon',...
'Display','iter',...
'PlotFcn',{@gaplotbestf,@gaplotrange},...
'OutputFcn',@custom_output);
% Call to the 'ga' function:
[x,fval,exitflag,output,population,scores]=ga(f,nvars,A,b,[],[],lb,ub,g,options);
disp(['Optimal value of d1: ',num2str(x(1)),' mm']);
disp(['Optimal value of t1: ',num2str(x(2)),' mm']);
disp(['Optimal value of d2: ',num2str(x(3)),' mm']);
disp(['Optimal value of t2: ',num2str(x(4)),'mm']);
disp(['Optimal value of f: ',num2str(fval),' kg']);
figure(1);
plot3(x(1),x(2),x(3),'b.','Markersize',20);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dp=design_parameters
dp.L1=0.82; %(m)
dp.L2=0.691; %(m)
dp.L3=0.270; %(m)
dp.L4=0.04; %(m)
dp.L5=1.59; %(m)
dp.L6=0.627; %(m)
dp.L7=0.568;
dp.Lad=0.601;
dp.Lss=0.4;
dp.mb=150;
dp.Rho=2700;
dp.g=10;
dp.db=6e-3;
dp.sigmau=110e6;
dp.E=70e9;
dp.K=2.1;
dp.deltamax=1e-3;
dp.X1_min=10e-3; dp.X1_max=20e-3; %
dp.X2_min=0.5e-3; dp.X2_max=1e-3; %
dp.X3_min=10e-3; dp.X3_max=20e-3; %
dp.X4_min=0.5e-3; dp.X4_max=1e-3;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=objective_function(x,dp)
A1=(x(1)/2)^2-(x(1)/2-x(2))^2;
A2=(x(1)/2-x(2))^2-(x(1)/2-2*x(2))^2;
A3=((x(1)+2*x(2))^2-(x(1))^2)/4;
A4=(x(3)/2)^2-(x(3)/2-x(4))^2;
f=2*((pi*A1*dp.L1*dp.Rho)+(pi*A1*dp.L2*dp.Rho)+(pi*A2*dp.L3*dp.Rho)+(6*pi*A3*dp.L4*dp.Rho)+(0.097))+((pi*A4*dp.L5*dp.Rho)+(pi*A4*dp.L6*dp.Rho)+(pi*A4*dp.L7*dp.Rho)); %
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [c,ceq]=nonlinear_constraints(x,dp)
A5=(x(1))^4-(x(1)-2*x(2))^4;
c(10,1)=0.2*dp.mb*dp.g-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
c(11,1)=((dp.mb*dp.g/2)*(dp.Lss)^3/(48/64)*pi*A5)-dp.deltamax; %
ceq=[]; % There are no nonlinear equality constraints
end
%%%%%%%%%%%%
function [state,options,optchanged]=custom_output(options,state,flag)
global ccc
title(['Current generation: ',num2str(state.Generation)]);
if ~isempty(ccc)
delete(ccc);
end
figure(1)
ccc=plot3(state.Population(:,1),state.Population(:,2),state.Population(:,3),'r.','Markersize',10);
pause(0.01);
optchanged = false;
end

Melden Sie sich an, um zu kommentieren.


Elmira Jafari
Elmira Jafari am 15 Dez. 2021
Bearbeitet: Walter Roberson am 15 Dez. 2021
I have a question, I run the same objective function, constraints, boundrie by GA and fmincon. fmincon gives me a logical answre but Ga cant solve it. what would be the reason? I have attached them. First one is by Ga and second one by fmincon. It is appreciated if help me as always.
GA
function wheel
clc; clear all; close all;
global ccc
rng('shuffle');
dp=design_parameters; % Generate structure variable containing all design parameters
f=@(x)objective_function(x,dp); % Objective function handle
g=@(x)nonlinear_constraints(x,dp); % Nonlinear constraint function handle
nvars=4; % Number of design variables
lb=[dp.X1_min;dp.X2_min;dp.X3_min;dp.X4_min];% Design variable bounds
ub=[dp.X1_max;dp.X2_max;dp.X3_max;dp.X4_max]; % Design variable bounds
A=[0,-(0.68*dp.db*dp.sigmau)/(0.3*dp.mb*dp.g),0,0;-1/dp.X1_min,0,0,0;1/dp.X1_max,0,0,0;0,-1/dp.X2_min,0,0;0,1/dp.X2_max,0,0;0,0,-1/dp.X3_min,0;0,0,1/dp.X3_max,0;0,0,0,-1/dp.X4_min;0,0,0,1/dp.X4_max];
b=[-1,-1,1,-1,1,-1,1,-1,1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialization of figure used to illustrate individuals within
% design space:
%figure(1); hold on; grid on; box on; view(3);
%axis([dp.X1_min dp.X1_max dp.X2_min dp.X2_max dp.X3_min dp.X3_max]);
%xlabel('d1'); ylabel('t1'); zlabel('d2');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Defining various GA options:
options=optimoptions('ga','PopulationSize',200,...
'EliteCount',5,...
'MaxGenerations',300,...
'MutationFcn',@mutationadaptfeasible,...
'CrossoverFcn',{@crossoverintermediate,1.5},...
'FunctionTolerance',1e-12,...
'ConstraintTolerance',1e-6,...
'HybridFcn','fmincon',...
'Display','iter',...
'PlotFcn',{@gaplotbestf,@gaplotrange},...
'OutputFcn',@custom_output);
% Call to the 'ga' function:
[x,fval,exitflag,output,population,scores]=ga(f,nvars,A,b,[],[],lb,ub,g,options);
disp(['Optimal value of d1: ',num2str(1000*x(1)),' mm']);
disp(['Optimal value of t1: ',num2str(1000*x(2)),' mm']);
disp(['Optimal value of d2: ',num2str(1000*x(3)),' mm']);
disp(['Optimal value of t2: ',num2str(1000*x(4)),'mm']);
disp(['Optimal value of f: ',num2str(fval),' kg']);
% figure(1);
% plot3(x(1),x(2),x(3),'.b');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dp=design_parameters
dp.L1=0.82; %(m)
dp.L2=0.691; %(m)
dp.L3=0.270; %(m)
dp.L4=0.04; %(m)
dp.L5=1.59; %(m)
dp.L6=0.627; %(m)
dp.L7=0.568;
dp.Lad=0.601;
dp.Lss=0.4;
dp.mb=150;
dp.Rho=2700;
dp.g=10;
dp.db=6e-3;
dp.sigmau=110e6;
dp.E=70e9;
dp.K=2.1;
dp.deltamax=1e-3;
dp.X1_min=14e-3; dp.X1_max=20e-3; %
dp.X2_min=0.5e-3; dp.X2_max=1e-3; %
dp.X3_min=10e-3; dp.X3_max=16e-3; %
dp.X4_min=0.5e-3; dp.X4_max=1e-3;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=objective_function(x,dp)
A1=(x(1)/2)^2-(x(1)/2-x(2))^2;
A2=(x(1)/2-x(2))^2-(x(1)/2-2*x(2))^2;
A3=((x(1)+2*x(2))^2-(x(1))^2)/4;
A4=(x(3)/2)^2-(x(3)/2-x(4))^2;
f=2*((pi*A1*dp.L1*dp.Rho)+(pi*A1*dp.L2*dp.Rho)+(pi*A2*dp.L3*dp.Rho)+(6*pi*A3*dp.L4*dp.Rho)+(0.097))+((pi*A4*dp.L5*dp.Rho)+(pi*A4*dp.L6*dp.Rho)+(pi*A4*dp.L7*dp.Rho)); %
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [c,ceq]=nonlinear_constraints(x,dp)
A5=(x(1))^4-(x(1)-2*x(2))^4;
c(10,1)=0.2*dp.mb*dp.g-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
c(11,1)=((dp.mb*dp.g/2)*(dp.Lss)^3/(48/64)*pi*A5)-dp.deltamax; %
ceq=[]; % There are no nonlinear equality constraints
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [state,options,optchanged]=custom_output(options,state,flag)
global ccc
title(['Current generation: ',num2str(state.Generation)]);
if ~isempty(ccc)
delete(ccc);
end
figure(1)
ccc=plot3(state.Population(:,1),state.Population(:,2),state.Population(:,3),'r.','Markersize',10);
pause(0.01);
optchanged = false;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Wheelchair
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Wheelchair
% clc;
clear all; close all;
dp=design_parameters; % Generate structure variable containing all design parameters
f=@(x)objective_function(x,dp); % Objective function handle
nonlcon=@(x)nonlinear_constraints(x,dp); % Nonlinear constraint function handle
lb=[dp.X1_min;dp.X2_min;dp.X3_min;dp.X4_min];% Design variable bounds
ub=[dp.X1_max;dp.X2_max;dp.X3_max;dp.X4_max]; % Design variable bounds
x0=[25e-3;1.5e-3;20e-3;1e-3]; % Initial design point
A=[0,-(0.68*dp.db*dp.sigmau)/(0.3*dp.mb*dp.g),0,0;-1/dp.X1_min,0,0,0;1/dp.X1_max,0,0,0;0,-1/dp.X2_min,0,0;0,1/dp.X2_max,0,0;0,0,-1/dp.X3_min,0;0,0,1/dp.X3_max,0;0,0,0,-1/dp.X4_min;0,0,0,1/dp.X4_max];
b=[-1,-1,1,-1,1,-1,1,-1,1];
% Minimization using fmincon and the specified options:
options=optimoptions('fmincon','Display','iter','OptimalityTolerance',1e-12,'ConstraintTolerance',1e-6,'StepTolerance',1e-12);
[x,fval]=fmincon(f,x0,A,b,[],[],lb,ub,nonlcon,options);
% Display of the optimal design results:
disp(['Optimal value of d1: ',num2str(1000*x(1)),' mm']);
disp(['Optimal value of t1: ',num2str(1000*x(2)),' mm']);
disp(['Optimal value of d2: ',num2str(1000*x(3)),' mm']);
disp(['Optimal value of t2: ',num2str(1000*x(4)),'mm']);
disp(['Optimal value of f: ',num2str(fval),' kg']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dp=design_parameters
dp.L1=0.82; %(m)
dp.L2=0.691; %(m)
dp.L3=0.270; %(m)
dp.L4=0.04; %(m)
dp.L5=1.59; %(m)
dp.L6=0.627; %(m)
dp.L7=0.568;
dp.Lad=0.601;
dp.Lss=0.4;
dp.mb=150;
dp.Rho=2700;
dp.g=10;
dp.db=6e-3;
dp.sigmau=110e6;
dp.E=68e9;
dp.K=2.1;
dp.deltamax=1e-3;
dp.X1_min=14e-3; dp.X1_max=20e-3; % Minimum/maximum length of rectangular (mm)
dp.X2_min=0.5e-3; dp.X2_max=1.1e-3; %
dp.X3_min=10e-3; dp.X3_max=15e-3; %
dp.X4_min=0.5e-3; dp.X4_max=1.1e-3;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=objective_function(x,dp)
A1=(x(1)/2)^2-(x(1)/2-x(2))^2;
A2=(x(1)/2-x(2))^2-(x(1)/2-2*x(2))^2;
A3=((x(1)+2*x(2))^2-(x(1))^2)/4;
A4=(x(3)/2)^2-(x(3)/2-x(4))^2;
f=2*((pi*A1*dp.L1*dp.Rho)+(pi*A1*dp.L2*dp.Rho)+(pi*A2*dp.L3*dp.Rho)+(6*pi*A3*dp.L4*dp.Rho)+(0.097))+((pi*A4*dp.L5*dp.Rho)+(pi*A4*dp.L6*dp.Rho)+(pi*A4*dp.L7*dp.Rho)); %
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [c,ceq]=nonlinear_constraints(x,dp)
A5=(x(1))^4-(x(1)-2*x(2))^4;
c(10,1)=0.2*dp.mb*dp.g*100-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
c(11,1)=((dp.mb*dp.g/2)*(dp.Lss)^3/(48/64)*pi*A5)-dp.deltamax; %
ceq=[]; % There are no nonlinear equality constraints
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4 Kommentare
Elmira Jafari
Elmira Jafari am 15 Dez. 2021
Thanks Robert your right but even I make them similar, my problem is not solved.
Walter Roberson
Walter Roberson am 15 Dez. 2021
I broke all the functions out into separate files. I then edited wheel to call the functions you built for fmincon. It did not have any problem.
Single objective optimization:
4 Variable(s)
11 Nonlinear inequality constraint(s)
9 Linear inequality constraint(s)
Options:
CreationFcn: @gacreationlinearfeasible
CrossoverFcn: @crossoverintermediate
SelectionFcn: @selectionstochunif
MutationFcn: @mutationadaptfeasible
Best Max Stall
Generation Func-count f(x) Constraint Generations
1 10350 0.761147 0 0
2 20500 0.761047 0 0
3 38060 0.75681 0 0
4 64785 0.752895 0 0
5 95215 0.752255 0 0
6 131300 0.752225 0 0
Optimization terminated: average change in the fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.
Switching to the hybrid optimization algorithm (FMINCON).
FMINCON terminated.
Optimal value of d1: 14 mm
Optimal value of t1: 1.0029 mm
Optimal value of d2: 10 mm
Optimal value of t2: 0.50061mm
Optimal value of f: 0.75222 kg

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by