How I can add For loop inside of if statement ?

Hello everyone! Kindly ask how I can make for loop inside of if statement. If it is allowed to do in that order.
for example I want to set an angle that was calculated according to the if statement and the calculated angle will be set for the next iteration of for loop. My main code is running, and I got an answer that the angle was calculated two times and two values changed for one iteration. But it should be like the result of first calculation set as Theta for the next iteration, and in the second iteration it calculates according to the previous one.
Thank you in advance
This is my code
k = 5;
if Xr == 0
planes(:,:,1)
for i = 1:K
ThetaB = 90 - Theta0;
PhiB = Phi0;
fprintf('ThetaB: %f\n',ThetaB);
end
elseif Xr == 3
planes(:,:,3)
for i = 1:k
ThetaB = 90-Theta0;
PhiB = Phi0;
fprintf('ThetaB: %f\n',ThetaB);
end
elseif Yr == 0
planes(:,:,2)
ThetaB = 90-Theta0;
PhiB = Phi0;
fprintf('ThetaB: %f\n',ThetaB);
elseif Yr == 3
planes(:,:,4)
for i = 1:k
ThetaB = 90 - Theta0;
fprintf('ThetaB: %f\n',ThetaB);
end
elseif Zr == 0
planes(:,:,5)
for i = 1:k
ThetaB = Theta0;
PhiB = Phi0;
fprintf('ThetaB: %f\n',ThetaB);
end
elseif Zr == 3
planes(:,:,6)
for i = 1:k
ThetaB = Theta0;
PhiB = Phi0;
fprintf('ThetaB: %f\n',ThetaB);
end
end
And this is how I am running my main
for ii = 1:K
ii
X(ii)
Y(ii)
Z(ii)
Theta(ii)

4 Comments

What is the purpose of assigning the same value to a variable inside a loop:
for i = 1:k
...
PhiB = Phi0;
...
end
Displaying the matrixes have no effect, so what is the reason to do this:
planes(:,:,1)
What should your main script do? I do not understand its purpose.
Hello, @Jan thank your answer. My reason to do it is trying to define correc angle of reflection. Because my function does not show me right angle. And it seems like my angle ThetBar calculated two times per iteration which lead to the mistake. Now I think I have to set ThetaBar value accroding to the number of plane and set it in main. How do you think ? Could you please help me :(
How I can add here statement to main for ThetaBar and plane as output. If plane number is odd, then ThetaBar = 90 -Theta0, and in case number of plane is even then ThetaBar = Theta0
Thank you in advance for anyy suggestion and help. I trying to do it, wish I will succes on it, because It seems very complicated for me.
clc
clear all
close all
X0 = 1.5;
Y0 = 1.5;
Z0 = 3.0;
Theta0 = 60;
Phi0 = 90;
K = 5;
X = [X0 zeros(1,K)];
Y = [Y0 zeros(1,K)];
Z = [Z0 zeros(1,K)];
Theta = [Theta0 zeros(1,K)];
Phi = [Phi0 zeros(1,K)];
for ii = 1:K
ii
Theta(ii)
%ThetaBar(ii)
[XBar, YBar, ZBar, ThetaBar, PhiBar] = reflection5(X(ii), Y(ii), Z(ii), Theta(ii), Phi(ii));
[p, Xr, Yr, Zr, ThetaBar, tempPlane] = planeLocation5(XBar, YBar, ZBar,X(ii), Y(ii), Z(ii),Theta(ii));
X(ii+1) = Xr;
Y(ii+1) = Yr;
Z(ii+1) = Zr;
Theta(ii+1) = ThetaBar;
Phi(ii+1) = PhiBar;
end
What does "plane number is odd" mean? Which variable represents the "plane number"?
What are the functions reflection5 and planeLocation5?
"And it seems like my angle ThetBar calculated two times per iteration which lead to the mistake" - Why fo you think so?
Hello, @Jan thank you for your valuable question. I fix my problem nad happy for it. You are right I overwrite my variable, that is why it does not work. I add mod to planeLocation function. Thanks a lot again
plane = A(:,1)
if mod(plane,2) == 0 %odd
ThetaBar = 90 - Theta0;
fprintf('ThetaBar odd: %f\n',ThetaBar);
else
ThetaBar = Theta0; %even
fprintf('ThetaBar even: %f\n',ThetaBar);
end

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 13 Mar 2023

Commented:

on 17 Mar 2023

Community Treasure Hunt

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

Start Hunting!