% Experiment #1 Stall Speed and CLmax Determination
% Ref: NTPS, pages D24-D26.
% This MATLAB script computes stall airspeed and CLmax
% given Vindicated, flap setting, indicated geopotential pressure altitude.
% It assumes standard outside air temperature for the calibrated pressure altitude.
disp(' ')
disp('Start here')
disp('Experiment 1') %Example 1
disp(' ')
clear all
echo on
% Compute stall speed and Clmax

% Item 1 Indicated pressure altitude for the flight experiment.
% The following vector must be of the same length as Vi, flap and FuelWeight.
Hi=[4500,4100,4400]   %indicated geopotential feet at stall. Put your data in this vector.

% Item 2 Indicated airspeed and flap setting from the experiment.
% The following vectors must be of the same length as Hi and FuelWeight.
Vi=  [61,60,59]   %stall speed knots. Put your data in this vector and the one below.
flap=[ 0, 0, 0] % Flap setting %flap=0 for no flap %flap=0.5 for half flap %flap=1 for full flap

% The following step is not listed in the NTPS Data Reduction procedure, 
% but we will do this in this and other experiments.
% Correct indicated altitude for instrument and position errors.
Hcalibrated=ALTBaron58(Hi,Vi,flap)

% Items 3 and 4 Correct for indicated airspeed for instrument, position errors.
Vcalibrated=AS1Baron58(Vi,flap) % Airspeed units are KNOTS.

% Items 5 and 6 Compute equivalent airspeed
% Item 6a Get standard pressure (p, lbf/ft^2) for calibrated pressure altitude.
GeometricFlag=0 %Hvector is Geopotential altitude
[temp,p,rho,Hgeopvector]=      atmosphere4(Hcalibrated,GeometricFlag);
p
% Item 6b Get equivalent airspeed (Vequivalent)
[Vequivalent,qcOverp0,qcOverp]=AS2(Vcalibrated,p); %Knots
Vequivalent

% Item 7 Compute test weights.
ZeroFuelWeight=4500 % pounds <<<Use the number Professor Holleman gave you.
% The following vector must be of the same length as Hi, Vi, and flap.
FuelWeight=[900,800,700]  % pounds (lbf). Put your data in this vector. (6lbf/gal)
Wt=ZeroFuelWeight+FuelWeight

% Item 8
rho0=0.00237691267925741; % Sea level density, slugs per cubic foot
S=199.2; % ft squared
Vefps=1.687808*Vequivalent % convert knots to ft/sec
Clmax=2*Wt./(rho0*Vefps.*Vefps*S)

% Item 8 done using true airspeed (Vtrue, knots)
% In this calculation we assume standard temperature for the calibrated pressure altitude.
% It would be better if we had mesured outside air temperature, but we did not in this experiment.
temp     %degree Rankine
[Vtrue,M,aknots,rho,sigma]=    AS3(Vequivalent,temp,Hcalibrated); % knots
Vtfps=1.687808*Vtrue % convert knots to ft/sec
Clmax2=2*Wt./(rho.*Vtfps.*Vtfps*S)

% Item 9 Stall speed corrected for weight
Ws=5500 % pounds (lbf), standard weight is maximum takeoff weight (Baron 58 manual p. 1-12)
Vs1=Vequivalent.*sqrt(Ws./Wt) % knots


% *********
% Compute warning speed

% Item 10 Indicated ressure ltitude for the flight experiment.
% The following vectors must be of the same length as Hi and FuelWeight.
Viwarn=[64,66,63]   % indicated stall warning speed knots. Put your data in this vector and the one below.

% The following step is not listed in the NTPS Data Reduction procedure, 
% but we will do this in this and other experiments.
% Correct indicated altitude for instrument and position errors.
Hcalibratedwarn=ALTBaron58(Hi,Viwarn,flap)

% Items 11 and 12 Correct for indicated airspeed for instrument, position errors.
Vcalibratedwarn=AS1Baron58(Viwarn,flap) % Airspeed units are KNOTS.

% Items 13 and 14 Compute equivalent airspeed
% Item 14a Get standard pressure (p, lbf/ft^2) for calibrated pressure altitude.
GeometricFlag=0 %Hcalibratedwarn is Geopotential altitude
[temp,pwarn,rho,Hgeopvector]=      atmosphere4(Hcalibratedwarn,GeometricFlag);
pwarn
% Item 14b Get equivalent airspeed (Vequivalent)
[Vequivalentwarn,qcOverp0,qcOverp]=AS2(Vcalibratedwarn,pwarn); %Knots
Vequivalentwarn

% Item 15 Warning speed corrected for weight
Vwarn=Vequivalentwarn.*sqrt(Ws./Wt) % knots

% Item 16 Stall warning
StallWarning=Vwarn-Vs1 %knots
echo off
