# Matlab cannot recognize variable

2 views (last 30 days)
Jaevon Stewart on 2 Apr 2024
Edited: Jaevon Stewart on 2 Apr 2024
I have this code here.
%May change entire thing to function
clc;
clear;
%Variables
sigma = .1;
lock = 8;
twist = -8;
a = 5.7*(180/pi);
mu = .25;
alpha_tpp = 2.25;
siga = sigma*a; % This variable is seen a lot.
%% solve for inflow
% Change
CT = .0012;
lambdaH = sqrt(CT/2);
muz = mu*tand(alpha_tpp);
aproxerr = 10^5;
esterr = 100;
n = 0;
f = .5;
while aproxerr < esterr
n = n+1;
if n == 1
lambdaold = lambdaH;
else
lambdaold = lambda;
end
%iterating on
lambdain = lambdaH^2/(sqrt(lambdaold^2+mu^2));
lambda = lambdaold - (lambdaold - muz - lambdain)/(1+((lambdain*lambdaold)/(lambdaold^2+mu^2)))*f;
esterr = abs(lambda - lambdaold)/lambda;
end
%% Question 2
delta = 1 - mu^2 + (9/4*mu^4);
%theta_collective =
%(1/delta)*((1+((3/2)*mu^2)))*((6*CT)/(sigma*a)+(.375*mu^2*twist) I
%do not know if this is necessary
Betanaught = ((lock/8)/delta)*(1-((19/18)*mu^2)+(1.5*mu^4))*((6*CT)/siga)+(.005+((29/120)*mu^2)-(.2*mu^4)+(.375*mu^6))*twist + lambda*((1/6)-((7/12)*mu^2)+(.25*mu^4));
Unrecognized function or variable 'lambda'.
Why is Lambda unrecognized here? I ran another code that had pretty much the same iteration up until the end of the while loop, and lambda was defined in it. So what is wrong here? And btw, I appreciate all the help you guys offer me.
Other code for reference
format long
% lambda is x
Rmain=27; %ft
Cmain=1.7; %ft
Nbmain=4;
Tipspeed_main=725; %ft/s
W=16000; %lb
alt=5000;
alpha_tpp=3;
%knots
%%
Amain=pi*Rmain^2;
sigma=(Nbmain*Cmain)/(pi*Rmain);
[~, ~, ~, RHO] = atmosisa(alt*0.3048);
rho=RHO*0.0685218/35.3147;
CT = W/(rho*Amain*Tipspeed_main^2);
xh = sqrt(CT/2);
V = 0*1.68781:10*1.68781:200*1.68791;
Mu = V./(Tipspeed_main);
for k1=1:length(Mu)
mu=Mu(k1);
muz = mu*tand(alpha_tpp);
n = 0;
aperr = 10^-5;
eserr = 100;
while eserr > aperr
n = n + 1;
if n == 1
xold = xh;
else
xold = x ;
end
%iterating on
xin = xh.^2./sqrt(xold.^2+mu.^2);
x = xold - (xold - muz - xin)./(1+((xin.*xold)./(xold.^2+mu.^2))).*.5;
eserr = abs((x - xold)./x);
end
X(k1)=x;
end
figure
plot(Mu,X,'-x')
grid on

Dyuman Joshi on 2 Apr 2024
Edited: Dyuman Joshi on 2 Apr 2024
"Why is Lambda unrecognized here?"
Because the while loop is not initiated, as the condition is not satisfied.
As the while loop does not run, lambda is not defined.
And when you use lambda (undefined parameter) to define Betanaught, it gives you the aforementioned error.
aproxerr = 10^5;
esterr = 100;
while aproxerr < esterr
...
end
Maybe aproxerr is 10^-5 (or something else). Correct the value and your code will run (I tested with couple of smaller values 10^0, 10^-1, etc).
##### 3 CommentsShow 1 older commentHide 1 older comment
Jaevon Stewart on 2 Apr 2024
Thank you! I simply needed to flip the sign in the while loop (facepalm) I completely missed the fact that that was incorrect.
Jaevon Stewart on 2 Apr 2024
Edited: Jaevon Stewart on 2 Apr 2024
Not flip the sign, change approxerr to 10^-5**

### Categories

Find more on Logical in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!