Undefined function or variable 'lb'.

조회 수: 8 (최근 30일)
Afiqah Ismail
Afiqah Ismail 2018년 11월 23일
댓글: Afiqah Ismail 2018년 11월 23일
I have two function.The first 1 is afiqah and the second is MyCost.I wolud to call the data from afiqah to MyCost.
function[ub,lb,o]=afiqah()
lb=[57 66 22 22 17 8 32 33 36 33 18 41 80 115 105 70 57 42 17 17 12 10 11 20 36 37 20 30 35 15 20 2]
ub=[1170 1303 440 440 330 87 675 650 720 640 350 220 2200 2100 2100 1400 1139 837 405 220 240 269 220 730 710 400 600 661 150 105.5 330 29]
ak=[2759 7929 2053 7929 2053 1647 2180 3766 2525 4680 3938 4608 5261 4608 1300 817 1226 1340 2759 5507 9278 10815 6235 2759 2013 83 1250 725 313 313 13.97 2];
bk=[7.27 7.3 7.33 7.35 7.23 7.39 7.39 7.42 8.3 8.4 8.51 8.4 8.13 8.15 8.17 8.18 8.27 8.78 9.06 8.89 9.14 9.25 9.27 9.33 9.37 4.57 4.91 4.73 4.59 4.59 4.94 4.31];
o=ak.*bk;
end
function [FC] = MyCost(lb,ub,o)
afiqah;
randomArray=lb+(ub-lb).*rand(1,32);
intArray=floor(randomArray);
Pgen=sum(intArray);
if(11000<=Pgen)&&(Pgen<=16000)
FC=sum(intArray.*o);
end
end
The error is
MyCost(lb, ub, o)
Undefined function or variable 'lb'.

답변 (2개)

madhan ravi
madhan ravi 2018년 11월 23일
편집: madhan ravi 2018년 11월 23일
lb=[57 66 22 22 17 8 32 33 36 33 18 41 80 115 105 70 57 42 17 17 12 10 11 20 36 37 20 30 35 15 20 2];
ub=[1170 1303 440 440 330 87 675 650 720 640 350 220 2200 2100 2100 1400 1139 837 405 220 240 269 220 730 710 400 600 661 150 105.5 330 29];
ak=[2759 7929 2053 7929 2053 1647 2180 3766 2525 4680 3938 4608 5261 4608 1300 817 1226 1340 2759 5507 9278 10815 6235 2759 2013 83 1250 725 313 313 13.97 2];
bk=[7.27 7.3 7.33 7.35 7.23 7.39 7.39 7.42 8.3 8.4 8.51 8.4 8.13 8.15 8.17 8.18 8.27 8.78 9.06 8.89 9.14 9.25 9.27 9.33 9.37 4.57 4.91 4.73 4.59 4.59 4.94 4.31];
o=ak.*bk;
FC = MyCost(lb,ub,o) %function call
function [FC] = MyCost(lb,ub,o) % function definition
randomArray=lb+(ub-lb).*rand(1,32);
intArray=floor(randomArray);
Pgen=sum(intArray);
if(11000<=Pgen)&&(Pgen<=16000)
FC=sum(intArray.*o);
end
end
  댓글 수: 1
Afiqah Ismail
Afiqah Ismail 2018년 11월 23일
Both functions are in different tab .I would to call the data from afiqah to MyCost.So that i can get the value of FC in the MyCost.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2018년 11월 23일
You call your afiqah function with 0 output arguments inside your MyCost function. That means when the afiqah function finishes executing, it throws away the variables lb, ub, and o you defined inside it since the MyCost function never asked for those variables. Call afiqah with three outputs inside MyCost if you want the contents of the variables defined in it to be available to MyCost.
  댓글 수: 1
Afiqah Ismail
Afiqah Ismail 2018년 11월 23일
Can you show example.I am new in matlab.
Both functions are in different tab.When i try to run the MyCost there will be error
MyCost(lb, ub, o)
Undefined function or variable 'lb'.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by