Undefined function or variable 'lb'.

5 ビュー (過去 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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by