Clear Filters
Clear Filters

hey so i need to repeat the code everytime to see and analyze the time responses for 20 different values of k. How can i do it at once and have the output for all the values?

12 views (last 30 days)
% Time response problme #3
% time response and pole zero plot
k=input('Enter the value of k');
ng1=[k];dg1=[1];
ng=[20]; dg=[1 1 5];
[na, da]=series(ng,dg,ng1,dg1)
[num, den]=cloop(na,da)
sys=tf(num,den)
[z,p,k]=tf2zp(num,den)
subplot(1,2,1);step(sys,20e-1)
S = stepinfo(sys)
subplot(1,2,2); zplane(num,den)
  2 Comments
Sam Chak
Sam Chak on 7 May 2024
Edited: Sam Chak on 7 May 2024
The 'cloop()' function has been outdated for over two decades. It seems like you may have copied the code from very old books, or your professor provided you with legacy code for completing the homework questions. Additionally, 'zplane' is used to create a zero-pole plot for discrete-time systems, but your systems are continuous-time.
help cloop
CLOOP is obsolete, use FEEDBACK instead.
Have you considered replacing the 'cloop()' function with the 'feedback()' function in the code? This change could help simplify the problem by allowing you to create a single plot for a scalar value of the gain k.
By doing so, you can evaluate if the plot is correctly displayed as desired. If this approach proves successful, you can then tackle the problem using the for-loop approach and the supplied values of 'k'.
Pragna
Pragna on 7 May 2024
hhey thanks ....they did teach us bout both cloop and feedback in class but what did you mean by zplane being used only for discrete time systems ??? is there any other functions wrt to continuous time systems???

Sign in to comment.

Accepted Answer

Infinite_king
Infinite_king on 7 May 2024
Hi Pragna,
You can use 'for' loop to execute the same code on different values.
% Store all input values in an array
input_values = [ 1 2 3 ];
% execute the code in a for loop
for single_input_value = input_values % loop will execute
k = single_input_value;
% place the remaining code inside the for loop
ng1=[k];dg1=[1];
ng=[20]; dg=[1 1 5];
[na, da]=series(ng,dg,ng1,dg1)
[num, den]=cloop(na,da)
sys=tf(num,den)
[z,p,k]=tf2zp(num,den)
% create new figure for each iteration
figure();
subplot(1,2,1);step(sys,20e-1)
S = stepinfo(sys)
subplot(1,2,2); zplane(num,den)
end
na = 1x3
0 0 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
da = 1x3
1 1 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
num = 1x3
0 0 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
den = 1x3
1 1 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sys = 20 ------------ s^2 + s + 25 Continuous-time transfer function.
z = 0x1 empty double column vector
p =
-0.5000 + 4.9749i -0.5000 - 4.9749i
k = 20
S = struct with fields:
RiseTime: 0.2254 TransientTime: 7.6746 SettlingTime: 7.6746 SettlingMin: 0.3748 SettlingMax: 1.3833 Overshoot: 72.9156 Undershoot: 0 Peak: 1.3833 PeakTime: 0.6283
na = 1x3
0 0 40
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
da = 1x3
1 1 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
num = 1x3
0 0 40
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
den = 1x3
1 1 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sys = 40 ------------ s^2 + s + 45 Continuous-time transfer function.
z = 0x1 empty double column vector
p =
-0.5000 + 6.6895i -0.5000 - 6.6895i
k = 40
S = struct with fields:
RiseTime: 0.1646 TransientTime: 7.5945 SettlingTime: 7.5945 SettlingMin: 0.3332 SettlingMax: 1.5917 Overshoot: 79.0688 Undershoot: 0 Peak: 1.5917 PeakTime: 0.4683
na = 1x3
0 0 60
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
da = 1x3
1 1 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
num = 1x3
0 0 60
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
den = 1x3
1 1 65
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sys = 60 ------------ s^2 + s + 65 Continuous-time transfer function.
z = 0x1 empty double column vector
p =
-0.5000 + 8.0467i -0.5000 - 8.0467i
k = 60
S = struct with fields:
RiseTime: 0.1356 TransientTime: 7.7949 SettlingTime: 7.7949 SettlingMin: 0.2984 SettlingMax: 1.6824 Overshoot: 82.2648 Undershoot: 0 Peak: 1.6824 PeakTime: 0.3897
Refer the following documentation to know more about 'for' loops - https://www.mathworks.com/help/matlab/ref/for.html

More Answers (2)

Paul
Paul on 10 Jun 2024 at 0:56
Or use a model array
plant = tf(20,[1 1 5]);
k = realp('k',1);
sys = feedback(k*plant,1);
sysarr = sampleBlock(sys,'k',linspace(1,20,5));
figure
stepplot(sysarr)
If rather put each plot on a separate axis, then
N = nmodels(sysarr);
figure
h = cellfun(@(sys) stepplot(axes(figure),sys),squeeze(mat2cell(sysarr,1,1,ones(1,N),1)));

Anton Kogios
Anton Kogios on 7 May 2024
There are many ways to go about this and your question is not very specific in what you want. What values do you want to keep?
One way is to use a for loop:
k=1:20; % enter your 20 values of k
for i = 1:length(k)
ng1=[k(i)];dg1=[1];
ng=[20]; dg=[1 1 5];
[na, da]=series(ng,dg,ng1,dg1)
[num, den]=cloop(na,da)
sys=tf(num,den)
[z,p,k]=tf2zp(num,den)
figure(i)
subplot(1,2,1);step(sys,20e-1)
S = stepinfo(sys)
subplot(1,2,2); zplane(num,den)
end
Note that k will get overwritten by the output of tf2zp, so name it something else if you want to keep it.
  2 Comments
Sam Chak
Sam Chak on 7 May 2024
Hi @Pragna, actually, if you read @Anton Kogios' earlier explanations, he clarified that using the same 'k' variable in the output argument of the 'tf2zp' function will overwrite it. Changing that, as he suggested, should fix the issue.
Remember, it's important not to blindly copy and paste the code. Take the time to understand what each line does if you want to learn. Also, please keep in mind that just because @Infinite_king's code works, it doesn't necessarily mean it follows good programming practices, especially when dealing with for-loops.
% Store all input values in an array
input_values = [1 2 3];
% execute the code in a for loop
for single_input_value = input_values % loop will execute
...
end
Modification on @Anton Kogios's code:
k = 1:4.75:20;
for i = 1:length(k)
G1 = tf(k(i));
G2 = tf(20, [1 1 5]);
Gcl = feedback(G1*G2, 1); % closed-loop TF
[z, p, K] = zpkdata(Gcl, 'v'); % <-- do not use the same 'k'
figure(i)
subplot(1,2,1);
step(Gcl), grid on
S = stepinfo(Gcl);
subplot(1,2,2);
rlocus(Gcl), grid on
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!