MATLAB: Undefined function ‘Qb’ for input arguments of type ‘double’.

undefine variable

I have problem to get the Qb. Here is my code:
a=0.993; for j=1:length(Q) B=0.03; if (0<=B && B<=1) Qb(j)=((1-B)*a*Qb(j-1)+(1-a)*B*Q(j))/(1-a*B); elseif (B == 0) Qb(j)= a*Qb(j-1); else Qb(j)= Q(j)-Qs(j); end
end
Please help me to figure out.
Thanks!
Helen

Best Answer

There are actually two problems here. The first is the Qb(j) reference that throws the error, and the second is that the first call to Qb(j-1) is going to throw an error because in MATLAB subscripts have to be positive integers.
So, (1) preallocate Qb, and (2) start the j index at 2:
Q = 1:10; % Define ‘Q’ arbitrarily
Qb = zeros(size(Q)); % Preallocate ‘Qb’
a=0.993;
for j=2:length(Q) % Start ‘j’ at 2
B=0.03;
if (0<=B && B<=1)
Qb(j)=((1-B)*a*Qb(j-1)+(1-a)*B*Q(j))/(1-a*B);
elseif (B == 0)
Qb(j)= a*Qb(j-1);
else
Qb(j)= Q(j)-Qs(j);
end
end
I don’t know if this produces what you want, but it runs without errors. Your ‘Qs’ array also doesn’t exist in this code, but the condition didn’t trigger that reference to it when I ran your code.