![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/246813/untitled.jpeg)
hi there i have this code it doesnot work will and I need help with finding the error
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kareem Alnahar
el 5 de Nov. de 2019
Comentada: Steven Lord
el 5 de Nov. de 2019
the code is
step=2.5; t11=0; t12=180; t21=0; t22=90;
a1=1; a2=0.5;
hold on
for i=t11:step:t12;
for k=t21:step:t22;
x=a1*cos(i*pi/180)+a2*cos((pi/180)*(i+k));
y=a1*sin(i*pi/180)+a2*sin((pi/180)*(i+k));
plot(x,y)
end
end
but it doesnot plot any thing
0 comentarios
Respuesta aceptada
Caleb Wilkins
el 5 de Nov. de 2019
Hello,
I would solve your problem by saving the data into a matrix that can be plotted later (after the loop). I have modified your code to produce this.
![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/246813/untitled.jpeg)
Here is the modified code with comments. Hope this helps.
step=2.5; t11=0; t12=360; t21=0; t22=360;
a1=1; a2=0.5;
x = zeros(length(t11:step:t12),length(t21:step:t22)); %pre allocate the size. This helps if you want to easily change values also. It will not have remenant data left from previous "runs"
y = zeros(length(t11:step:t12),length(t21:step:t22));
index=1; %initilaize the index variable. Must be greater than 0.
for i=t11:step:t12
for k=t21:step:t22
x(index)=a1*cos(i*pi/180)+a2*cos((pi/180)*(i+k));
y(index)=a1*sin(i*pi/180)+a2*sin((pi/180)*(i+k));
index = index+1; %add one to the index before next cycle.
end
end
plot(x,y,'k') %plot the arrays of data with a black line.
2 comentarios
Steven Lord
el 5 de Nov. de 2019
Another potential approach, if you need the plot to appear and be updated at each iteration, would be to use an animatedline to which you addpoints inside the loop. Use the example on the animatedline documentation page as a model.
Más respuestas (0)
Ver también
Categorías
Más información sobre Animation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!