Wish to use output of the first iteration in a loop as an input in the second iteration of the loop

1 view (last 30 days)
tic
M1 = 5;
Altitude = 28000;
gamma = 1.4;
Theta = 8;
[Beta] = betacalling(M1);
%Assume Beta is = [17 21 32];
for i = 1:M1
for j = 1:Beta
Mn1s1 = M1.*sin(Beta.*(pi./180));
M2 = (1./sind(Beta-Theta)).*sqrt((1+((gamma-1)./2).*(M1.^2).*(sind...
(Beta).^2))./((gamma*(M1.^2).*(sind(Beta).^2)-(gamma-1)./2)));
end
end
toc
First time it runs the calculation I want it to use 1st Beta value and M1 as 5. Second time it runs the calculation, I want it to use Beta value of 21 and M2 value instead of M1. And similarly for the third time as.
Can you please help me with this kind of a for loop.
Thank you.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 6 Aug 2023
%Values
M1 = 5;
Altitude = 28000;
gamma = 1.4;
Theta = 8;
%[Beta] = betacalling(M1);
Beta = [17 21 32];
%Preallocate M2 to store the values of each iteration
M2 = zeros(size(Beta));
for j = 1:numel(Beta)
Mn1s1 = M1.*sin(Beta(j).*(pi./180));
%M2 value corresponding to value in Beta
M2(j) = (1./sind(Beta(j)-Theta)).*sqrt((1+((gamma-1)./2).*(M1.^2).*(sind...
(Beta(j)).^2))./((gamma*(M1.^2).*(sind(Beta(j)).^2)-(gamma-1)./2)));
%Assign M2 value i.e. M2(j) obtained to M1, to be used for next iteration
M1 = M2(j);
end
Beta
Beta = 1×3
17 21 32
M2
M2 = 1×3
4.5708 2.9221 1.6832
  8 Comments
Dean
Dean on 6 Aug 2023
"If easier then having theta = linspace(8,18,1) %to have more than 3 inputs for theta if required"
sorry. this is just to consider having more than 3 more values of theta. More than 8, 10, 18.
Dyuman Joshi
Dyuman Joshi on 7 Aug 2023
Running for more than 4 iterations give complex values for variables.
%Number of iterations to do
N = 4;
%Values
Altitude = 28000;
gamma = 1.4;
Theta = [8 10 zeros(1,N-2)];
M1 = [5 zeros(1,N)];
Mn1s1 = zeros(1,N);
Beta = zeros(1,N);
for k = 1:N
if k>2
Theta(k) = Theta(k-1) + Theta(k-2);
end
%Beta value from the function
Beta(k) = betacalling(M1(k),Theta(k));
Mn1s1(k) = M1(k).*sind(Beta(k));
%Next value of M1
M1(k+1) = (1./sind(Beta(k)-Theta(k))).*sqrt((1+((gamma-1)./2).*(M1(k).^2).*(sind...
(Beta(k)).^2))./((gamma*(M1(k).^2).*(sind(Beta(k)).^2)-(gamma-1)./2)));
end
M1
M1 = 1×5
5.0000 4.1972 3.4319 2.3803 1.0481
Beta
Beta = 1×4
17.5700 21.5460 32.7167 60.6327
Mn1s1
Mn1s1 = 1×4
1.5094 1.5414 1.8549 2.0744
Theta
Theta = 1×4
8 10 18 28
function Beta = betacalling(M1,theta)
n=0;
gamma = 1.4;
%All operations in degrees
mu = asind(1/M1);
c = tand(mu)^2;
a = ((gamma-1)/2+(gamma+1)*c/2)*tand(theta);
b = ((gamma+1)/2+(gamma+3)*c/2)*tand(theta);
d = sqrt(4.*(1-3.*a.*b).^3./((27.*a.^2.*c+9.*a.*b-2).^2)-1);
Beta = atand((b+9.*a.*c)./(2.*(1-3.*a.*b))-(d.*(27.*a.^2.*c+9.*a.*b-2))./...
(6.*a.*(1-3.*a.*b)).*tan(n.*pi./3+1./3.*atan(1./d)));
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!