Unreconized function or variable

2 次查看(过去 30 天)
Jason
Jason 2023-9-7
评论: Matt J 2023-9-7
Unrecognized function or variable 'vR'.
Error in rectifier_code (line 60)
plot(t,vS,'--',t,vR)
I am starring at this program trying to understand why I cannot get it to run. Is it the for loop that is not defined properly that is causing the program not to define vR?
% Specify amplitude of source voltage
A = 1;
% Specify the frequency (in Hz) of the source voltage
f = 60;
% Specify the value of the capacitor (in micro-Farads)
CuF = 10;
% Specify the value of the capacitor (in Farads)
C = CuF*1e-6;
% Specify the value of the resistor (in Ohms)
R = 2000; % 2 kilo-Ohm
% Specify a time-vector ranging from 0 to 70 milliseconds in increments of
% 0.1 milliseconds
t = 0:0.1e-3:70e-3; %%% EXPRESSION FOR TIME VECTOR
% Specify the source voltage over all time
vS = A*sin(2*pi*f*t);
% The SWITCH variable is 'diode' and will take on character values of
% either 'on' or 'off'. Initialize to 'on'.
diode = 'on';
% Loop over the time vector
for k = 1:length(t) %%% FOR-LOOP CODE WITH LOOP VARIABLE k
% SET UP SWITCH STRUCTURE
switch diode %%% SWITCH CODE HERE
% RUN THE CASE WHEN THE DIODE IS ON
case 1 %%% CASE CODE HERE
vR(k) = vS(k); % resistor voltage is equal to source voltage
iR(k) = vR(k)/R; % i = v/R (Ohm's law)
iC(k) = C * 2*pi*f * A*cos(2*pi*f*t(k)); % capacitor current
iD(k) = iR(k) + iC(k); % current through the diode
if iD(k) <= 0,
diode = 'off';
t_off = t(k); % keep track of the time of diode turn-off
end
% RUN THE CASE WHEN THE DIODE IS OFF
case 2 %%% CASE CODE HERE
vR(k) = A*sin(2*pi*f*t_off) * exp(-(t(k) - t_off) / (R*C) );
if vS(k) >= vR(k);
diode = 'on';
end
end
end
% Plot the source and resistor voltages
plot(t,vS,'--',t,vR)
Unrecognized function or variable 'vR'.
axis([0 70e-3 -1.5 1.5])
legend('Source voltage','Resistor voltage')
title(strcat('Half-wave Diode Rectifier for C =',num2str(CuF),'\muF'))
ylabel('voltage (volts)')
xlabel('time (seconds)')

回答(1 个)

Matt J
Matt J 2023-9-7
编辑:Matt J 2023-9-7
vR is never defined because none of your switch cases are ever satisfied. One solution:
switch diode %%% SWITCH CODE HERE
case 'on' % RUN THE CASE WHEN THE DIODE IS ON
....
case 'off' % RUN THE CASE WHEN THE DIODE IS OFF
....
otherwise
error 'Unrecognized case';
end
  2 个评论
Jason
Jason 2023-9-7
Wow... I knew it was simple. Thank you
Matt J
Matt J 2023-9-7
You're very welcome, but please Accept-click the answer to indicate that the question's been resolved.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by