# How to delete last value in the plot?

26 views (last 30 days)
BioZ on 12 Oct 2021
Commented: BioZ on 12 Oct 2021
Hi all, just a quick question I have the following code, which plots the trajectory of the aircraft, I have added a 0.15s pause at the end of the for loop for animation sake. My problem is that when the "Animation is running it plots the last value as 0 which means there is always a line going to the initial condition (Starting point) any idea how to fix it?" Any help would be much appreciated. clc; clear all; close all
%Aircraft parameters
g=9.80665;
m=41000;
W = m*g;
Cd0 = 0.02
S=120%m^2
b=34 %m
AR=b^2/S
ef=0.82; % Efficiency Factor
K=0.03 % combined induced drag factor, = 𝑘 ∕ (𝜋 𝐴𝑅)
Ta = 110000; %Thrust Available
Tp=0.7; %Thrust Percentage
Tu=Ta*Tp; % (Thrust Used)
Cl=sqrt((pi()/3)*ef*AR*Cd0);
Cd = K+Cd0*Cl^2
%Cd=Cd0+((Cl^2)/(pi()*ef*AR));
tf = 60; %Final time
dt = 1; %Time step
t = 0:dt:tf;
V0 = 40; %Initial Velocity
X0 = 0; %Initial Displacement
h0 = 0; %Initial Height
gamma0 = 0.0872665 % 5deg initial climb.
%Pre-Fill with zeros in order to avoid buildup in the loop.
V = zeros(1,length(t))
V(1) = V0; % Initial velocity in dt frame
X = zeros(1,length(t))
X(1) = X0; % Initial X displacement in dt frame
h = zeros(1,length(t));
h(1) = h0; %Initial h displacement in dt frame
gamma = zeros(1,length(t));
gamma(1) = gamma0; %Initial gamma in dt frame
for n=2:length(t)
rho = (20-h(n-1)/1000)/(20+h(n-1)/1000)*1.225;
D = Cd*S*0.5*rho*V(n-1)^2;
V(n) = V(n-1)+((Tu-D-W*sin(gamma(n-1)))/m)*dt;
ROC = ((Tu*V(n)-D*V(n))/W);
gamma(n)= gamma(n-1)+asin(ROC/V(n));
X(n) = X(n-1)+V(n)*dt*cos(gamma(n));
h(n) = h(n-1)+V(n)*dt*sin(gamma(n));
pause(0.15)
plot(X,h);
end
%% equation checks
rhotest= (20-h(61)/1000)/(20+h(61)/1000)*1.225;
tst2=((Tu-D-W*sin(gamma(61)))/m)*dt;
%V(n) = V(n-1)+(1/m)*(Ta-D-W*sin(gamma(n-1)));
tst=(1/m)*(Tu-D-W*sin(gamma(1)));
%% Plots
plot(X,h);
xlabel('Distance (m)')
ylabel('Altitude(m)')
figure;
plot(gamma,t);

Walter Roberson on 12 Oct 2021
X = zeros(1,length(t))
X(1) = X0; % Initial X displacement in dt frame
h = zeros(1,length(t));
You are pre-allocating X and h to maximum length. You loop writing more and more values into them, and at each step you
plot(X,h);
but that asks to plot all of X against all of h -- including plotting the still-zero parts that you have not written into yet.
You should instead only plot up to point n
plot(X(1:n), h(1:n))
BioZ on 12 Oct 2021
That is fantastic, thank you very much!

### Categories

Find more on Orange in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!