For loops and If statements not working correctly
Show older comments
Hello,
I'm trying to plot a time to velocity graph where the velocity is calculated from 3 equations. The three equations are:

This is what my code looks like:
clc
clear
time = [0:0.1:25];
velocity = zeros(length(time),1);
for t = time
if t <= 5
velocity = 0.1553567*time.^6 - 2.0416*time.^5 + 9.1837*time.^4 - 14.829*time.^3 - 1.3703*time.^2 + 32.821*time - 1.3155;
elseif (5<t) && (t<=15.4)
velocity = 0.003980879*time.^5 - 0.2247*time.^4 + 4.8682*time.^3 - 50.442*time.^2 + 254.67*time - 430.66;
else
velocity = -0.073*time.^2 + 6.1802*time + 40.423;
end
end
plot(time,velocity)
For some reason all the time seems to go through the else statement. Can anyone please help me out, I've been struggling on this simple code for a while now. The graph should also look something similar to this:

Accepted Answer
More Answers (1)
Walter Roberson
on 13 Oct 2019
0 votes
https://www.mathworks.com/matlabcentral/answers/480004-need-help-with-taking-an-array-inside-a-for-loop-and-plotting-it#answer_391484
Categories
Find more on Programming 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!