stuck in a small issue related to implementation of an aircraft trim condition code with cost function: error is "TOO MANY INPUT ARGUMENTS"
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% TRIM.m
clear; clc; close all;
global x u gamma;
x(1) = 170;
x(5) = 0;
gamma = 0;
name = input('Name of Cost function file? : ','s');
cg= 0.25; land=1; % 0 = clean 1=gear+flaps;
u = [0.1 -10 cg land];
x(2)= .1; % Alpha, initial guess
x(3)=x(2) + gamma; % Theta
x(4)=0; % Pitch rate
x(6)=0;
s0=[u(1) u(2) x(2)];
% Now initialize any other states and get initial cost
disp(['Initial cost= ', num2str(feval(name,s0))]);
[s,fval]= fminsearch(name,s0);
x(2) = s(3) ; x(3) = s(3)+ gamma;
u(l)=s(1); u(2)=s(2);
disp(['minimizing vector= ',num2str(feval)]);
disp(['minimum cost vector= ',num2str(s)]);
temp=[length(x), length(u),x,u];
name= input('Name of output file? : ',' s');
dlmwrite(name,temp);
% Medium-sized transport aircraft, longitudinal dynamics
time = 0.0;
function[xd] = transp(time,x,u)
%Aircraft Data
S=2170.0; CBAR=17.5; MASS=5.0E3; IYY= 4.1E6;
TSTAT=6.0E4; DTDV =-38.0; ZE = 2.0; CDCLS= .042;
CLA = .085; CMA = -.022; CMDE =-.016; % per degree
CMQ =-16.0; CMADOT= -6.0; CLADOT= 0.0; % per radian
RTOD = 57.29578; GD=32.17;
% defining input parameters
THTL = u(1);
ELEV = u(2);
XCG = u(3);
LAND = u(4);
VT = x(1);
ALPHA= RTOD*x(2);
THETA= x(3);
Q = x(4);
H = x(5);
QBAR = 34.34; %0.5 * (2.377E-3) * VT^2;
QS = QBAR*S;
SALP= sin (x(2));
GAM = THETA - x(2); SGAM = sin(GAM); CGAM = cos(GAM);
if LAND == 0
CLO= .20; CDO= .016;
CMO= .05; DCDG= 0.0; DCMG= 0.0;
elseif LAND == 1 % LANDING FLAPS ON & GEAR DOWN
CLO= 1.0;
CMO= -.20; DCDG= 0.02; DCMG= -0.05;
else
disp('Landing Gear and Flaps ?')
end
THR= (TSTAT+VT*DTDV)*max(THTL,0); % THRUST
CL=CLO+CLA*ALPHA; % NONDIM. LIFT
CM=DCMG+CMO+CMA*ALPHA+CMDE*ELEV+CL*(XCG-.25); % MOMENT
CD = DCDG+CDO+(CDCLS*CL*CL); % DRAG POLAR
% State EQUATIONS NEXT
xd(1) = (THR*CALP) - (QS*CD)/ MASS - (GD*SGAM);
xd(2)= (-THR*SALP-QS*CL+MASS*((VT*Q)+(GD*CGAM)))/(MASS*VT+QS*CLADOT);
xd(3)= Q;
xd(5)= VT*SGAM; % Verticle Speed
xd(6)= VT*CGAM; % Horizonal Speed
D = .5*CBAR*(CMQ*Q+CMADOT*xd(2))/VT; % PITCH DAMPING
xd(4)= (QS*CBAR*(CM + D) + THR*ZE)/ IYY; % Q-DOT
end
%% COST FUNCTION
function[f]=cost_func(s)
global x u
u(1) = 0.1;
u(2) = -10;
x(2) = 0;
x(3) = x(2);
u(1)=s(1); u(2)=s(2);
x(2) = s(3);
s0=[u(1) u(2) x(2)];
time = 0.0;
[xd] = transp(time,x,u);
f= xd(1)^2 + 100*xd(2)^2 + 10*xd(4)^2;
end
3 commentaires
Réponse acceptée
VBBV
le 9 Fév 2024
Modifié(e) : VBBV
le 9 Fév 2024
Few syntax and typo errors that are persistent in your code need to checked.
% TRIM.m
clear; clc; close all;
% x u gamma;
x = zeros(1,6); % define x
u = zeros(1,4); % define u
x(1) = 170;
x(5) = 0;
gamma = 0;
name = 'cost_func' %input('Name of Cost function file? : ','s');
cg= 0.25;
land=1; % 0 = clean 1=gear+flaps;
u = [0.1 -10 cg land];
x(2)= .1; % Alpha, initial guess
x(3)=x(2) + gamma; % Theta
x(4)=0; % Pitch rate
x(6)=0;
s0=[u(1) u(2) x(2)];
% Now initialize any other states and get initial cost
disp(['Initial cost= ', num2str(feval(name,x,u,s0))]);
[s,fval]= fminsearch(@cost_func,s0); % syntax error while using fminsearch
x(2) = s(3) ; x(3) = s(3)+ gamma;
u(1)=s(1); % typo error
u(2)=s(2);
disp(['minimizing vector= ',num2str(fval)]); % typo error
disp(['minimum cost vector= ',num2str(s)]);
temp=[length(x), length(u),x,u];
% name= input('Name of output file? : ',' s');
% dlmwrite(name,temp);
% Medium-sized transport aircraft, longitudinal dynamics
time = 0.0;
%% COST FUNCTION
function[f]=cost_func(x,u,s)
% global x u
u(1) = 0.1;
u(2) = -10;
x(2) = 0;
x(3) = x(2);
x(4) = 0;
x(5) = 0; x(6) = 0;
% u(1)=s(1);
% u(2)=s(2);
% x(2) = s(3);
s0=[u(1) u(2) x(2)];
time = 0.0;
[xd] = transp(time,x);
f = xd(1)^2 + 100*xd(2)^2 + 10*xd(4)^2;
end
function[xd] = transp(time,x)
%Aircraft Data
S=2170.0; CBAR=17.5; MASS=5.0E3; IYY= 4.1E6;
TSTAT=6.0E4; DTDV =-38.0; ZE = 2.0; CDCLS= .042;
CLA = .085; CMA = -.022; CMDE =-.016; % per degree
CMQ =-16.0; CMADOT= -6.0; CLADOT= 0.0; % per radian
RTOD = 57.29578; GD=32.17;
cg= 0.25;
land=1; % 0 = clean 1=gear+flaps;
u = [0.1 -10 cg land];
% x = zeros(1,6);
% defining input parameters
THTL = u(1);
ELEV = u(2);
XCG = u(3);
LAND = u(4);
VT = x(1);
ALPHA= RTOD*x(2);
THETA= x(3);
Q = x(4);
H = x(5);
QBAR = 34.34; %0.5 * (2.377E-3) * VT^2;
QS = QBAR*S;
SALP= sin (x(2));
GAM = THETA - x(2);
SGAM = sin(GAM);
CGAM = cos(GAM);
THR= (TSTAT+VT*DTDV)*max(THTL,0); % THRUST
if LAND == 0
CLO= .20; CDO = .016;
CMO= .05; DCDG= 0.0; DCMG= 0.0;
CL=CLO+CLA*ALPHA; % NONDIM. LIFT
CM=DCMG+CMO+CMA*ALPHA+CMDE*ELEV+CL*(XCG-.25); % MOMENT
CD = DCDG+CDO+(CDCLS*CL*CL); % DRAG POLAR
elseif LAND == 1 % LANDING FLAPS ON & GEAR DOWN
CLO= 1.0;
CMO= -.20; DCDG= 0.02; DCMG= -0.05;
CDO = .016;
CL=CLO+CLA*ALPHA; % NONDIM. LIFT
CM=DCMG+CMO+CMA*ALPHA+CMDE*ELEV+CL*(XCG-.25); % MOMENT
CD = DCDG+CDO+(CDCLS*CL*CL); % DRAG POLAR
%
else
disp('Landing Gear and Flaps ?')
end
% State EQUATIONS NEXT
xd(1) = (THR*SALP) - (QS*CD)/ MASS - (GD*SGAM);
xd(2)= (-THR*SALP-QS*CL+MASS*((VT*Q)+(GD*CGAM)))/(MASS*VT+QS*CLADOT);
xd(3)= Q;
xd(5)= VT*SGAM; % Verticle Speed
xd(6)= VT*CGAM; % Horizonal Speed
D = .5*CBAR*(CMQ*Q+CMADOT*xd(2))/VT; % PITCH DAMPING
xd(4)= (QS*CBAR*(CM + D) + THR*ZE)/ IYY; % Q-DOT
end
Plus de réponses (1)
Matthew Sisson
le 9 Fév 2024
Modifié(e) : Matthew Sisson
le 9 Fév 2024
Your error might be related to user input on the following line.
name = input('Name of Cost function file? : ','s');
I assume you are entering "cost_func" as shown:
--> Name of Cost function file? : cost_func
Voir également
Catégories
En savoir plus sur Multimodal dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!