How can I add a Variable with a variable inside of it?

2 次查看(过去 30 天)
Hello, Im sorry if this seems like a really complicated question:
So my question is: I have Aircraft A , B and C; However I have just put Aircraft A, is it possible I can add different aircrafts as Objects? And How cause its really annoying that I have to add More aaircraft by adding more variables.
clc;
clear;
%inputs for aircraft A:
L_D = 15.9;
L_D_Loiter = 18.4;
Endurance = 35*60;
C = 0.565/3600;
Alt_Range = 185200;
CruisingVelocity = 231.7661522;
MissionAirRange = 5556000;
Wo = 75e3;
Wpay = 16e3
% FUEL FRACTION ESTIMATION
MainMissionFuelRatio = exp(
(-MissionAirRange*C)/(CruisingVelocity*(L_D)
)
);
loiter = exp(
(-Endurance*C)/(
(L_D_Loiter)
)
);
AltMissionFuelRatio = exp(
(-Alt_Range*C)/(CruisingVelocity*(L_D)
)
);
Additional_Weight_Fraction = 0.995*0.99*0.98*0.995*0.995*0.999*0.99*0.995*0.95;
FuelFraction = 1 - (MainMissionFuelRatio*AltMissionFuelRatio*loiter*Additional_Weight_Fraction);
% TOTAL WEIGHT ESTIMATION:
k = 1;
error = 1;
while (error>= 0.05)
We_Wo = 0.97*Wo(k)^-0.06;
% Empty weight fraction
Wo(k+1) = Wpay/(1- We_Wo- FuelFraction);
% Total weight est
error = abs(
(Wo(k+1)-Wo(k)
)/Wo(k)
);
k = k+1;
end
  2 个评论
Stephen23
Stephen23 2022-4-28
"Hello, Im sorry if this seems like a really complicated question"
The question is very simple and so is the answer: use arrays.
"...its really annoying that I have to add More aaircraft by adding more variables."
Very annoying... and not required. The MATLAB approach is to use arrays. You should use arrays.
Jan
Jan 2022-4-28
Notes: Redefining the Matlab function "error" as a variable can cause unexpected side-effects. Avoid this, because it causes troubles frequently.
Matlab does not accept line breaks inside the code. Replace
error = abs(
(Wo(k+1)-Wo(k)
)/Wo(k)
);
by
err = abs((Wo(k+1) - Wo(k)) / Wo(k));

请先登录,再进行评论。

回答(1 个)

Ayush Modi
Ayush Modi 2023-9-29
Hi,
I understand you would like to save the values of each aircraft without having to use different variable names each time.
You can achieve this by using “struct”. Please refer to the below code snippet for defining “struct”.
aircraft = struct('L_D', [], 'L_D_loiter', [], 'Endurance', []); % Similarly, Add the rest of the properties
% Create multiple structure objects
numObjects = 3; % Number of objects to create
% Using an array of structures
aircraftarray(1:numObjects) = aircraft; % Here, each element in “aircraftarray” is an object.
aircraftarray(1).L_D = 15.6;
aircraftarray(1).L_D_loiter = 30;
aircraftarray(1).Endurance = 1002; % Add the values of other properties for first aircraft
% Add the values for second aircraft
aircraftarray(2).L_D = 12.9;
aircraftarray(2).L_D_loiter = 35;
aircraftarray(2).Endurance = 1222;
% Similarly, you can add values for other aircraft as well.
You can refer to the below MathWorks documentation for more information on “struct” function:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Guidance, Navigation, and Control (GNC) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by