function Hcalibrated=ALTWarrior3(Hindicated,Vindicated,flap)
%function Hcalibrated=ALTWarrior3(Hindicated,Vindicated,flap)
% Correct indicated altitude for instrument error and static position error.
% Data is extracted from Piper Warrior III Information Manual, page 5-12.
% This data gives the velocity position error correction.
% In this function we convert the velocity position error to 
% an altitude position error.
% It is assumed that the Piper Warrior III has zero instrument correction.
% Therefore 
%             Hinstrumentcorrected (Hic) = Hindicated (Hi)
% Given below are the position error corrections to get Hcalibrated. 
% They are a function of Hindicated, Vindicated, and flap position.
%             Hcalibrated=f(Hindicated,Vindicated,flap)
% Hindicated is indicated pressure altitude in geopotential feet.
% Hcalibrated is calibrated pressure altitude in geopotential feet.
% Vindicated is indicated airspeed in knots.
%                   flap=0   for no flap
%                   flap=0.5 for half flap
%                   flap=1   for full flap
% Approximation
%   rho is computed from Hi not Hc (however three iterations are made to
%   improve this.
% Ref: Ward and Strganac, pages 18-19

Vcalibrated=AS1Warrior3(Vindicated,flap);
DVpc=Vcalibrated-Vindicated;
% Use equation 2.3 (p.19) for subsonic speed
k=1.4*2116.22/661.489^2;
A0=661.489; % speed of sound at sea level, knots
DPpoverDVpc=k*Vindicated.*(1+.2*(Vindicated.*Vindicated)/(A0*A0)).^2.5;
DPp=DPpoverDVpc.*DVpc;

% Use equation 2.24 on page 18
% Iteration 1
[temp,press,rho,Hgeopvector]=atmosphere4(Hindicated,0);
DHpc=DPp./(rho*32.17);
Hcalibrated=Hindicated+DHpc;

% Iteration 2 to improve calculation of rhostd
[temp,press,rho,Hgeopvector]=atmosphere4(Hcalibrated,0);
DHpc=DPp./(rho*32.17);
Hcalibrated=Hindicated+DHpc;

% Iteration 3 to improve calculation of rhostd
[temp,press,rho,Hgeopvector]=atmosphere4(Hcalibrated,0);
DHpc=DPp./(rho*32.17);
Hcalibrated=Hindicated+DHpc;

% Convergence is good after three iterations
