How to index variables in two combined function for overwriting data

Hi everyone! Kindly ask about help in function. Could you please help which varibles should be indexed. If I will use X0, Y0, Z0 as a first point and calculate Xr, Yr, Zr, Theta, Phi with help of two function. And then Xr, Yr, Zr, ThetaBar, PhiBar will be overwrited for X0,Y0,Z0 and new Xr, Yr, Zr will be calculated, and so on. Thank you in advance
X0 = 1.5;
Y0 = 1.5;
Z0 = 3.0;
Theta0 = 30;
Phi0 = 90;
K = 4;
X(ii) = [X0 zeros(1,K)];
Y(ii) = [Y0 zeros(1,K)];
Z(ii) = [Z0 zeros(1,K)];
Theta(ii) = [Theta0 zeros(1,K)];
Phi(ii) = [Phi0 zeros(1,K)];
for ii = 1:K
[XBar, YBar, ZBar, ThetaBar, PhiBar] = reflection5(X0(ii), Y0(ii), Z0(ii), Theta0(ii), Phi0(ii));
[p, Xr(ii), Yr(ii), Zr(ii), ThetaBar, PhiBar, tempPlane] = planeLocation5(XBar(ii), YBar(ii), ZBar(ii), ThetaBar(ii), PhiBar(ii));
X(ii+1) = Xr;
Y(ii+1) = Yr;
Z(ii+1) = Zr;
Theta(ii+1) = ThetaBar;
Phi(ii+1) = PhiBar;
end
Best regards, Aknur

 Akzeptierte Antwort

With some bold guessing:
X0 = 1.5;
Y0 = 1.5;
Z0 = 3.0;
Theta0 = 30;
Phi0 = 90;
K = 4;
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
[XBar, YBar, ZBar, ThetaBar, PhiBar] = reflection5(X(ii), Y(ii), Z(ii), Theta(ii), Phi(ii));
[p, Xr, Yr, Zr, ThetaBar, PhiBar, tempPlane] = planeLocation5(XBar, YBar, ZBar, ThetaBar, PhiBar);
X(ii+1) = Xr;
Y(ii+1) = Yr;
Z(ii+1) = Zr;
Theta(ii+1) = ThetaBar;
Phi(ii+1) = PhiBar;
end

3 Kommentare

Hello, @Jan ! Thank you a lot! It works! But it does not take result of Xr, Yr, Zr instead of X0, Y0, Z0 for the next iteration. It supposed to be like
X0, Y0, Z0 --> Xr, Yr, Zr and this Xr, Yr, Zr will be set as X0, Y0, Z0 -->(in function we will get Xr, Yr, Zr) and etc
it gave some kind of this XBar: 1.500000
YBar: 3.337117
ZBar: 0.181981
XBar: 1.500000
YBar: 3.337117
ZBar: 0.181981
Second iteration
XBar: 1.500000. for Theta 60 degree
YBar: 5.967162
ZBar: 1.013995
XBar: 1.500000
YBar: 3.337117
ZBar: 0.181981
@Aknur: "But it does not take result of Xr, Yr, Zr instead of X0, Y0, Z0 for the next iteration." - As far as I can see, it does:
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 First iteration:
ii = 1
% Then X(ii) = X0
% XBar is calculated and used to get Xr. Then:
X(ii+1) = Xr % This is X(2)
% Next iteration:
ii = 2
% Now X(ii) = X(2), which is Xr of the previous iteration.
This sounds like what you are asking for. If not, modify the corresponding index.
Hi @Jan yes, that is it. Thank you sooo much. It works perfect!| I was just mixed with 0 value and ii

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2021b

Gefragt:

am 5 Mär. 2023

Kommentiert:

am 17 Mär. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by