Azzera filtri
Azzera filtri

Unreconized function or variable

3 visualizzazioni (ultimi 30 giorni)
Jason
Jason il 7 Set 2023
Commentato: Matt J il 7 Set 2023
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)')

Risposte (1)

Matt J
Matt J il 7 Set 2023
Modificato: Matt J il 7 Set 2023
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 Commenti
Jason
Jason il 7 Set 2023
Wow... I knew it was simple. Thank you
Matt J
Matt J il 7 Set 2023
You're very welcome, but please Accept-click the answer to indicate that the question's been resolved.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by