For loops and If statements not working correctly
4 views (last 30 days)
Show older comments
Deepak Singh
on 13 Oct 2019
Commented: Deepak Singh
on 14 Oct 2019
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:
0 Comments
Accepted Answer
Turlough Hughes
on 13 Oct 2019
First of all, when you use element-wise operators (.* .^ ./) like you have been trying you don't necessarily need to run the for loop
Your answer could be:
time =[0:0.1:25];
t1=time(time<5);
velocity1=0.1553567*t1.^6 - 2.0416*t1.^5 + 9.1837*t1.^4 - 14.829*t1.^3 - 1.3703*t1.^2 + 32.821*t1 - 1.3155;
t2=time(5<=time & time<=15.4)
velocity2 = 0.003980879*t2.^5 - 0.2247*t2.^4 + 4.8682*t2.^3 - 50.442*t2.^2 + 254.67*t2 - 430.66;
t3=time(time>15.4)
velocity3=-0.073*t3.^2 + 6.1802*t3 + 40.423
velocity=[velocity1 velocity2 velocity3]
plot(time,velocity)
However, when you run the for loop you are solving for one value of time on each iteration of the loop and so element-wise operators aren't necessary. Though you will need to include an index to indicate the position to store your newly determined velocity on each iteration of the loop.
I included an index c which increments by 1 each iteration of the loop and the new value for velocity is stored in velocity(1,c)
So, the first three iteration of your loop will do the following
velocity (1,1) = answer when you plug t=0 into equations
velocity(1,2) = answer when t=0.1
velocity(1,3) = answer when t=0.2
and so on.
See the following:
clc
clear
time = [0:0.1:25];
velocity = zeros(size(time)); % time is a row vector, so best to keep velocity as a row vector.
c=0;
for t = time
c=c+1; % index
if t < 5
velocity(1,c) = 0.1553567*t^6 - 2.0416*t^5 + 9.1837*t^4 - 14.829*t^3 - 1.3703*t^2 + 32.821*t - 1.3155;
elseif 5<=t && t<=15.4
velocity(1,c) = 0.003980879*t^5 - 0.2247*t^4 + 4.8682*t^3 - 50.442*t^2 + 254.67*t - 430.66;
else
velocity(1,c) = -0.073*t^2 + 6.1802*t + 40.423;
end
end
plot(time,velocity)
More Answers (1)
Walter Roberson
on 13 Oct 2019
https://www.mathworks.com/matlabcentral/answers/480004-need-help-with-taking-an-array-inside-a-for-loop-and-plotting-it#answer_391484
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!