kindly help in debugging the following code for optimization

2 次查看(过去 30 天)
function [ f, g ] = problem_function( x ) D1=x(1);D2=x(2);D3=x(3);D4=x(4);D5=x(5);P1=x(6);P2=x(7);P3=x(8);P4=x(9);P5=x(10);V1=x(11);V2=x(12);V3=x(13);V4=x(14);V5=x(15); L1=2;L2=3;L3=4;L4=5;L5=6; L=[L1 L2 L3 L4 L5]; D=[D1 D2 D3 D4 D5]; %%%%%%%%%%%%%%%%%%%%%%%%% Q1=V1*(3.14/4)*D1^2; Q2=V2*(3.14/4)*D2^2; Q3=V3*(3.14/4)*D3^2; Q4=V4*(3.14/4)*D4^2; Q5=V5*(3.14/4)*D5^2; % Objective functions F(X) CT=2.05*L*D'.^3; %%%%%%%%%%%%%%%%%%%%%%% if all(P(1:5)>=18) CPP=0; else CPP=(CT/5)*sum(18-P(1:5)); end %%%%%%%%%%%%%%%%%%%% if all(V(1:5)<=10) CPV=0; else CPV=(CT/5)*sum(V(1:5).^2-100); end %%%%%% f=CT+CPV+CPP; % Equality constraints G(X) = 0 MUST COME FIRST in g(1:me) g(1)=P1-P2-11.7*(L1*Q1^3/D1^5); g(2)=P2-P3-11.7*(L2*Q2^3/D2^5); g(3)=P3-P4-11.7*(L3*Q3^3/D3^5); g(4)=P2-P4-11.7*(L4*Q4^3/D4^5); g(5)=P4-P5-11.7*(L5*Q5^3/D5^5); g(6)= 40-Q1; g(7)=Q1-Q2-Q4; g(8)=Q2-14-Q3; g(9)=Q3+Q4-Q5;
end %%%% the result say Undefined function or variable 'P'.
Error in example>problem_function (line 82) if all(P(1:5)>=18) any helps plz.

采纳的回答

Roger Stafford
Roger Stafford 2018-2-24
Matlab's error message is quite true - you have not defined P. Evidently you intended to have:
P = [P1 P2 P3 P4 P5];
but you did not write it!
It should be pointed out that you need not have had so many lines of code. For example, assuming x is a row vector, you could have written:
L = 2:6;
D = x(1:5);
P = x(6:10);
V = x(11:15);
Q = 3.14/4*V.*D.^2;
and so forth.
  1 个评论
adarsh
adarsh 2018-2-24
i have done like this: D=x(1:5); P=x(6:10); V=x(11:15) %D1=x(1);D2=x(2);D3=x(3);D4=x(4);D5=x(5);P1=x(6);P2=x(7);P3=x(8);P4=x(9);P5=x(10);V1=x(11);V2=x(12);V3=x(13);V4=x(14);V5=x(15); L1=2;L2=3;L3=4;L4=5;L5=6; L=[L1 L2 L3 L4 L5]; D=[D1 D2 D3 D4 D5]; P=[P1 P2 P3 P4 P5]; V=[V1 V2 V3 V4 V5]; %%%%%%%%%%%%%%%%%%%%%%%%% Q1=V1*(3.14/4)*D1^2; Q2=V2*(3.14/4)*D2^2; Q3=V3*(3.14/4)*D3^2; Q4=V4*(3.14/4)*D4^2; Q5=V5*(3.14/4)*D5^2; % Objective functions F(X) CT=2.05*L*D'.^3; %%%%%%%%%%%%%%%%%%%%%%%
if all(P(1:5)>=18) CPP=0; else CPP=(CT/5)*sum(18-P(1:5)); end %%%%%%%%%%%%%%%%%%%% if all(V(1:5)<=10) CPV=0; else CPV=(CT/5)*sum(V(1:5).^2-100); end %%%%%% f=CT+CPV+CPP; % Equality constraints G(X) = 0 MUST COME FIRST in g(1:me) g(1)=P1-P2-11.7*(L1*Q1^3/D1^5); g(2)=P2-P3-11.7*(L2*Q2^3/D2^5); g(3)=P3-P4-11.7*(L3*Q3^3/D3^5); g(4)=P2-P4-11.7*(L4*Q4^3/D4^5); g(5)=P4-P5-11.7*(L5*Q5^3/D5^5); g(6)= 400-Q1; g(7)=Q1-Q2-Q4; g(8)=Q2-14-Q3; g(9)=Q3+Q4-Q5;
end %%%%% 1. But now in the results it says : Undefined function or variable 'D1'.
Error in example>problem_function (line 75) D=[D1 D2 D3 D4 D5];

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by