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)
Show older comments
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.
0 Comments
Accepted Answer
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
M2
8 Comments
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
Beta
Mn1s1
Theta
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
More Answers (0)
See Also
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!