Hello! How to make for loop for cycle calculation using previous coordinates to calculate next one, etc
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aknur
el 13 de Oct. de 2022
Hello! I am trying to apply for loop. I have x0, y0, z0 coordinates and wrote a function to calculate next x1, y1, z1
I need to make for loop and the previous calculated result of x1, y1, z1 will be considered for the next cycle ( instead of x0, y0, z0) and so on (x2, y2, z2 will , number of cycle will be 5. I tried to do it. Could you please help me with my code. Thank you in advance
function [x1, y1, z1, F] = reflection(x0,y0,z0, Theta, Phi)
disp(x1);
disp(y1);
disp(z1);
disp(F);
end
And here is my for loop attempt
x0 = 1.5;
y0 = 1.5;
z0 = 3.0;
for ii=1:10
r1 = 1i;
r2 = 1i + 1;
reflection(x(ii), y(ii), z(ii));
end
1 comentario
Dyuman Joshi
el 13 de Oct. de 2022
You can call the function in a loop -
x0 = [1.5 zeros(1,10)];
y0 = [1.5 zeros(1,10)];
z0 = [3.0 zeros(1,10)];
for ii=2:11
r1 = 1i;
r2 = 1i + 1;
[x0(ii),y0(ii),z0(ii),~]=reflection(x0(ii-1), y0(ii-1), z0(ii-1));
end
However, the function requires 5 inputs, of which 2 are missing from the function call above (Theta, Phi)
Also, what is the significance of r1 and r2 in the for loop?
Respuesta aceptada
David Hill
el 13 de Oct. de 2022
H,d,A,B,E,F are all constant, therefore your could do all iterations at once.
[x,y,z]=reflection(1.5,1.5,3,60,45,5)
function [x, y, z] = reflection(x,y,z, Theta, Phi,n)%n=number of iterations
n=1:n;
H = 3;
d = H /cos(Theta);
A = (sind(Theta)* H)/cosd(Theta);
B = (sind(Phi)* sind(Theta)* H)/cosd(Theta);
E = cosd(Phi) * sind(Theta)* H/cosd(Theta);
F = (sind(Theta) * H/cosd(Theta))/sind(Phi);
x = x + n*B;
y = y + n*E;
z = z - n*F;
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!