"Undefined function or variable 'PA'. The first assignment to a local variable determines its class.". Why do I keep getting this error?

10 ビュー (過去 30 日間)
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;

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 8 月 3 日
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 件のコメント
Ali Zubair
Ali Zubair 2022 年 8 月 3 日
i tried this,
PA = 0;
still same error.
Walter Roberson
Walter Roberson 2022 年 8 月 4 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolar Power についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by