Hello! How to make for loop for cycle calculation using previous coordinates to calculate next one, etc

4 Ansichten (letzte 30 Tage)
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 Kommentar
Dyuman Joshi
Dyuman Joshi am 13 Okt. 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Hill
David Hill am 13 Okt. 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)
x = 1×5
5.1742 8.8485 12.5227 16.1969 19.8712
y = 1×5
5.1742 8.8485 12.5227 16.1969 19.8712
z = 1×5
-4.3485 -11.6969 -19.0454 -26.3939 -33.7423
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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by