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?
3 次查看(过去 30 天)
显示 更早的评论
% 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 个评论
Sam Chak
2024-5-7
编辑:Sam Chak
2024-5-7
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'.
采纳的回答
Infinite_king
2024-5-7
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
Refer the following documentation to know more about 'for' loops - https://www.mathworks.com/help/matlab/ref/for.html
更多回答(2 个)
Paul
2024-6-10
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 个评论
Sam Chak
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Digital Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!