How to plot multiple curves in the same graph window?

I want to plot graphs for multiple Kp values for a proportional controller. I tried using for loop to perform this. But I didn't get the output.Below, I attach the code which I have written for your reference. Please guide me on this. If you can tell me if there's any other way to do it, then that would be great.
Kindly reply asap.
Thank you.

5 Comments

Can you share the code, use alt+Enter or attach as m file? Not screenshot?
yeah, sure.
I've attached the file down below there.
Thanks.
Also, sorry, I forgot to attach another screenshot. Below, I have attached the screenshot of a similar graph which I want to get.
Thank you.
Just paste short demo code as text, use the code button to format ... much easier than having to download a file and open it externally.
For figures, SaveAs a .jpg and then attach the immage with the picture icon at the INSERT group.
I'll guess just from the words you didn't use "hold on" after the first plot to add subsequent to the same axes...
clc;
clear all;
close all;
J=20.48*10e-6;
b=0.702;
kt=14.70;
kb=0.48;
R=1.6;
L=0.02*0.001;
num=kt;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+(kb*kt))];
for kp=0:50:300 %plotting graph for P controller with Kp values in steps of 50
ki=0;
kd=0;
numc=[kd,kp,ki];
denc=[0 1];
numa=conv(num,numc);
dena=conv(den,denc);
[numac,denac]=cloop(numa,dena);
step(numac,denac)
hold on;
xlabel('time(sec)'), ylabel('velocity(rad/sec)')
title ('pid control');
grid;
end

Sign in to comment.

 Accepted Answer

Try this:
J=20.48*10e-6;
b=0.702;
kt=14.70;
kb=0.48;
R=1.6;
L=0.02*0.001;
num=kt;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+(kb*kt))];
kp=0:50:300; %plotting graph for P controller with Kp values in steps of 50
for k = 1:numel(kp)
ki=0;
kd=0;
numc=[kd,kp(k),ki];
denc=[0 1];
numa=conv(num,numc);
dena=conv(den,denc);
[numac,denac]=cloop(numa,dena);
sys = tf(numac,denac)
[y{k},t{k}] = step(sys);
% QL(k,:) = [min(y{k}), max(y{k})] % Information
end
figure
hold on
for k = 2:numel(y)
plot(t{k}, y{k})
end
hold off
xlabel('time(sec)'), ylabel('velocity(rad/sec)')
title ('pid control');
grid
It is necessary to create a system object to use the step funciton. The first step output is uniformly zero, and that causes problems with the plot. Start with the second one instead.
Also, use feedback rather than cloop (that has been deprecated).

8 Comments

Thank you!
edit: Can you please tell me why did you include second for loop? And in second for loop, nume1(y) basically means y is an array of some 'x' elements. but I am not able to determine array y.
Please help.
Thanks
As always, my pleasure!
but if I use feedback instead of cloop it's showing not enough input arguments.Why is it so??
Error using feedback (line 71)
Not enough input arguments.
Error in cs_proj1 (line 49)
[numac,denac]=feedback(numa,dena);
The feedback function is different. It requires two system objects as its arguments.
Note that I created a system object with the tf call, then used it with step. You will have to define at least two system objects in your code to use feedback.
Okay. Got it. Thanks. Can you please tell me why did you include second for loop? And in second for loop, nume1(y) basically means y is an array of some 'x' elements. but I am not able to determine array y. Please help. Thanks.
As always, my pleasure!
It is easier to plot outside the original loop, so I created the data in the first loop, then created a second loop to plot it. If you had wanted to use this syntax:
y = step(sys,t)
then all the ‘y’ vectors would have been the same length, and the second loop would not have been required.
Example —
J=20.48*10e-6;
b=0.702;
kt=14.70;
kb=0.48;
R=1.6;
L=0.02*0.001;
num=kt;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+(kb*kt))];
kp=0:50:300; %plotting graph for P controller with Kp values in steps of 50
tv = linspace(0, 5E-11, 150); % Common Time Vector
for k = 2:numel(kp)
ki=0;
kd=0;
numc=[kd,kp(k),ki];
denc=[0 1];
numa=conv(num,numc);
dena=conv(den,denc);
[numac,denac]=cloop(numa,dena);
sys = tf(numac,denac)
y(:,k) = step(sys,tv);
end
figure
plot(tv, y)
xlabel('time(sec)'), ylabel('velocity(rad/sec)')
title ('pid control');
grid
How to change the range of x and y axes of the graph which is already plotted?
I want to obtain the graph similar to the graph I have attached below there. Scale of the graph seems to be different on execution of this code. How do we change it? I tried to use linspace() function for this before giving range for kp values. But it didn't work.
Please help.
Thanks.
J=0.01;
b=0.1;
p=0.01;
R=1;
L=0.5;
num=p;
den=[ (J*L) ((J*R)+(L*b)) ((b*R) + p^2)] ;
%%%%% Different values of kp , ki and kd
%%%%%%%%%
kp=0:30:300;
for k = 1:numel(kp)
ki=0;
kd=0;
numc=[kd,kp(k),ki];
denc=[0 1];
numa=conv(num,numc);
dena=conv(den,denc);
[numac,denac]=cloop(numa,dena);
sys = tf(numac,denac)
[y{k},t{k}] = step(sys);
% QL(k,:) = [min(y{k}), max(y{k})] % Information
end
figure
hold on
for k = 2:numel(y)
plot(t{k}, y{k})
end
hold off
xlabel('time(sec)'), ylabel('velocity(rad/sec)')
title ('pid control');
To change the axis limits use the xlim, ylim or axis functions. To set limits on axes that are already plotted, see the documentation on Axes Properties.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!