The variable already defined ,but in the design apps it cannot recognized the variable.
Show older comments
Unrecognized function or variable 'A'.
Error in Tariff (line 21)
F6 = A+B+C;
function Bill = Tariff(Energy)
if Energy <=200
A = Energy*0.218;
B=0; C=0; D=0; E=0; P=0.4; EST= 0;
elseif Energy <=300
A = 43.6;
B = (Energy-200)*0.334;
C=0; D=0; E=0; P=0.15; EST= 0;
elseif Energy <=600
A = 43.6; B = 33.4;
C = (Energy-300)*0.516;
D=0; E=0; P=0.1; EST= 0;
elseif Energy <= 900
A = 43.6; B = 33.4; C = 154.8;
D = (Energy-600)*0.546;
E=0; P=0.05; EST= Energy-600;
elseif Energy > 900
A = 43.6; B = 33.4; C = 154.8; D = 327.6;
E = (Energy-900)*0.571; P=0; EST= Energy-600;
end
F6 = A+B+C;
ST = D+E;
Total = F6+ST;
ICPT = Energy*0.02;
ICPTST = EST*0.02;
Pemulih = round((Total-ICPT)*P,2);
PemulihST = round((ST-ICPTST)*P,2);
STtotal = round(ST-ICPTST-PemulihST,2);
Tax = round((STtotal)*0.06,2);
KWTBB = round((Total-Pemulih)*0.016,2);
Bill = round(Total-ICPT-Pemulih+Tax+KWTBB,1);
2 Comments
Stephen23
on 2 Nov 2021
"The variable already defined"
Nope, consider what your code does if Energy = [0,1000]
It looks like you are making the common beginner mistake of using IF/ELSEIF/ELSE when you should be using indexing.
Lee Yit Tung
on 3 Nov 2021
Accepted Answer
More Answers (1)
Jan
on 2 Nov 2021
Why do you assume, that "the variable A is already defined", if Matlab displays clearly, that it is not?
What is your input Energy? If it is an array, the expression
if Energy <=200
is interpreted as:
if all(Energy(:) <= 200)
When none of the conditions is met for all elements of Energy, the variable A is not defined.
Use the debugger to examine what's going on: Set a breakpoint in the first line and step through your code line by line. You will see, that no code is executed, wich defines A.
3 Comments
Lee Yit Tung
on 3 Nov 2021
Stephen23
on 3 Nov 2021
@Lee Yit Tung: then set DBSTOP IF ERROR and save the value of ENERGY in a mat file and upload it here by clicking on the paperclip button. Then we can help you to find out why your code does not work.
Lee Yit Tung
on 3 Nov 2021
Categories
Find more on Instrument Control 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!