"Undefined function or variable 'PA'. The first assignment to a local variable determines its class.". Why do I keep getting this error?
Show older comments
I have a function for my MPPT which i will implement or arduino using simulink but whenever i try to run the simulation it shows me this error "Undefined function or variable 'PA'. The first assignment to a local variable determines its class."

function D= fcn(VA,IA)
persistent VAprev
persistent PAprev
persistent Dprev
if isempty(VAprev)
VAprev = 0;
end
if isempty(PAprev)
PAprev = 0;
end
if isempty(Dprev)
Dprev = 0.5;
end
dataType = 'double';
double(PA);
double(IA);
D=Dprev;
PA=VA*IA;
DeltaVA= VA-VAprev;
DeltaPA= PA-PAprev;
if DeltaPA>0
if DeltaVA>0
D=Dprev - 0.0001;
elseif DeltaVA<0
D=Dprev +0.0001;
end
elseif DeltaPA<0
if DeltaVA>0
D=Dprev+0.0001;
elseif DeltaVA<0
D=Dprev-0.0001;
end
end
if D>0.9
D=0.9;
elseif D<0
D=0;
end
VAprev=VA;
PAprev=PA;
Dprev=D;
Answers (1)
Walter Roberson
on 3 Aug 2022
double(PA);
double(IA);
D=Dprev;
PA=VA*IA;
You do something (useless) with PA before you assign to it.
Remember in MATLAB a type name used like a function is not a type declaration, it is a type conversion.
PA = 0;
might be suitable
2 Comments
Ali Zubair
on 3 Aug 2022
Walter Roberson
on 4 Aug 2022
function D = fcn(VA,IA)
persistent VAprev
persistent PAprev
persistent Dprev
if isempty(VAprev)
VAprev = 0;
end
if isempty(PAprev)
PAprev = 0;
end
if isempty(Dprev)
Dprev = 0.5;
end
D = 0;
IA = 0;
PA = 0;
PV = 0;
DeltaVA = 0;
DeltaPA = 0;
D = Dprev;
PA = VA*IA;
DeltaVA = VA-VAprev;
DeltaPA = PA-PAprev;
if DeltaPA>0
if DeltaVA>0
D = Dprev - 0.0001;
elseif DeltaVA<0
D = Dprev + 0.0001;
end
elseif DeltaPA<0
if DeltaVA>0
D = Dprev + 0.0001;
elseif DeltaVA<0
D = Dprev - 0.0001;
end
end
if D>0.9
D = 0.9;
elseif D<0
D = 0;
end
VAprev = VA;
PAprev = PA;
Dprev = D;
end
Categories
Find more on Solar Power 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!