1
votes

I have a simulation of an aircraft, and I'm trying to plot it's intended trajectory and the actual flightpath in a plot.

Initially I was using a XY Graph block from Simulink, but it doesn't allow to show a second signal in the background. So I thought about using an Interpreted MATLAB Function block and plot the two things there.

I have an initialization M-file where I define the vectors x_t and y_t, the coordinates of the trajectory, and open the figure. I make those two global variables and the function in Simulink receives them, together with x_e and y_e from the simulation.

Now the problem is that it gets really slow, so much I can't actually control the aircraft properly. Any suggestions on how to achieve my goal? I appreciate your help.

Below I have put the code I'm using for that purpose, so that you can see the whole picture. By the way, 'i' is a variable being used to store data into vectors and is being incremented in another function.

*** Initialization.m ***

% The definition of x_1:x_5 and y_1:y_5 has been omitted.

x_t=[x_1 x_2 x_3 x_4 x_5];
y_t=[y_1 y_2 y_3 y_4 y_5];

figure(1);

---------------------------- 

*** Flightpath.m ***

function Flightpath(u)

%% Variables
%%
global i x_t y_t x_e y_e;

x_e(i)=u(1); y_e(i)=u(2);

%% Real-Time Graphic
%%
t=plot(x_t,y_t,20000,10000,'ro',x_e,y_e);
axis([-5000 25000 -5000 50000]); set(t,'MarkerFaceColor','r');
1

1 Answers

1
votes

There are multiple things that you should change,

  1. You are growing the size of x_e and y_e at each time step. That is always bad, and will slow things down considerably. (I recognize that you may not know the final size in advance, but that doesn't negate the fact that growing it at each step is bad.)

  2. You are using plot, a high level routine, when lower level functionality would suffice.

  3. You are plotting x_t and y_t at each time step, when it only needs to be plotted once.

  4. You should plot (possibly dummy data containing only 2 points) for x_e and y_e at time 0 with the line visibility turned off, and store the line handle. Then at each time step you should just get the XData and YData for the line, and replace it with new XData and YData that contains the new data the current time point.

  5. You would have much finer control of what's happening by writing an M-code S-Function that just using the Interpreted Function block.