If/ElseIf Statement Inequality Not Resolving Correctly

I'm trying to plot the angular position of a robotic arm whose equation of motion changes over time. Ideally the plot of the position should have this shape:
but instead looks like this:
I have an if/elseif/else statement to handle the change in equation. The problem is that "else" part is being tripped for all values of t, which is why the plot is linear. I cannot seem to figure out why this is, but I can only assume it's because I've made some mistake with my if conditions. Below is a copy of my code. Any help would be appreciated. Thank you!
vm1 = 0.41415;
theta01 = -0.57982;
thetaf1 = 0.57982;
hold on
t = linspace(0,3);
if (t < 0.5)
theta1 = theta01 + (vm1 .* (t .^ 2));
elseif (t > 2.5)
theta1 = thetaf1 - (vm1 .* (9 - (6 .* t) + (t .^ 2)));
else
theta1 = vm1 .* (t - 1.5);
end
plot(t, theta1)

 Accepted Answer

If statements only work on one value at a time. You can't have it check the entire vector all at the same time. You can put the if statement inside a for loop and loop through each value of t one by one:
for i = 1:length(t)
if (t(i) < 0.5)
theta1(i) = theta01 + (vm1 .* (t(i) .^ 2));
elseif (t(i) > 2.5)
theta1(i) = thetaf1 - (vm1 .* (9 - (6 .* t(i)) + (t(i) .^ 2)));
else
theta1(i) = vm1 .* (t(i) - 1.5);
end
end
or create a piecewise function:
theta1 = (theta01 + (vm1 .* (t .^ 2))).*(t<0.5) + ...
(thetaf1 - (vm1 .* (9 - (6 .* t) + (t .^ 2)))).*(t>2.5) + ...
(vm1 .* (t - 1.5)).*(t>=0.5).*(t<=2.5);
The actual functions may not be quite right yet, but at least now you can visualize it.
piecewiseFxn.png

7 Comments

This is exactly what I needed, thank you so much!
Quick followup question, is there a way to have i increment by very small amounts? Say, 0.001? I think that might fix the weird disjunction at 0.5 and 2.5.
You have two ways to do that. Either increase the number of points created by linspace (100 is default):
t = linspace(0,3,500);
or use the colon operator instead and specify the increment:
t = 0:0.001:3;
I figured it out, I just needed to adjust the value of vm. Thank you again for your help!
I don't think that making the increments smaller will help, though. It looks to me like the slope of the middle line is off, so it is not aligning with the tails. I calculated what the slope between the inner most points of the tails is and I get vm1 = 0.476188. It's still not perfect, but play around with that.
There's a specific equation for vm values that I didn't use the first time, it should be vm1 = 0.464. The final result comes out looking like this: Screenshot_3.png
Nice! Love when the math works!
I must have been writing my last comment the same time you were writing yours. What I said was in reference to decreasing the point interval spacing.
You were right on that front, there was still the weird discontinuity when I decreased the interval. Cheers!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!