%***********************************************************************************
% 
% Pitching Moment Subfunction
% Primary authors: Kacie Burton, Kevin Dahya, Mellisa Glaser, Kerem Koray
% Wael Nour, and Tanya Tunistra
% AAE 421 Design Project Spring 2001
%
%***********************************************************************************

function [Cm0,Cma_dot,Cm_de,xbar_ac,Cm_a]=pitchingmoment(Cl_alphah,eta_h,Vh,xh,cbar,dE_da,CL_de,l_H,Cl_alpha_h,Vh_bar,lambda,lambda_c4,A,B,xw,Cl_alpha_w,S_horz,Sref,xbar_ach,xbar_cg,xbar_ac,Cl_alpha,xbar_acw,Cl_alpha_wb)


% These equations were given in Roskam, Methods for Estimating Stability and Control Derivative os Conventional Subsonic Airplanes

Cm0 = -0.04;																%Selig data about c/4
Cma_dot_w=0;																%assumed zero except for triangular wings
Cma_dot_h=-2*Cl_alphah*eta_h*Vh*(xh/cbar)*dE_da;				%Eqn 6.5 of Roskam Handout (rad-1)
Cma_dot=Cma_dot_w+Cma_dot_h											%Eqn 6.4 of Roskam Handout (rad-1)

l_H=27.8/12;																%Measured from Boiler Xpress
Cm_de=-CL_de*(l_H/cbar)													%Eqn 10.7 of Roskam Handout
Cmqh=-2*Cl_alpha_h*eta_h*Vh_bar;										%Eqn 5.4 of Roskam Handout (rad-1)
global K
K=.88;																		%From Figure 5.1 of Roskam Handout
Cmqw_M=-K*Cl_alpha_w*cos(lambda_c4)*(((A*(2*(xw/cbar)^2+.5*xw/cbar))/(A+2*cos(lambda_c4)))+(((1/24)*(A^3*(tan(lambda_c4))^2))/(A+6*cos(lambda_c4)))+(1/8));		%at M=0  Eqn 5.8 of Roskam Handout
Cmqw=Cmqw_M*(((((A^3)*(tan(lambda_c4))^2)/(A*B+6*cos(lambda_c4)))+3/B)/((((A^3)*(tan(lambda_c4))^2)/(A+6*cos(lambda_c4)))+3));						%at M  Eqn 5.7 of Roskam Handout
Cmq=Cmqw+Cmqh																%Eqn 5.6 of Roskam Handout

xbar_acB=0.32																%Estimate based on xcg
xbar_acw = 0.3;															%Figure 3.8 of Roskam Handout, measured from aircraft	
xbar_acWB=xbar_acB+xbar_acw;											%Eqn 3.19 of Roskam Handout
xbar_ac=(xbar_acWB+(Cl_alpha_h/Cl_alpha_wb)*eta_h*(S_horz/Sref)*xbar_ach*(1-dE_da))/(1+(Cl_alpha_h/Cl_alpha_wb)*eta_h*(S_horz/Sref)*(1-dE_da));	%Eqn 3.18 of Roskam Handout
dCm_dCL=xbar_cg-xbar_ac;												%Eqn 3.17 of Roskam Handout
Cm_a=(dCm_dCL)*Cl_alpha													%Eqn 3.16 of Roskam Handout
