Why is my plot not working within my for loop?

I am trying to plot multiple helices. With the code I am using (below) the helix is plotting how I want it to, however, only one helix is not plotting. Does anyone know why this may be the case?
clc;
clear variables;
close all;
n = input('number of revolutions ');
r = input('radius of stent ');
a = input('angle with respect to the upwards horizontal in degrees ');
w = input('number of wires ');
R = input('radius of wire ');
h=(2*n*pi*r)/tand(90-a); %height of stent determined by other inputs
e = (2*pi)/(w/2); %theta (spacing between where each wire starts)
if mod(w,2) ==0 %number of wires is even
else
fprintf('ERROR:number of wires must be even'); %stops code if wire number is odd
return
end
if mod(n,1) ==0 %number of revolutions is whole number
else
fprintf('ERROR:number of revolutions must be whole number');
return
end
for i=1:w/2
for t = (i-1)*e:2*pi/w:(n*2*pi)+e*(i-1); %t value for CCW helices
if mod(t,4*pi/w)==0
x{i} = (r-R)*sin(t);
y{i} = (r-R)*cos(t);
z{i} = (h/(n*2*pi))*t(1);
plot3(x{i},y{i},z{i},'.','MarkerSize',25,'MarkerFaceColor','red','MarkerEdgeColor','red')
hold on
else
x{i} = (r+R)*sin(t);
y{i} = (r+R)*cos(t);
z{i} = (h/(n*2*pi))*t(11);
plot3(x{i},y{i},z{i},'.','MarkerSize',25,'MarkerFaceColor','black','MarkerEdgeColor','black')
hold on
end
hold on
end
hold on
end

댓글 수: 5

Adam Danz
Adam Danz 2018년 7월 17일
편집: Adam Danz 2018년 7월 17일
Could you provide a set of input values so your code can be run?
Off the bat I have a suggestion. You are calling 'hold on' too many times. Prior to your loop you can create the figure and axis and hold it once. This isn't the source of the problem you described, though.
figure;
axh = axis();
hold(axh, 'on')
then you can get rid of all the other 'hold on' lines.
Thank you for your initial help. A set of input values could be 2,20,34,12,1 (in the order it asks you for).
A guess would be
if mod(t,4*pi/w)==0
is never actually satisfied for most cases owing to FP rounding...
Do you have any suggestions on how to fix this?
Using those inputs, your code breaks in the 'else' section of your conditional at line
z{i} = (h/(n*2*pi))*t(11);
I'm guessing that t(11) is a mistake.

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam Danz
Adam Danz 2018년 7월 17일
편집: Adam Danz 2018년 7월 17일

0 개 추천

Change
z{i} = (h/(n*2*pi))*t(11);
to
z{i} = (h/(n*2*pi))*t(1); %or whatever the index should be
and it works with the inputs you specified in the comment section above.
For inputs [2,20,34,12,1]

댓글 수: 4

Yes it works for one single wire, however it should be plotting multiple wires.
Adam Danz
Adam Danz 2018년 7월 17일
편집: Adam Danz 2018년 7월 17일
It is plotting multiple data sets (wires). It's just plotting directly over top of previous data with an occasional unique data point appearing.
I determined that in debug mode by changing the colors after the first complete iteration of the i-loop.
Another way to test that is to create a new figure at every i-loop iteration.
for i=1:w/2
figure
axis
hold on
for t = (i-1)*e:2*pi/w:(n*2*pi)+e*(i-1); %t value for CCW helices
if mod(t,4*pi/w)==0
x{i} = (r-R)*sin(t);
y{i} = (r-R)*cos(t);
z{i} = (h/(n*2*pi))*t(1);
plot3(x{i},y{i},z{i},'.','MarkerSize',25,'MarkerFaceColor','red','MarkerEdgeColor','red')
else
x{i} = (r+R)*sin(t);
y{i} = (r+R)*cos(t);
z{i} = (h/(n*2*pi))*t(1);
plot3(x{i},y{i},z{i},'.','MarkerSize',25,'MarkerFaceColor','black','MarkerEdgeColor','black')
end
end
end
Here are all 6 'wires'; they appear 2D because the axes aren't rotated.
But when the i value changes, the t values change, which means the x and y values would change, so how are they overlapping if the points should be different?
Adam Danz
Adam Danz 2018년 7월 17일
편집: Adam Danz 2018년 7월 17일
But they mostly do not change (some do) so something's wrong with you code.
To start, you are currently only storing x,y,z values over the i-loop so you're overwriting a lot of the data making it impossible to look at all of the x,y,z values at once. I changed the code so you store ALL of the data for both i and t loops. If you go this route, you should allocate the x y z data prior to the loops.
figure
axis
hold on
for i=1:w/2
tAll = (i-1)*e:2*pi/w:(n*2*pi)+e*(i-1);
for t = 1:length(tAll); %t value for CCW helices
if mod(tAll(t),4*pi/w)==0
x(i,t) = (r-R)*sin(tAll(t));
y(i,t) = (r-R)*cos(tAll(t));
z(i,t) = (h/(n*2*pi))*tAll(t);
plot3(x(i,t),y(i,t),z(i,t),'.','MarkerSize',25,'MarkerFaceColor','red','MarkerEdgeColor','red')
else
x(i,t) = (r+R)*sin(tAll(t));
y(i,t) = (r+R)*cos(tAll(t));
z(i,t) = (h/(n*2*pi))*tAll(t);
plot3(x(i,t),y(i,t),z(i,t),'.','MarkerSize',25,'MarkerFaceColor','black','MarkerEdgeColor','black')
end
end
end
Now we can look at ALL of the x,y,z values and you can see that there are lots of duplicates. Here I show just the x values. The rows are iterations across the i-loop and the columns are across the t-loop. There are tiny differences between these numbers but they are close enough to be plotted on top of each other. It looks like there are about 6 near-repetitions for each (x,y,z) value and there also happens to be 6 'wires'.
If the values are not what you expect, you'll need to go through your code and figure out what went wrong.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Julia Morandi
Julia Morandi 2018년 7월 17일

0 개 추천

Thank you for your help!

카테고리

도움말 센터File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품

질문:

2018년 7월 17일

댓글:

2018년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by