Subplot is failing to produce the figure that I want, where have I messed up?

2 views (last 30 days)
When I run the script I can only get the figure to produce the plot column 1 row 1, but it needs to produce a lot more. I should be getting back the top figure for the first part, and the bottom image for the second part on the image below:
Here is the code I am working with:
% Find the position of the teacup handle on the ride in different positions
% Clear window, variables, figure
clear;
clc;
clf;
% Variables
R = 6; % Radius of big platter (meters)
r = 1; % Radius of teacup (meters)
P_theta = [0 60 120 180 240 300]; % Platter in degrees
H_theta = [0 90 180 270]; % Handle in degrees
% Plot for platter and handle
k = 1;
for h = 1:length(H_theta);
for p = 1:length(P_theta);
figure(1);
xh = r.*cosd(H_theta(h)); % x-coord. for handle, meters
yh = r.*sind(H_theta(h)); % y-coord. for handle, meters
% Plot circle of the platter
t = linspace(0,2*pi); % radians
xp = R.*cos(t); % x-coord. for platter, meters
yp = R.*sin(t); % y-coord. for platter, meters
subplot(length(H_theta),length(P_theta),k);
plot(xp,yp,'-k');
hold on;
xc = r.*cos(t); % x-coord. for cup, meters
yc = r.*sin(t); % y-coord. for cup, meters
xc = xc+R.*cosd(P_theta); % x-coord. for moved cup, meters
yc = yc+R.*sind(P_theta); % y-coord. for moved cup, meters
xh = xh+R.*cosd(P_theta); % x-coord. for moved handle, meters
yh = yh+R.*sind(P_theta); % y-coord. for moved handle, meters
% Plot teacup and handle
plot(xc,yc,'-r');
plot(xh,yh,'kx');
xlable('meters');
ylable('meters');
title('Teacup on Platter');
if h == 1
figure(2);
subplot(length(H_theta), length(P_theta), p );
plot(xp,yp,'-k');
hold on;
% Plot teacup and handle, part 2
plot(xc,yc,'-r');
plot(xh,yh,'kx');
xlable('meters');
ylable('meters');
title('Teacup on Platter');
end
end
k = k+1;
end
This is the image I am getting back:

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jul 2015
Right after your first plot, you do
t = linspace(0,2*pi);
xc = r.*cos(t)
xc = xc+R.*cosd(P_theta)
t is a vector of length 100, so xc becomes a vector of length 100. R is a scalar. P_theta is a vector of 6 elements. cosd() of a vector of 6 elements is a vector of 6 elements. A scalar times a vector of 6 elements is a vector of 6 elements. A vector of 100 elements "+" a vector of 6 elements is an error.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!