필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
Dyuman Joshi
Dyuman Joshi 2022년 10월 13일
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?

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

채택된 답변

David Hill
David Hill 2022년 10월 13일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by