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?
古いコメントを表示
% 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 件のコメント
Hi @Pragna
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
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
2024 年 5 月 7 日
採用された回答
その他の回答 (2 件)
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
2024 年 5 月 7 日
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 件のコメント
Pragna
2024 年 5 月 7 日
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
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
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!














