function [f,g] = optimize_fixedwing(x0)

S_ref = x0(1,1);
x_wing = x0(1,2);
flap_angle_to = x0(1,3);
Sht_ref = x0(1,4);
Svt_ref = x0(1,5);
T_to_uninst = x0(1,6);
x_maingear = x0(1,7);
W_to = x0(1,8);
W_e = x0(1,9);
h = x0(1,10);
V = x0(1,11);


% _____________________________________________________________________
% Set Constants
% _____________________________________________________________________

% set to 1 for fixed wing, 1 for tiltwing
tiltwing = 0;

% set to 1 to produce output, 0 to suppress output
output = 0;

% WING GEOMETRY:
b_wing = 64;			% Wing span (ft)
taper_wing = 0.8;		% Taper ratio 
sweep_wing_25c = 5;	% Quarter-chord sweep angle  (deg)
tc_avg_wing = 0.14;		% Average Thickness-to-chord ratio
flap_to_chord = 0.3;	% Flap chord ratio
eta_i_flap = 0.2; 		% Flap inboard station
eta_o_flap = 0.6;		% Flap outboard station
flap_angle_cr = 0;	% Flap angle for cruise (deg)
flap_angle_land = 50;	% Flap angle for landing (deg)
slat_angle_to = 10; 	% Slat angle for takeoff (deg)
slat_angle_cr = 0; 	% Slat angle for cruise (deg)
slat_angle_land= 20; 	% Slat angle for landing (deg)
wing_angle_to = 10;		% Wing Angle for takeoff (tiltwing only)
wing_angle_cr = 0;		% Wing angle for cruise (tiltwing only)
wing_angle_land = 45;	% Wing angle for landing (tiltwing only)

% HORIZONTAL TAIL GEOMETRY:
b_ht = 29;		% Span (ft)
taper_ht = 0.8;		% Taper ratio
sweep_ht_25c = 5; 	% Quarter-chord sweep angle
tc_avg_ht = 0.12;		% Average Thickness-to-chord ratio
x_ht = 50;		% Distance from nose to leading edge (along x-axis) (ft)

% VERTICAL TAIL:
b_vt = 14;		% Span (ft)
taper_vt = 0.8;		% Taper ratio
sweep_vt_25c = 5;	% Quarter-chord sweep angle
tc_avg_vt = 0.10;		% Average Thickness-to-chord ratio
x_vt = 52;		% Distance from nose to leading edge (along x-axis) (ft)

% MISSION PROFILE:
h_to = 0;		% Takeoff Altitude (ft)
V_to = 55;			% Takeoff Speed (in knots)
h_land = 0;		% Landing Altitude (ft)
V_land = 45;		% Landing Speed (in knots)

% PROPULSION:
y_eng = 12;		% Distance from centerline to outboard engine (ft)
N_e = 4;				% Number of Engines

% LANDING GEAR GEOMETRY:
x_nosegear = 5;
gear_up = 0;
gear_down = 1;
y_maingear = 8.5;		% Distance from centerline to main gear (ft)

% WEIGHTS:
W_payload = 10000;		% Payload Weight (lb)

% ____________________________________________________________________


% Call all subsystem functions
% _________________________________________________________________________________________


% High-lift for takeoff configuration
[CL_max_to, Cmu_data_to, CL_data_to] = highlift(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,...
   tc_avg_wing,flap_to_chord,eta_i_flap,eta_o_flap, slat_angle_to, flap_angle_to);

% High-lift for cruise configuration
[CL_max, Cmu_data_cr, CL_data_cr] = highlift(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,...
   tc_avg_wing,flap_to_chord,eta_i_flap,eta_o_flap, slat_angle_cr, flap_angle_cr);

% High-lift for landing configuration
[CL_max_land, Cmu_data_land, CL_data_land] = highlift(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,...
   tc_avg_wing,flap_to_chord,eta_i_flap,eta_o_flap, slat_angle_land, flap_angle_land);

% Vn_Diagram
[n_ult, V_C, V_D, V_H] = vn(b_wing,S_ref,taper_wing,sweep_wing_25c,h,...
   V,W_to,CL_max);

% Propulsion
[L_eng,D_eng] = propulsion(tiltwing,T_to_uninst);

% Weights & Balance
[W_to,W_e,W_eng_single,x_cg_aft,x_cg_forward,z_cg_high,I_yy] =...
   wbfunction(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,tc_avg_wing,x_wing,...
   b_ht,Sht_ref,taper_ht,sweep_ht_25c,tc_avg_ht,x_ht,b_vt,Svt_ref,taper_vt,sweep_vt_25c,...
   x_vt,h,V,T_to_uninst,N_e,x_nosegear,x_maingear,W_to,W_payload,W_e,n_ult,V_C,V_D,V_H,D_eng,L_eng);

% Landing Gear
[upsw_angle_deg, toff_angle_deg, tov_angle_deg, pctwt_nose] = lgearfunction(x_nosegear,...
   x_maingear,y_maingear,x_cg_aft, x_cg_forward,z_cg_high);

