How can I create a 2D plot using a parfor loop?

14 次查看(过去 30 天)
I have some code that works as expected in a for-loop, but I would like to know if I can procude the same results instead using a parfor-loop.
I am new to parfor-loops so please inform me if I am using this featture incorrectly.
This code works as expected and produces an adequate plot:
L1 = 10;
L2 = 5;
L3 = 3;
theta1 = (0:5:90);
theta2 = (0:5:180);
theta3 = (0:5:180);
[THETA1,THETA2] = meshgrid(theta1,theta2);
figure
hold on
for i = 1:length(theta3)
THETA3 = theta3(i);
x = L1*cosd(THETA1) + L2*cosd(THETA1-THETA2) + L3*cosd(THETA1-THETA3);
y = L1*sind(THETA1) + L2*sind(THETA1-THETA2) + L3*sind(THETA1-THETA3);
plot(x,y)
end
hold off
xlabel('x')
ylabel('y')
title('Inverse Kinematic Plot of a 3-Bar Linkage')
Using the parfor-loop the code breaks down and my plot is completly empty:
L1 = 10;
L2 = 5;
L3 = 3;
theta1 = (0:5:90);
theta2 = (0:5:180);
theta3 = (0:5:180);
[THETA1,THETA2] = meshgrid(theta1,theta2);
figure
hold on
parfor i = 1:length(theta3)
THETA3 = theta3(i);
x = L1*cosd(THETA1) + L2*cosd(THETA1-THETA2) + L3*cosd(THETA1-THETA3);
y = L1*sind(THETA1) + L2*sind(THETA1-THETA2) + L3*sind(THETA1-THETA3);
plot(x,y)
end
hold off
xlabel('x')
ylabel('y')
title('Inverse Kinematic Plot of a 3-Bar Linkage')

回答(1 个)

KSSV
KSSV 2019-3-22
YOu cannot see the plot..but you can save it and late access it.
L1 = 10;
L2 = 5;
L3 = 3;
theta1 = (0:5:90);
theta2 = (0:5:180);
theta3 = (0:5:180);
[THETA1,THETA2] = meshgrid(theta1,theta2);
figure
hold on
parfor i = 1:length(theta3)
THETA3 = theta3(i);
x = L1*cosd(THETA1) + L2*cosd(THETA1-THETA2) + L3*cosd(THETA1-THETA3);
y = L1*sind(THETA1) + L2*sind(THETA1-THETA2) + L3*sind(THETA1-THETA3);
plot(x,y)
saveas(gcf,['Plot' num2str(i) '.fig']);
end
hold off
xlabel('x')
ylabel('y')
title('Inverse Kinematic Plot of a 3-Bar Linkage')

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by