How to make a for loop add results together consecutively?
1 view (last 30 days)
Show older comments
Dexter Greene
on 26 Jan 2021
Commented: Dexter Greene
on 26 Jan 2021
Hi, very new to Matlab and coding in general, I can't work out how to use a for loop to calculate individual results and add them together one by one as it progresses. I have a set of readings for acceleration at a fixed range of velocities, and my script can calculate the time taken to accelerate from each velocity reading to the next, but I cannot work out how to have my for loop add each time reading as it is calculated in order to plot time against velocity.
Airspeed.Thrust is a set of thrust readings from a separate table.
l = 2.4 %wingspan
m = 0.465+(l*0.1) %mass of aircraft
%calc
a = Airspeed.Thrust./m
%loop
t = 0
for v = 0:16
t = (1./a)
end
I tried to alter my loop to:
for v = 0:16
t = t+(1./a)
end
But for some reason this gives a completely different starting value, but produces an identically shaped graph.
0 Comments
Accepted Answer
David Hill
on 26 Jan 2021
No loop is needed assuming your acceleration values are in an array.
v=0:16;
t=1./a;
plot(cumsum(t),v);
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!