Help!! (Function definitions are not permitted in this context.)
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I try to insert values into this matlab code
numer_poly = [60000];
denom_poly = [4 1];
time_delay = 0;
wrange = logspace(-3, 3, 1000);
function iok = plot_bode( numer_poly, denom_poly, time_delay, wrange);
% --- plot_bode ---
%
% iok = plot_bode( numer_poly, denom_poly, time_delay, wrange);
%
% This function plots a polar-plot for the transfer-function represented
% by the row-vectors <numer_poly> and <denom_poly> and with the time-delay
% <time_delay>. The frequency range is <wrange> (if specified).
%
% The last two arguments in the list can be omitted.
% The function is capable of choosing default values for both of these.
% ---------------------------------------------------------------------
jot = sqrt(-1);
iok = 0;
if (nargin < 3)
time_delay = 0.0;
end
if (nargin < 4) % --- Establish default frequency range. ---
ruuts_numer = roots(numer_poly);
ruuts_denom = roots(denom_poly);
all_ruuts = [ ruuts_numer; ruuts_denom ];
max_ruuts = max(abs(all_ruuts)); % Find a maximum frequency.
max_ru = max_ruuts*1000;
% --- Exercise caution to ensure that time-delay does not produce
% crazy rotations at the end.
if ( (time_delay*max_ru)/5000 > (0.1*pi) )
max_ru = 5000*(0.1*pi) / time_delay;
end
tx = max_ruuts * 1.0E-12; % Min freq. not to be lower than this.
min_ruuts = min(abs(all_ruuts)); % Find a minimum frequency.
min_ru = min_ruuts/1000;
min_ru = max([ min_ru, tx ]);
frqs = (logspace(log10(min_ru), log10(max_ru),5001))';
else
frqs = wrange;
end
% --- Now assess the orders of numerator and denominator.
order_num = max(size(numer_poly))-1;
t_num = min(size(numer_poly));
order_dnm = max(size(denom_poly))-1;
t_dnm = min(size(denom_poly));
s_values = jot*frqs;
% --- --- First compute the denominator.
t_values = s_values*0.0+1; % This will represent (s_values.^ii)
d_values = s_values*0.0; % This will represent the summation.
for ii=1:order_dnm+1;
d_values = d_values + t_values*denom_poly(order_dnm+2-ii);
t_values = t_values.*s_values;
end
% --- --- Next compute the numerator.
t_values = s_values*0.0+1; % This will represent (s_values.^ii)
n_values = s_values*0.0; % This will represent the summation.
for ii=1:order_num+1;
n_values = n_values + t_values*numer_poly(order_num+2-ii);
t_values = t_values.*s_values;
end
% --- --- Finally combine them.
xfer_values = (n_values./d_values);
disp(' '); disp(' '); td = 0.0;
% td = input(' Enter the amount of pure time-delay (s) to include : ');
phase_lags = td*frqs;
xfer_angs = unwrap(angle((xfer_values)-phase_lags));
if ( max(abs( xfer_values )) < 1)
disp(' ');
disp(' This transfer function has gain < 1 for all frequencies ');
disp(' Phase Margin is not meaningful ');
disp(' '); pause(1);
low_gain = 1;
elseif ( min(abs( xfer_values )) > 1)
disp(' ');
disp(' This transfer function has gain > 1 for all frequencies in range');
disp(' Phase Margin is not meaningful here ');
disp(' '); pause(1);
low_gain = 1;
else
disp(' ');
itx = first_crss( log(abs(xfer_values)) );
% disp([' itx = ' int2str(itx)]), pause
frq_0dB = frqs(itx); phs_0dB = xfer_angs(itx);
disp([' Frequency where gain closest to unity is ' num2str(frq_0dB) ' (rad/s) ']);
disp([' Phase at that frequency = ' num2str(phs_0dB*180/pi) ' degrees ']);
PM = phs_0dB+pi;
disp([' Phase margin = ' num2str(PM*180/pi) ' degrees ']);
disp(' '); pause(1);
low_gain = 0;
end
if ( min( xfer_angs ) > -pi );
disp(' ');
disp(' This transfer function has phase > -<pi> for all frequencies ');
disp(' Gain Margin is not meaningful ');
disp(' '); pause(1);
low_lag = 1;
else
disp(' ');
[junk,itx] = min(abs( xfer_angs + pi) );
frq_180d = frqs(itx); gaindB_180d = 20*log10(abs(xfer_values(itx)));
disp([' Freq. where phase closest to -180 degrees is ' num2str(frq_180d) ' (rad/s) ']);
disp([' Gain at that frequency = ' num2str(gaindB_180d) ' dB ']);
GM = 0-gaindB_180d;
disp([' Gain margin = ' num2str(GM) ' dB ']);
disp(' '); pause(1);
low_lag = 0;
end
figure(1);
subplot(211);
semilogx(frqs, 20*log10(abs(xfer_values)),'linewidth',2)';
grid on;
hold on;
semilogx(frqs, 0,'r-');
if (low_lag==0)
semilogx([ frq_180d; frq_180d ], [0 gaindB_180d], 'k','linewidth',2);
end
axis([ min(frqs) max(frqs) min(log10(abs(xfer_values)))*20 max(log10(abs(xfer_values)))*20 ]);
hold off;
xlabel(' Frequency (rad/s) ');
ylabel(' Gain (dBs) ');
subplot(212);
semilogx(frqs, xfer_angs*180/pi,'linewidth',2)';
grid on;
hold on;
semilogx(frqs, (frqs*0-180),'r-');
if (low_gain==0)
semilogx([ frq_0dB; frq_0dB ], [phs_0dB*180/pi -180], 'k','linewidth',2);
end
axis([ min(frqs) max(frqs) -360 180 ]);
hold off;
xlabel(' Frequency (rad/s) ');
ylabel(' Phase angle (degrees) ');
but it keeps telling me this error
??? Error: File: plot_bode.m Line: 6 Column: 1 Function definitions are not permitted in this context.
What should I do?
0 comentarios
Respuestas (1)
C.J. Harris
el 22 de Mzo. de 2012
What you are doing is mixing a script and a function within a single m-file. Remove the first four lines:
numer_poly = [60000];
denom_poly = [4 1];
time_delay = 0;
wrange = logspace(-3, 3, 1000);
These variables needs to be defined elsewhere, not within the same m-file as the function.
See the documentation to fully understand the difference between a script and a function.
1 comentario
Sean de Wolski
el 22 de Mzo. de 2012
or just write function at the top line to turn the script into a function.
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!