I need help constructing a more efficient code to plot multiple data points on one step plot
Mostrar comentarios más antiguos
I am working on an assignment for my Stability and Control class that is pretty extensive as far as lines of code are concerned. We are to code flight dynamics for a Cessna 182 aircraft in which we are analyzing the flight affects of things such as:
- AoA/Pitch due to elevator deflection
- Slide slip due to aileron/rudder deflection
- Heading due to aileron/rudder deflection
We are analyzing the short period, phugoid, dutch roll, roll, short period approximation, phugoid approximation, Dutch roll approximation, and roll approximations transfer functions for the aircraft in 3 different flight conditions; Climb, Cruise, and Approach. We are given the aircrafts specific data, longitudinal, and lateral-directional variables.
I have already created the code for each aformentioned condition but they are in separate .m files. What I would ultimately like to happen is have a single file or a couple of files (longitudinal and lateral-directional) that can model these dynamics and output a step(sys) plot that overlays the first 3 seconds of the dynamics (t = 0:0.01:3) for each analysis with its associated approximation e.g. AoA to elevator deflection for climb, cruise, and approach short period dynamics and short period approximation for climb, cruise, and approach -- 6 trends on one plot.
I am adding the code for just the AoA to elevator deflection and approximation for all three flight conditions to this query to see if someone is able to find a solution that is more efficient than having separate files. I am not MATLAB savvy enough to create a more truncated solution yet.
4 comentarios
Brian
el 21 de Nov. de 2022
When you say more efficient, do you mean in terms of writing code?
The easiest change would probably be combining the shared math calculations into a single file or function, then just calling that for each of your cases.
It also looks like you could also combine the approximations. Assuming the calculations and parameters are the same, "SaC_AOA_Elev_SP_Approx_Approach.m", for example, could be reduced to:
SaC_AOA_ElevDeflection_Approach;
%%Approximation Denominator Polynomial Approximation
D_1 = U_1*[1 -(M_q+Z_a/U_1+M_adot) (Z_a*M_q/U_1-M_a)]
%AOA to Elevetor Deflection Numerator Polynomial Approximation
Na = [Z_dele (M_dele*U_1-M_q*Z_dele)]
%Transfer Function
sys = tf(Na,D_1)
damp(sys)
Since much of the code is shared with the "SaC_AOA_ElevDeflection_Approach.m" file.
Given the uploaded files, you could collect and plot their outputs onto a single plot with another file, as below.
SaC_AOA_ElevDeflection_Cruise;
hold on
SaC_AOA_ElevDeflection_Climb;
SaC_AOA_ElevDeflection_Approach;
SaC_AOA_Elev_SP_Approx_Cruise;
step(sys,0:0.01:3)
SaC_AOA_Elev_SP_Approx_Climb;
step(sys,0:0.01:3)
SaC_AOA_Elev_SP_Approx_Approach;
step(sys,0:0.01:3)
hold off
xlabel("Time (t)");
ylabel("Amplitude");
title('Combined AoA Plot')
legend('Cruise','Climb','Approach','Cruise Estimation','Climb Estimation','Approach Estimation')
You may want to adjust the styling of the plot to make it more readable.
Do you have more specific questions about what you are trying to do?
Paul
el 21 de Nov. de 2022
Another option would be to refactor the code by a little bit so that all related transfer functions are stored using Model Arrays. Then just a single command is needed, e.g.
step(sysArray)
Jeffrey Lewis
el 22 de Nov. de 2022
If using a model array, each model is stored as one element of the array. The model array itself can be multi-dimensional if it makes sense to do so. Here's a simple example.
sys1 = tf(1,[1 1],'Name','pitch');
sys2 = tf(1,[2 1],'Name','yaw');
sys = stack(1,sys1,sys2);
step(sys),legend
Actually, now that I think of it one of the drawbacks of Model Arrays is shown above, i.e., it's not easy to distinguish which curve on the plot goes with which model. Maybe it's easier to do
step(sys1,sys2),legend
Respuestas (0)
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

