kindly help in debugging the following code for optimization

3 views (last 30 days)
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.

Accepted Answer

Roger Stafford
Roger Stafford on 24 Feb 2018
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 Comment
adarsh
adarsh on 24 Feb 2018
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];

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox 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!