Attempting to plot a 3 x 3 grid of plots

53 views (last 30 days)
William Kistler
William Kistler on 19 Feb 2021
Commented: Star Strider on 20 Feb 2021
I am trying to plot the varying responses for a PID controller.
I have 3 values for K_p
I have 3 values for K_d
The function is already built in and pulls the K-values.
I need to create 9 subplots in a grid, but I'm terrible with for loops.
Should I create a vector for each?
Like this
Kp = [2.4 180 250];
Kd = [0 6.5 25];
What should the for loop be like?
for i = 1:length(Kp)
for j = 1:length(Kd)
plot([i:j]);
plot(time, response, 'k', 'LineWidth', 2)
end
end
All advice is appreciated, and forgive my lack of understanding. Thank You!

Answers (1)

Star Strider
Star Strider on 19 Feb 2021
Try something like this:
time = linspace(0,2*pi);
response = @(Kp,Kd) sin(time*Kp) .* cos(time*Kd);
Kp = [2.4 180 250];
Kd = [0 6.5 25];
k = 0;
for i = 1:length(Kp)
for j = 1:length(Kd)
k = k+1;
subplot(3,3,k)
plot(time, response(Kp(i),Kd(j)), 'k', 'LineWidth', 2)
title(sprintf('Kp = %3.1f, Kd = %2.1f',Kp(i),Kd(j)))
end
end
Adapt that to plot what you want to plot. Make appropriate changes to get the result you want.
  2 Comments
William Kistler
William Kistler on 20 Feb 2021
Your code runs elegantly on its own. Thank you for your help.
Unfortunately, due to the nature of the code we were given as part of the assignment, it isn't compatible.
I have attached the code to this message on the off chance that someone wants to give it a shot
Star Strider
Star Strider on 20 Feb 2021
I have no idea what you’re referring to. I don’t see anything similar in the .m file.
I’ll delete my Answer in a few hours.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!