Azzera filtri
Azzera filtri

Undefined Function or Variable

7 visualizzazioni (ultimi 30 giorni)
AeroEng
AeroEng il 3 Mag 2020
In my code I have a nested if loop. However, I define a variable and then call it in a separate if statement and it says it is undefined when it is clearly defined above. Plese help me. My code is attached below along with the error code.
clc;
clear;
close all;
tic
% Initial Conditions
%%%%%%%%%%%%%% SOUNDING ROCKETS %%%%%%%%%%%%%%%%%
ML = 1 ; % kg - Payload Mass
% hmax = [10 20 30]*10^3 ; % ft - Maximum Altitude
% amax = [5 10 20] ; % NonDim - Normalized Max Acceleration
% SM = [1 2 3] ; % NonDim - Static Margin
hmax = 20*10^3;
amax = 10;
SM = 2;
rhos = 2700 ; % kg/m^3 - Shell Density (Aluminum)
rhop = 1772 ; % kg/m^3 - Propellant Density
sigmas = 60*10^6 ; % Pa - Shell Working Stress
N = 3 ; % NonDim - Number of Fins
g = 9.81 ; % m/s^2 - Acceleration due to Gravity
% Vertical Takeoff so we get;
theta = 0;
ct = cosd(theta);
% AT STP
T = 298 ; % Kelvin - Temperature
Pa = 101.325*10^3 ; % Pa - Pressure
gamma = 1.4 ;
R_gc = 287 ; % J/kg*K - Universal Gas Constant
a_ss = sqrt(gamma*R_gc*T) ; % Meters/Second - Speed of Sound
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part I
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rmax = 1+amax ; % ND - Mass Ratio
Weq = sqrt((hmax*g)./((log(Rmax)/2).* ...
(log(Rmax)-2)+((Rmax-1)./Rmax))) ; % Meters/Second - Equiv Vel.
tb = (Rmax-1)*Weq/(g*Rmax) ; % Seconds - Burn Time
Meq = Weq/a_ss ; % ND - Equiv Mach
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P0_Pa = (1+Meq^2*((gamma-1)/2))^(gamma/(gamma-1)) ; % ND - Pressure Ratio
P0 = P0_Pa*Pa ; % Pa - Pressure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Guessing Value for D based on Diagram
D = 0.0922; % Meters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delta = D*P0/(2*sigmas) ; % Meters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Guessing a lambda value:
lambdamax = 0;
% Guessing Value for L based on Diagram
while true
syms Li X_cp X_cg
t=D*P0/(2*sigmas);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the structural mass as a function of D, L, delta, rhos
Mn = delta*rhos*pi*D*(D+(sqrt(D^2+(D/2)^2))) ; % kg
Vf = 0.5*D*D*delta ; % Meters^3
Mf = 3*(rhos*Vf) ; % kg
Mfb = pi*D*rhos*D*delta ; % kg
Mcyl = (2*pi*(D/2)*(Li+D)+2*pi*(D^2/4))*rhos*delta ; % kg
Mfin = (3/2)*D*rhos*delta ; % kg
Mcone = pi*(D/2)*((D/2)+sqrt(D^2 + (D^2/4)))*rhos*delta ; % kg
Ms = Mcyl+Mfin+Mcone ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the Propellent Mass
R = Rmax ; % ND
Mp = ((R - 1)*(Ms + ML)) ; % kg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding Lp
Lp = Mp/(pi*D^2*rhop/4) ; % Meters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the Center of Pressure
Ckn = (4*N*(4/3))/(1+sqrt(6)) ; % ND
Xcp = (1.33*D + Ckn*(D+Li+(D/3)))/(2+Ckn) ; % Meters
% Finding the Center of Gravity
Xcgnum = Mcone*(2/3)*D + Mcyl*((Li+D)/2)+Mp*(2*D+Li-Lp+(Lp/2))...
+ Mfin*(D+Li+(1/3)*D);
Xcgden = Mcone+Mcyl+Mfin+Mp ;
Xcg = Xcgnum/Xcgden ; % Meters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
eqn1 = Xcp-Xcg-D*SM == 0 ; % Meters
% Finding the value for L
L1 =vpasolve(eqn1,Li);
L=L1(real(L1)>0);
%***************** Re-going through all non-symbolically *****************%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the structural mass as a function of D, L, delta, rhos
Mn = delta*rhos*pi*D*(D+(sqrt(D^2+(D/2)^2))) ; % kg
Vf = 0.5*D*D*delta ; % Meters^3
Mf = 3*(rhos*Vf) ; % kg
Mfb = pi*D*rhos*D*delta ; % kg
Mcyl = (2*pi*(D/2)*(L+D)+2*pi*(D^2/4))*rhos*delta ; % kg
Mfin = (3/2)*D*rhos*delta ; % kg
Mcone = pi*(D/2)*((D/2)+sqrt(D^2 + (D^2/4)))*rhos*delta ; % kg
Ms = Mcyl+Mfin+Mcone ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the Propellent Mass
R = Rmax ; % ND
Mp = vpa(((R - 1)*(Ms + ML))) ; % kg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding Lp
Lp = Mp/(pi*D^2*rhop/4) ; % Meters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the Center of Pressure
Ckn = (4*N*(4/3))/(1+sqrt(6)) ; % ND
Xcpi = vpasolve((1.3333*D + (Ckn*(D+L+D./2)))/(2+Ckn) - X_cp==0);
Xcp = Xcpi(real(Xcpi)>0) ; % Meters
% Finding the Center of Gravity
Xcgnum = Mcone*(2/3)*D + Mcyl*((L+D)/2)+Mp*(2*D+L-Lp+(Lp/2))...
+ Mfin*(D+L+(1/3)*D);
Xcgden = Mcone+Mcyl+Mfin+Mp ;
Xcgi = vpasolve((Xcgnum/Xcgden)-X_cg) ; % Meters
Xcg = Xcgi(real(Xcgi)>0) ; % Meters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Block 10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Lpf = L+D ; % Meters
lambda = ML/(Ms+Mp) ;
M0 = ML+Ms+Mp ; % kg
if (L>D && Xcp>Xcg && Lp<Lpf)
if (lambda >= lambdamax)
Lnew = L ;
Lpnew = Lp ;
Dnew = D ;
tnew = t ;
Xcpnew = Xcp ;
Xcgnew = Xcg ;
Msnew = Ms ;
M0new = M0 ;
Mpnew = Mp ;
lambdamax_new = lambdamax ;
lambdamax = lambda ;
D = D+0.001 ;
else
Lp = Lpnew ;
t = tnew ;
Xcp = Xcpnew ;
Xcg = Xcgnew ;
L = Lnew ;
D = Dnew ;
lambdamax = lambdamax_new ;
Ms = Msnew ;
M0 = M0new ;
Mp = Mpnew ;
break ;
end
else
Lp = Lpnew ;
t = tnew ;
Xcp = Xcpnew ;
Xcg = Xcgnew ;
L = Lnew ;
D = Dnew ;
lambdamax = lambdamax_new ;
Ms = Msnew ;
M0 = M0new ;
Mp = Mpnew ;
break ;
end
end
% Problem 1
% Creating the Table
Impluse = Weq/g;
m_dot = Mp/tb;
Thrust = m_dot*Impluse*g
pressure_R=P0/Pa
tD_r= t/D
t
D
L
Lp
ld_r =L/D
Xcg
Xcp
Mp
Ms
M0
lambdamax
Eplison = lambda_m*M_s/(M_l)
toc
Below is the error:
Undefined function or variable 'Lpnew'.
Error in Project1Code (line 227)
Lp = Lpnew ;

Risposte (1)

Walter Roberson
Walter Roberson il 4 Mag 2020
Modificato: Walter Roberson il 4 Mag 2020
if (L>D && Xcp>Xcg && Lp<Lpf)
That is false the first time through (because Lp > Lpf). Your code expects that it will be true the first time through, as you only assign to Lpnew when the condition is true.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by