how to write a loop
Afficher commentaires plus anciens
I would like to obtain t values according to i parameters. If i=1, it should return just t1. If i=3, it should return t1, t2 and t3. If i=5, it should return t1,t2,t3,t4 and t5 and so on. How can write this part into a loop??
clear ; clc;
n = 8; r = 2; h = 5; p=3;
Ang_seg = 360/n;
for i=1:n
Xni(i) = r*cos((i-1)*Ang_seg*pi/180);
Yni(i) = r*sin((i-1)*Ang_seg*pi/180);
Zni(i) = 0;
Cbottom = [Xni' Yni' Zni'];
Xnk(i) = Xni(i);
Ynk(i) = Yni(i);
Znk(i) = h;
Ctop = [Xnk' Ynk' Znk'];
end
%how to write this part into a loop:
for i=1
t1 = ( Xni(i+1) - Xni(i) ) / ( ( Xnk(i+1) - Xni(i) ) - ( Xnk(i) - Xni(i+1) ) );
end
for i=3
t1 = ( Xni(i-1) - Xni(i-2) ) / ( ( Xnk(i) - Xni(i-2) ) - ( Xnk(i+5) - Xni(i-1) ) );
t2 = ( Xni(i) - Xni(i-2) ) / ( ( Xnk(i) - Xni(i-2) ) - ( Xnk(i-2) - Xni(i) ) );
t3 = ( Xni(i+1) - Xni(i-2) ) / ( ( Xnk(i) - Xni(i-2) ) - ( Xnk(i-1) - Xni(i+1) ) );
end
for i=5
t1 = ( Xni(i-3) - Xni(i-4) ) / ( ( Xnk(i-1) - Xni(i-4) ) - ( Xnk(i+2) - Xni(i-4) ) );
t2 = ( Xni(i-2) - Xni(i-4) ) / ( ( Xnk(i-1) - Xni(i-4) ) - ( Xnk(i+3) - Xni(i-2) ) );
t3 = ( Xni(i-1) - Xni(i-4) ) / ( ( Xnk(i-1) - Xni(i-4) ) - ( Xnk(i-4) - Xni(i-1) ) );
t4 = ( Xni(i) - Xni(i-4) ) / ( ( Xnk(i-1) - Xni(i-4) ) - ( Xnk(i-3) - Xni(i) ) );
t5 = ( Xni(i+1) - Xni(i-4) ) / ( ( Xnk(i-1) - Xni(i-4) ) - ( Xnk(i-2) - Xni(i+1) ) );
end
Réponses (1)
Instead of using:
for i=1
use
if i==1
(And it is generally not recommended to mask MATLAB functions in your code, such as the function i.)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!