% Drag for takeoff configuration
[CD_to,CL_to]=drag_opt(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,...
   tc_avg_wing,x_wing,flap_to_chord,eta_o_flap,eta_i_flap,flap_angle_to,...
   slat_angle_to,b_ht,Sht_ref,taper_ht,sweep_ht_25c,tc_avg_ht,x_ht,b_vt,...
   Svt_ref,taper_vt,sweep_vt_25c,tc_avg_vt,h_to,V_to,L_eng,D_eng,N_e,x_nosegear,...
   gear_down,x_cg_aft,x_cg_forward,CL_data_to,Cmu_data_to,wing_angle_to,1,output);

% Drag for cruise configuration
[CD_cr,CL_cr]=drag_opt(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,...
   tc_avg_wing,x_wing,flap_to_chord,eta_o_flap,eta_i_flap,flap_angle_cr,slat_angle_cr,b_ht,Sht_ref,...
   taper_ht,sweep_ht_25c,tc_avg_ht,x_ht,b_vt,Svt_ref,taper_vt,sweep_vt_25c,tc_avg_vt,...
   h,V,L_eng,D_eng,N_e,x_nosegear,gear_up,x_cg_aft,x_cg_forward,CL_data_cr,...
   Cmu_data_cr,wing_angle_cr,2,output);

% Drag for landing configuration
[CD_land,CL_land]=drag_opt(tiltwing,b_wing,S_ref,taper_wing,sweep_wing_25c,...
   tc_avg_wing,x_wing,flap_to_chord,eta_o_flap,eta_i_flap,flap_angle_land,...
   slat_angle_land,b_ht,Sht_ref,taper_ht,sweep_ht_25c,tc_avg_ht,x_ht,b_vt,...
   Svt_ref,taper_vt,sweep_vt_25c,tc_avg_vt,h_land,V_land,L_eng,D_eng,N_e,x_nosegear,...
   gear_down,x_cg_aft,x_cg_forward,CL_data_land,Cmu_data_land,wing_angle_land,3,output);

% Performance
[W_f_used,R_bl,R_cruise,t_cl,T_to,S_g_to,S_g_land] = performance(tiltwing,b_wing,S_ref,taper_wing,...
   sweep_wing_25c,flap_angle_to,flap_angle_land,wing_angle_to,wing_angle_land,h,V,T_to_uninst,N_e,...
   W_payload,CL_max,CL_max_to,Cmu_data_to,CL_data_to,CL_max_land,Cmu_data_land,CL_data_land,W_to,...
   W_e,CD_to,CL_to,CD_cr,CL_cr,CD_land,CL_land);

% Stability & Control
[cg_max, SM_min, Sht_reqd, V_mc, V_s_L] = stabcont(tiltwing, b_wing, S_ref,...
   taper_wing, sweep_wing_25c, x_wing, b_ht, Sht_ref, taper_ht, sweep_ht_25c,...
   x_ht, b_vt, Svt_ref, taper_vt, sweep_vt_25c, x_vt, h, V, y_eng, N_e, x_maingear,...
   CL_data_to, CL_data_land, W_to, x_cg_aft, x_cg_forward, z_cg_high, I_yy, T_to);

% Cost
[LCC,DOC] = cost_opt(tiltwing,h,V,N_e,V_C,W_to,W_e,W_f_used,R_bl,R_cruise,...
   W_eng_single,t_cl,T_to,output);

% Define Objective Function
f = LCC;
% _____________________________________________________________________


% _____________________________________________________________________
% Set Constraints
% _____________________________________________________________________

% WING GEOMETRY:
% None yet

% HORIZONTAL TAIL GEOMETRY:
% None yet

% VERTICAL TAIL:
% None yet

% MISSION PROFILE:
% None yet

% OVERALL GEOMETRY
% None yet

% PERFORMANCE
g(1,1) = -R_bl + 1500;			% Block Range (nm)
g(2,1) = S_g_to - 300;			% Takeoff groundroll (ft)
g(3,1) = S_g_land - 400;		% Landing groundroll (ft)

% STABILITY & CONTROL
g(4,1) = cg_max - 25;			% Maximum CG excursion (in % of mac)
g(5,1) = SM_min - 5;				% Minimum Static Margin (in % of mac)
g(6,1) = Sht_reqd - Sht_ref;	% Minimum horizontal tail area required for takeoff rotation (ft^2)
g(7,1) = V_mc - 1.2*V_s_L;		% Minimum control speed (ft/sec)

% PROPULSION:
% None yet

% LANDING GEAR GEOMETRY:
g(8,1) = 18 - toff_angle_deg;	% Tipover angle (deg) 
g(9,1) = tov_angle_deg - 54;	% Turnover angle (deg)
g(10,1) = 10 - upsw_angle_deg;	% Fuselage clearance angle (deg)
g(11,1) = 8 - pctwt_nose;		% Percent weight on nose

fprintf('One function call completed\n')

% WEIGHTS:
% None yet

% _____________________________________________________________________