Error using fmincon Too many input arguments

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 个评论

Works for me ?
Wheelchair()
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 5 1.849113e+00 0.000e+00 2.879e+02 1 16 1.682777e+00 0.000e+00 7.068e+01 1.137e-03 2 21 1.051250e+00 0.000e+00 4.107e+02 1.876e-02 3 26 9.346769e-01 0.000e+00 4.646e+01 1.911e-03 4 37 8.589080e-01 0.000e+00 1.529e+01 2.648e-04 5 68 8.589079e-01 0.000e+00 1.529e+01 2.200e-10 6 77 8.589079e-01 0.000e+00 1.529e+01 2.200e-10 7 87 8.589079e-01 0.000e+00 1.529e+01 2.200e-10 8 97 8.589079e-01 0.000e+00 1.529e+01 2.200e-10 9 107 8.589079e-01 0.000e+00 1.529e+01 2.200e-10 10 117 8.589078e-01 0.000e+00 1.529e+01 2.200e-10 11 127 8.589078e-01 0.000e+00 1.529e+01 2.200e-10 12 137 8.589078e-01 0.000e+00 1.529e+01 2.200e-10 13 147 8.589078e-01 0.000e+00 1.529e+01 2.200e-10 14 157 8.589077e-01 0.000e+00 1.529e+01 2.200e-10 15 167 8.589077e-01 0.000e+00 1.529e+01 2.200e-10 16 177 8.589077e-01 0.000e+00 1.529e+01 2.200e-10 17 187 8.589077e-01 0.000e+00 1.529e+01 2.200e-10 18 197 8.589077e-01 0.000e+00 1.529e+01 2.200e-10 19 207 8.589076e-01 0.000e+00 1.529e+01 2.200e-10 20 217 8.589076e-01 0.000e+00 1.529e+01 2.200e-10 21 227 8.589076e-01 0.000e+00 1.529e+01 2.200e-10 22 237 8.589076e-01 0.000e+00 1.529e+01 2.200e-10 23 247 8.589076e-01 0.000e+00 1.529e+01 2.200e-10 24 257 8.589075e-01 0.000e+00 1.529e+01 2.200e-10 25 267 8.589075e-01 0.000e+00 1.529e+01 2.200e-10 26 277 8.589075e-01 0.000e+00 1.529e+01 2.200e-10 27 287 8.589075e-01 0.000e+00 1.529e+01 2.200e-10 28 297 8.589074e-01 0.000e+00 1.529e+01 2.200e-10 29 307 8.589074e-01 0.000e+00 1.529e+01 2.200e-10 30 317 8.589074e-01 0.000e+00 1.529e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 31 327 8.589074e-01 0.000e+00 1.529e+01 2.200e-10 32 337 8.589074e-01 0.000e+00 1.529e+01 2.200e-10 33 347 8.589073e-01 0.000e+00 1.529e+01 2.200e-10 34 357 8.589073e-01 0.000e+00 1.529e+01 2.200e-10 35 367 8.589073e-01 0.000e+00 1.529e+01 2.200e-10 36 377 8.589073e-01 0.000e+00 1.529e+01 2.200e-10 37 387 8.589073e-01 0.000e+00 1.529e+01 2.200e-10 38 397 8.589072e-01 0.000e+00 1.529e+01 2.200e-10 39 407 8.589072e-01 0.000e+00 1.529e+01 2.200e-10 40 417 8.589072e-01 0.000e+00 1.529e+01 2.200e-10 41 427 8.589072e-01 0.000e+00 1.529e+01 2.200e-10 42 437 8.589071e-01 0.000e+00 1.529e+01 2.200e-10 43 447 8.589071e-01 0.000e+00 1.529e+01 2.200e-10 44 457 8.589071e-01 0.000e+00 1.529e+01 2.200e-10 45 467 8.589071e-01 0.000e+00 1.529e+01 2.200e-10 46 477 8.589071e-01 0.000e+00 1.529e+01 2.200e-10 47 487 8.589070e-01 0.000e+00 1.529e+01 2.200e-10 48 497 8.589070e-01 0.000e+00 1.529e+01 2.200e-10 49 507 8.589070e-01 0.000e+00 1.529e+01 2.200e-10 50 517 8.589070e-01 0.000e+00 1.529e+01 2.200e-10 51 527 8.589070e-01 0.000e+00 1.529e+01 2.200e-10 52 537 8.589069e-01 0.000e+00 1.529e+01 2.200e-10 53 547 8.589069e-01 0.000e+00 1.529e+01 2.200e-10 54 557 8.589069e-01 0.000e+00 1.529e+01 2.200e-10 55 567 8.589069e-01 0.000e+00 1.529e+01 2.200e-10 56 577 8.589068e-01 0.000e+00 1.529e+01 2.200e-10 57 587 8.589068e-01 0.000e+00 1.529e+01 2.200e-10 58 597 8.589068e-01 0.000e+00 1.529e+01 2.200e-10 59 607 8.589068e-01 0.000e+00 1.529e+01 2.200e-10 60 617 8.589068e-01 0.000e+00 1.529e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 61 627 8.589067e-01 0.000e+00 1.529e+01 2.200e-10 62 637 8.589067e-01 0.000e+00 1.529e+01 2.200e-10 63 647 8.589067e-01 0.000e+00 1.529e+01 2.200e-10 64 657 8.589067e-01 0.000e+00 1.529e+01 2.200e-10 65 667 8.589067e-01 0.000e+00 1.529e+01 2.200e-10 66 677 8.589066e-01 0.000e+00 1.529e+01 2.200e-10 67 687 8.589066e-01 0.000e+00 1.529e+01 2.200e-10 68 697 8.589066e-01 0.000e+00 1.529e+01 2.200e-10 69 707 8.589066e-01 0.000e+00 1.529e+01 2.200e-10 70 717 8.589065e-01 0.000e+00 1.529e+01 2.200e-10 71 727 8.589065e-01 0.000e+00 1.529e+01 2.200e-10 72 737 8.589065e-01 0.000e+00 1.529e+01 2.200e-10 73 747 8.589065e-01 0.000e+00 1.529e+01 2.200e-10 74 757 8.589065e-01 0.000e+00 1.529e+01 2.200e-10 75 767 8.589064e-01 0.000e+00 1.529e+01 2.200e-10 76 777 8.589064e-01 0.000e+00 1.529e+01 2.200e-10 77 787 8.589064e-01 0.000e+00 1.529e+01 2.200e-10 78 797 8.589064e-01 0.000e+00 1.529e+01 2.200e-10 79 807 8.589064e-01 0.000e+00 1.529e+01 2.200e-10 80 817 8.589063e-01 0.000e+00 1.529e+01 2.200e-10 81 827 8.589063e-01 0.000e+00 1.529e+01 2.200e-10 82 837 8.589063e-01 0.000e+00 1.529e+01 2.200e-10 83 847 8.589063e-01 0.000e+00 1.529e+01 2.200e-10 84 857 8.589063e-01 0.000e+00 1.529e+01 2.200e-10 85 867 8.589062e-01 0.000e+00 1.529e+01 2.200e-10 86 877 8.589062e-01 0.000e+00 1.529e+01 2.200e-10 87 887 8.589062e-01 0.000e+00 1.529e+01 2.200e-10 88 897 8.589062e-01 0.000e+00 1.529e+01 2.200e-10 89 907 8.589061e-01 0.000e+00 1.529e+01 2.200e-10 90 917 8.589061e-01 0.000e+00 1.529e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 91 927 8.589061e-01 0.000e+00 1.529e+01 2.200e-10 92 937 8.589061e-01 0.000e+00 1.529e+01 2.200e-10 93 947 8.589061e-01 0.000e+00 1.529e+01 2.200e-10 94 957 8.589060e-01 0.000e+00 1.529e+01 2.200e-10 95 967 8.589060e-01 0.000e+00 1.529e+01 2.200e-10 96 977 8.589060e-01 0.000e+00 1.529e+01 2.200e-10 97 987 8.589060e-01 0.000e+00 1.529e+01 2.200e-10 98 997 8.589060e-01 0.000e+00 1.529e+01 2.200e-10 99 1007 8.589059e-01 0.000e+00 1.529e+01 2.200e-10 100 1017 8.589059e-01 0.000e+00 1.529e+01 2.200e-10 101 1027 8.589059e-01 0.000e+00 1.529e+01 2.200e-10 102 1037 8.589059e-01 0.000e+00 1.529e+01 2.200e-10 103 1047 8.589058e-01 0.000e+00 1.529e+01 2.200e-10 104 1057 8.589058e-01 0.000e+00 1.529e+01 2.200e-10 105 1067 8.589058e-01 0.000e+00 1.529e+01 2.200e-10 106 1077 8.589058e-01 0.000e+00 1.529e+01 2.200e-10 107 1087 8.589058e-01 0.000e+00 1.529e+01 2.200e-10 108 1097 8.589057e-01 0.000e+00 1.529e+01 2.200e-10 109 1107 8.589057e-01 0.000e+00 1.529e+01 2.200e-10 110 1117 8.589057e-01 0.000e+00 1.529e+01 2.200e-10 111 1127 8.589057e-01 0.000e+00 1.529e+01 2.200e-10 112 1137 8.589057e-01 0.000e+00 1.529e+01 2.200e-10 113 1147 8.589056e-01 0.000e+00 1.529e+01 2.200e-10 114 1157 8.589056e-01 0.000e+00 1.529e+01 2.200e-10 115 1167 8.589056e-01 0.000e+00 1.529e+01 2.200e-10 116 1177 8.589056e-01 0.000e+00 1.529e+01 2.200e-10 117 1187 8.589055e-01 0.000e+00 1.529e+01 2.200e-10 118 1197 8.589055e-01 0.000e+00 1.529e+01 2.200e-10 119 1207 8.589055e-01 0.000e+00 1.529e+01 2.200e-10 120 1217 8.589055e-01 0.000e+00 1.529e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 121 1227 8.589055e-01 0.000e+00 1.529e+01 2.200e-10 122 1237 8.589054e-01 0.000e+00 1.529e+01 2.200e-10 123 1247 8.589054e-01 0.000e+00 1.529e+01 2.200e-10 124 1257 8.589054e-01 0.000e+00 1.529e+01 2.200e-10 125 1267 8.589054e-01 0.000e+00 1.529e+01 2.200e-10 126 1277 8.589054e-01 0.000e+00 1.529e+01 2.200e-10 127 1287 8.589053e-01 0.000e+00 1.529e+01 2.200e-10 128 1297 8.589053e-01 0.000e+00 1.529e+01 2.200e-10 129 1307 8.589053e-01 0.000e+00 1.529e+01 2.200e-10 130 1317 8.589053e-01 0.000e+00 1.529e+01 2.200e-10 131 1327 8.589052e-01 0.000e+00 1.529e+01 2.200e-10 132 1337 8.589052e-01 0.000e+00 1.529e+01 2.200e-10 133 1347 8.589052e-01 0.000e+00 1.529e+01 2.200e-10 134 1357 8.589052e-01 0.000e+00 1.529e+01 2.200e-10 135 1367 8.589052e-01 0.000e+00 1.529e+01 2.200e-10 136 1377 8.589051e-01 0.000e+00 1.529e+01 2.200e-10 137 1387 8.589051e-01 0.000e+00 1.529e+01 2.200e-10 138 1397 8.589051e-01 0.000e+00 1.529e+01 2.200e-10 139 1407 8.589051e-01 0.000e+00 1.529e+01 2.200e-10 140 1417 8.589051e-01 0.000e+00 1.529e+01 2.200e-10 141 1427 8.589050e-01 0.000e+00 1.529e+01 2.200e-10 142 1437 8.589050e-01 0.000e+00 1.529e+01 2.200e-10 143 1447 8.589050e-01 0.000e+00 1.529e+01 2.200e-10 144 1457 8.589050e-01 0.000e+00 1.529e+01 2.200e-10 145 1467 8.589049e-01 0.000e+00 1.529e+01 2.200e-10 146 1477 8.589049e-01 0.000e+00 1.529e+01 2.200e-10 147 1487 8.589049e-01 0.000e+00 1.529e+01 2.200e-10 148 1497 8.589049e-01 0.000e+00 1.529e+01 2.200e-10 149 1507 8.589049e-01 0.000e+00 1.529e+01 2.200e-10 150 1517 8.589048e-01 0.000e+00 1.529e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 151 1527 8.589048e-01 0.000e+00 1.529e+01 2.200e-10 152 1537 8.589048e-01 0.000e+00 1.529e+01 2.200e-10 153 1547 8.589048e-01 0.000e+00 1.529e+01 2.200e-10 154 1557 8.589048e-01 0.000e+00 1.529e+01 2.200e-10 155 1567 8.589047e-01 0.000e+00 1.529e+01 2.200e-10 156 1577 8.589047e-01 0.000e+00 1.529e+01 2.200e-10 157 1587 8.589047e-01 0.000e+00 1.529e+01 2.200e-10 158 1597 8.589047e-01 0.000e+00 1.529e+01 2.200e-10 159 1607 8.589046e-01 0.000e+00 1.528e+01 2.200e-10 160 1617 8.589046e-01 0.000e+00 1.528e+01 2.200e-10 161 1627 8.589046e-01 0.000e+00 1.528e+01 2.200e-10 162 1637 8.589046e-01 0.000e+00 1.528e+01 2.200e-10 163 1647 8.589046e-01 0.000e+00 1.528e+01 2.200e-10 164 1657 8.589045e-01 0.000e+00 1.528e+01 2.200e-10 165 1667 8.589045e-01 0.000e+00 1.528e+01 2.200e-10 166 1677 8.589045e-01 0.000e+00 1.528e+01 2.200e-10 167 1687 8.589045e-01 0.000e+00 1.528e+01 2.200e-10 168 1697 8.589045e-01 0.000e+00 1.528e+01 2.200e-10 169 1707 8.589044e-01 0.000e+00 1.528e+01 2.200e-10 170 1717 8.589044e-01 0.000e+00 1.528e+01 2.200e-10 171 1727 8.589044e-01 0.000e+00 1.528e+01 2.200e-10 172 1737 8.589044e-01 0.000e+00 1.528e+01 2.200e-10 173 1747 8.589043e-01 0.000e+00 1.528e+01 2.200e-10 174 1757 8.589043e-01 0.000e+00 1.528e+01 2.200e-10 175 1767 8.589043e-01 0.000e+00 1.528e+01 2.200e-10 176 1777 8.589043e-01 0.000e+00 1.528e+01 2.200e-10 177 1787 8.589043e-01 0.000e+00 1.528e+01 2.200e-10 178 1797 8.589042e-01 0.000e+00 1.528e+01 2.200e-10 179 1807 8.589042e-01 0.000e+00 1.528e+01 2.200e-10 180 1817 8.589042e-01 0.000e+00 1.528e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 181 1827 8.589042e-01 0.000e+00 1.528e+01 2.200e-10 182 1837 8.589042e-01 0.000e+00 1.528e+01 2.200e-10 183 1847 8.589041e-01 0.000e+00 1.528e+01 2.200e-10 184 1857 8.589041e-01 0.000e+00 1.528e+01 2.200e-10 185 1867 8.589041e-01 0.000e+00 1.528e+01 2.200e-10 186 1877 8.589041e-01 0.000e+00 1.528e+01 2.200e-10 187 1887 8.589040e-01 0.000e+00 1.528e+01 2.200e-10 188 1897 8.589040e-01 0.000e+00 1.528e+01 2.200e-10 189 1907 8.589040e-01 0.000e+00 1.528e+01 2.200e-10 190 1917 8.589040e-01 0.000e+00 1.528e+01 2.200e-10 191 1927 8.589040e-01 0.000e+00 1.528e+01 2.200e-10 192 1937 8.589039e-01 0.000e+00 1.528e+01 2.200e-10 193 1947 8.589039e-01 0.000e+00 1.528e+01 2.200e-10 194 1957 8.589039e-01 0.000e+00 1.528e+01 2.200e-10 195 1967 8.589039e-01 0.000e+00 1.528e+01 2.200e-10 196 1977 8.589039e-01 0.000e+00 1.528e+01 2.200e-10 197 1987 8.589038e-01 0.000e+00 1.528e+01 2.200e-10 198 1997 8.589038e-01 0.000e+00 1.528e+01 2.200e-10 199 2007 8.589038e-01 0.000e+00 1.528e+01 2.200e-10 200 2017 8.589038e-01 0.000e+00 1.528e+01 2.200e-10 201 2027 8.589038e-01 0.000e+00 1.528e+01 2.200e-10 202 2037 8.589037e-01 0.000e+00 1.528e+01 2.200e-10 203 2047 8.589037e-01 0.000e+00 1.528e+01 2.200e-10 204 2057 8.589037e-01 0.000e+00 1.528e+01 2.200e-10 205 2067 8.589037e-01 0.000e+00 1.528e+01 2.200e-10 206 2077 8.589036e-01 0.000e+00 1.528e+01 2.200e-10 207 2087 8.589036e-01 0.000e+00 1.528e+01 2.200e-10 208 2097 8.589036e-01 0.000e+00 1.528e+01 2.200e-10 209 2107 8.589036e-01 0.000e+00 1.528e+01 2.200e-10 210 2117 8.589036e-01 0.000e+00 1.528e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 211 2127 8.589035e-01 0.000e+00 1.528e+01 2.200e-10 212 2137 8.589035e-01 0.000e+00 1.528e+01 2.200e-10 213 2147 8.589035e-01 0.000e+00 1.528e+01 2.200e-10 214 2157 8.589035e-01 0.000e+00 1.528e+01 2.200e-10 215 2167 8.589035e-01 0.000e+00 1.528e+01 2.200e-10 216 2177 8.589034e-01 0.000e+00 1.528e+01 2.200e-10 217 2187 8.589034e-01 0.000e+00 1.528e+01 2.200e-10 218 2197 8.589034e-01 0.000e+00 1.528e+01 2.200e-10 219 2207 8.589034e-01 0.000e+00 1.528e+01 2.200e-10 220 2217 8.589033e-01 0.000e+00 1.528e+01 2.200e-10 221 2227 8.589033e-01 0.000e+00 1.528e+01 2.200e-10 222 2237 8.589033e-01 0.000e+00 1.528e+01 2.200e-10 223 2247 8.589033e-01 0.000e+00 1.528e+01 2.200e-10 224 2257 8.589033e-01 0.000e+00 1.528e+01 2.200e-10 225 2267 8.589032e-01 0.000e+00 1.528e+01 2.200e-10 226 2277 8.589032e-01 0.000e+00 1.528e+01 2.200e-10 227 2287 8.589032e-01 0.000e+00 1.528e+01 2.200e-10 228 2297 8.589032e-01 0.000e+00 1.528e+01 2.200e-10 229 2307 8.589032e-01 0.000e+00 1.528e+01 2.200e-10 230 2317 8.589031e-01 0.000e+00 1.528e+01 2.200e-10 231 2327 8.589031e-01 0.000e+00 1.528e+01 2.200e-10 232 2337 8.589031e-01 0.000e+00 1.528e+01 2.200e-10 233 2347 8.589031e-01 0.000e+00 1.528e+01 2.200e-10 234 2357 8.589030e-01 0.000e+00 1.528e+01 2.200e-10 235 2367 8.589030e-01 0.000e+00 1.528e+01 2.200e-10 236 2377 8.589030e-01 0.000e+00 1.528e+01 2.200e-10 237 2387 8.589030e-01 0.000e+00 1.528e+01 2.200e-10 238 2397 8.589030e-01 0.000e+00 1.528e+01 2.200e-10 239 2407 8.589029e-01 0.000e+00 1.528e+01 2.200e-10 240 2417 8.589029e-01 0.000e+00 1.528e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 241 2427 8.589029e-01 0.000e+00 1.528e+01 2.200e-10 242 2437 8.589029e-01 0.000e+00 1.528e+01 2.200e-10 243 2447 8.589029e-01 0.000e+00 1.528e+01 2.200e-10 244 2457 8.589028e-01 0.000e+00 1.528e+01 2.200e-10 245 2467 8.589028e-01 0.000e+00 1.528e+01 2.200e-10 246 2477 8.589028e-01 0.000e+00 1.528e+01 2.200e-10 247 2487 8.589028e-01 0.000e+00 1.528e+01 2.200e-10 248 2497 8.589027e-01 0.000e+00 1.528e+01 2.200e-10 249 2507 8.589027e-01 0.000e+00 1.528e+01 2.200e-10 250 2517 8.589027e-01 0.000e+00 1.528e+01 2.200e-10 251 2527 8.589027e-01 0.000e+00 1.528e+01 2.200e-10 252 2537 8.589027e-01 0.000e+00 1.528e+01 2.200e-10 253 2547 8.589026e-01 0.000e+00 1.528e+01 2.200e-10 254 2557 8.589026e-01 0.000e+00 1.528e+01 2.200e-10 255 2567 8.589026e-01 0.000e+00 1.528e+01 2.200e-10 256 2577 8.589026e-01 0.000e+00 1.528e+01 2.200e-10 257 2587 8.589026e-01 0.000e+00 1.528e+01 2.200e-10 258 2597 8.589025e-01 0.000e+00 1.528e+01 2.200e-10 259 2607 8.589025e-01 0.000e+00 1.528e+01 2.200e-10 260 2617 8.589025e-01 0.000e+00 1.528e+01 2.200e-10 261 2627 8.589025e-01 0.000e+00 1.528e+01 2.200e-10 262 2637 8.589024e-01 0.000e+00 1.528e+01 2.200e-10 263 2647 8.589024e-01 0.000e+00 1.528e+01 2.200e-10 264 2657 8.589024e-01 0.000e+00 1.528e+01 2.200e-10 265 2667 8.589024e-01 0.000e+00 1.528e+01 2.200e-10 266 2677 8.589024e-01 0.000e+00 1.528e+01 2.200e-10 267 2687 8.589023e-01 0.000e+00 1.528e+01 2.200e-10 268 2697 8.589023e-01 0.000e+00 1.528e+01 2.200e-10 269 2707 8.589023e-01 0.000e+00 1.528e+01 2.200e-10 270 2717 8.589023e-01 0.000e+00 1.528e+01 2.200e-10 First-order Norm of Iter F-count f(x) Feasibility optimality step 271 2727 8.589023e-01 0.000e+00 1.528e+01 2.200e-10 272 2737 8.589022e-01 0.000e+00 1.528e+01 2.200e-10 273 2747 8.589022e-01 0.000e+00 1.528e+01 2.200e-10 274 2757 8.589022e-01 0.000e+00 1.528e+01 2.200e-10 275 2767 8.589022e-01 0.000e+00 1.528e+01 2.200e-10 276 2777 8.589021e-01 0.000e+00 1.528e+01 2.200e-10 277 2787 8.589021e-01 0.000e+00 1.528e+01 2.200e-10 278 2797 8.589021e-01 0.000e+00 1.528e+01 2.200e-10 279 2807 8.589021e-01 0.000e+00 1.528e+01 2.200e-10 280 2817 8.589021e-01 0.000e+00 1.528e+01 2.200e-10 281 2827 8.589020e-01 0.000e+00 1.528e+01 2.200e-10 282 2837 8.589020e-01 0.000e+00 1.528e+01 2.200e-10 283 2847 8.589020e-01 0.000e+00 1.528e+01 2.200e-10 284 2857 8.589020e-01 0.000e+00 1.528e+01 2.200e-10 285 2867 8.589020e-01 0.000e+00 1.528e+01 2.200e-10 286 2877 8.589019e-01 0.000e+00 1.528e+01 2.200e-10 287 2887 8.589019e-01 0.000e+00 1.528e+01 2.200e-10 288 2897 8.589019e-01 0.000e+00 1.528e+01 2.200e-10 289 2907 8.589019e-01 0.000e+00 1.528e+01 2.200e-10 290 2917 8.589018e-01 0.000e+00 1.528e+01 2.200e-10 291 2927 8.589018e-01 0.000e+00 1.528e+01 2.200e-10 292 2937 8.589018e-01 0.000e+00 1.528e+01 2.200e-10 293 2947 8.589018e-01 0.000e+00 1.528e+01 2.200e-10 294 2957 8.589018e-01 0.000e+00 1.528e+01 2.200e-10 295 2967 8.589017e-01 0.000e+00 1.528e+01 2.200e-10 296 2977 8.589017e-01 0.000e+00 1.528e+01 2.200e-10 297 2987 8.589017e-01 0.000e+00 1.528e+01 2.200e-10 298 2997 8.589017e-01 0.000e+00 1.528e+01 2.200e-10 Solver stopped prematurely. fmincon stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 3.000000e+03. Optimal value of d1: 0.020019 mm Optimal value of t1: 0.0010257 mm Optimal value of d2: 0.0010639 mm Optimal value of t2: 0.0011383mm Optimal value of f: 0.8589 kg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Thanks. yes I. changed its location now it works correctly. I dont know what wrong was with it
sorry but I got the same error for solving by GA, Would you run this one as well?
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.x(1)_min dp.d_max dp.D_min dp.D_max dp.N_min dp.N_max]);
%xlabel('d'); ylabel('D'); zlabel('N');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Defining various GA options:
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),x(4),'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=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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
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; %
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.

请先登录,再进行评论。

回答(2 个)

You may use scatter3
figure(1);
scatter3(x(1),x(2),x(3),[],x(4),'filled');

3 个评论

it works but as you said I have 4 variables, how can I have 2 plots , one of the 2 variable with objective function and the other with other 2 variables and objective function. would you guide me?
Then may you are expecting:
plot(x(1),x(2),'.b',x(3),x(4),'.r');
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

请先登录,再进行评论。

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 个评论

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;
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); %
% ^^^^^^^^
Thanks Robert your right but even I make them similar, my problem is not solved.
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Optimization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by