How to plot an x value using for loop from an input?

22 views (last 30 days)
I am working on a project, and I am trying to graph the second half of the descent rate where the y axis is feet and the x axis is minutes. I want it to plot a point every minute, where it decreases 3000 feet per minute starting at the crusing altitude, down to 10,500 feet, and it plots a point every minute. So far I have got the ascent rate to work and the first half of the descent rate to work. Everytime I run it, I get an error about the indexing. This is also based off of a GUI, so the numbers below are able to be changed. sa= starting altitude, A1=crusing altitude, ar=ascent rate, FS=font size, sm=starting minutes, yd=y-axis point in first half descent, xd=x-axis point in first half descent, dcx=decent y values, dcy=descent x values. The points should be (mdd,13500) (minutes-9,16500) (minutes-10,19500)(minutes-11,22500) etc to A1. Also, how do I get the dots connected to each other for the first half of the descent rate?
elevation=525;
A1=30000;
minutes=135;
sa=0;
sm=0;
mb1=minutes-7;
mb2=minutes-6;
mb3=minutes-5;
mb4=minutes-4;
mb5=minutes-3;
mb6=minutes-2;
mb7=minutes-1;
mb8=minutes;
mdd=minutes-8;
x=elevation:2000:A1;
y=0:1:A1;
dcx=A1:-3000:10500;
dcy=mdd:-1:A1;
for i=1:length(x)
ar(i)=sa+x(i);
x1(i)=sm+y(i);
end
for k=1:length(dcx)
yd(k)=dcx(k);
xd(k)=dcy(k);
end
FS=12;
% axes(handles.timevsheight);
plot(x1,ar,'-o r','linewidth',2);
xlabel('Time (Minutes)','Interpreter','latex','FontSize',FS);
ylabel('Height (Feet)','Interpreter','latex','FontSize',FS);
xlim([0 minutes+5])
ylim([0 A1+1000])
title('Time vs Height')
hold on
plot(mb1,10500,'-o r','linewidth',2);
hold on
plot(mb2,9000,'-o r','linewidth',2);
hold on
plot(mb3,7500,'-o r','linewidth',2);
hold on
plot(mb4,6000,'-o r','linewidth',2);
hold on
plot(mb5,4500,'-o r','linewidth',2);
hold on
plot(mb6,3000,'-o r','linewidth',2);
hold on
plot(mb7,1500,'-o r','linewidth',2);
hold on
plot(mb8,0,'-o r','linewidth',2);
hold on
plot(xd,yd,'-o r','linewidth',2);
hold off

Answers (2)

the cyclist
the cyclist on 8 May 2022
Edited: the cyclist on 8 May 2022
I have to admit that I did not fully understand your explanation. But I can tell you the specific reason you are getting an indexing error from that code. Here is the first part of your code, with lines commented out if they are not directly relevant to the indexing issue.
% elevation=525;
A1=30000;
minutes=135;
% sa=0;
% sm=0;
% mb1=minutes-7;
% mb2=minutes-6;
% mb3=minutes-5;
% mb4=minutes-4;
% mb5=minutes-3;
% mb6=minutes-2;
% mb7=minutes-1;
% mb8=minutes;
mdd=minutes-8;
% x=elevation:2000:A1;
% y=0:1:A1;
% dcx=A1:-3000:10500;
dcy=mdd:-1:A1
dcy = 1×0 empty double row vector
Notice that dcy is an empty vector. The reason is that you are defining dcy as
127 : -1 : 30000
which is empty because you cannot get to 30000 from 127 in steps of -1. :-)
That definition is surely not what you intend. (But I could not glean what you did intend.)
  1 Comment
Hunter Nau
Hunter Nau on 8 May 2022
Sorry for not explaing better. I am trying to create points just like I did for the ascent rate, but in the form of a descent rate. I used the for loop to create the array since I knew the point the array ends on but not the point it starts at since it changes everytime, which is why the indexing is negative. I hope this explains it better

Sign in to comment.


William Rose
William Rose on 9 May 2022
@Hunter Nau, You are plotting each point of the descent as a separate series, which is why you cannot connect them. This also makes the script cumbersome. Make a single vector of all the times, from start to finish, and a corresponding vector of altitudes. The time spacng does not have to be uniform. In the example below, I used a final altitude different than the initial altitude, just to show that it can be done.
ra=2000; %ascent rate (ft/min)
rd=-3000; %descent rate (ft/min)
A0=0; %initial altitude
A1=30000; %crusing altitude (ft)
Af=6000; %final altitude
Tf=135; %final time
da=(A1-A0)/ra; %duration of ascent
dd=(Af-A1)/rd; %duration of descent
Td=Tf-dd; %time descent begins
t=[0:da,Td:Tf];
a=[A0+ra*(0:da), A1+rd*((Td:Tf)-Td)];
plot(t,a,'-ro');
Try it. Good luck.
  1 Comment
Hunter Nau
Hunter Nau on 10 May 2022
ok, I just need to have a for loop in the code. I tried using mine but I am getting an error with the vectors not being the same length.
elevation=525;
A1=30000;
minutes=135;
sa=0; % Starting altitude
sm=0; % Starting minutes
x=elevation:2000:A1; % Ascent array based on elevation and altitude input
% on y axis
y=0:1:A1; % Ascent array based on minutes and altitude for x axis
rd=-3000;
af=0;
dd=(af-A1)/rd;
td=minutes-dd;
for i=1:length(x) % Loop for ascent rate values
ar(i)=sa+x(i);
x1(i)=sm+y(i);
end
t=[0:x1,elevation:A1];
a=[elevation+ar*(0:x1),A1+rd*((td:minutes)-td)];
% axes(handles.timevsheight);
plot(t,a,'-ro');

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!