Error using fmincon Too many input arguments
Show older comments
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 Comments
Works for me ?
Wheelchair()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
%%%%%%%%%%%%%%%%%%%%%%%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48/64)*pi*A5)-dp.deltamax; %
ceq=[]; % There are no nonlinear equality constraints
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Elmira Jafari
on 15 Dec 2021
Elmira Jafari
on 15 Dec 2021
Edited: Walter Roberson
on 15 Dec 2021
Walter Roberson
on 15 Dec 2021
That works for me as well, until the very end. You have
plot3(x(1),x(2),x(3),x(4),'b.','Markersize',20);
plot3() requires that the number of data arguments is a multiple of 3, whereas you have 4 data arguments.
If you had been using plot() instead of plot3() then you would have plotted two points.
I would suggest to you that plotting two points would be a bit difficult to understand unless perhaps you had a legend() that told you which point was which.
Torsten
on 16 Dec 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
on 16 Dec 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.
Answers (2)
KSSV
on 15 Dec 2021
You may use scatter3
figure(1);
scatter3(x(1),x(2),x(3),[],x(4),'filled');
3 Comments
Elmira Jafari
on 15 Dec 2021
KSSV
on 15 Dec 2021
Then may you are expecting:
plot(x(1),x(2),'.b',x(3),x(4),'.r');
Elmira Jafari
on 15 Dec 2021
Edited: Walter Roberson
on 15 Dec 2021
Elmira Jafari
on 15 Dec 2021
Edited: Walter Roberson
on 15 Dec 2021
4 Comments
Walter Roberson
on 15 Dec 2021
You are not asking them to solve the same problem.
GA:
dp.E=70e9;
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;
fmincon:
dp.E=68e9;
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;
Walter Roberson
on 15 Dec 2021
Different constraints too.
GA:
c(10,1)=0.2*dp.mb*dp.g-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
% ^^^^
fmincon:
c(10,1)=0.2*dp.mb*dp.g*100-((pi.^3* dp.E/(dp.K*dp.Lad)^2*64)*A5); %
% ^^^^^^^^
Elmira Jafari
on 15 Dec 2021
Walter Roberson
on 15 Dec 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
Categories
Find more on Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!