MATLAB Answers

MATLAB time (seconds) vs velocity graph plotting questions

15 views (last 30 days)
Blake Sewell
Blake Sewell on 1 Sep 2020
Commented: Jon on 2 Sep 2020
Hi guys, I'm enquiring about a section of my homework again and its asking that I use strictly array creation and concatenation to plot the graph of the following question:
and the code I've written to try and solve this via youtube tutorials and lots of websites is:
%time interval of 0.1 seconds
dt=0.1;
%time establishment in intervals of 0.1 seconds
t1=0:dt:10;
t2=10:dt:15;
t3=15:dt:20;
%acceleration establishment
a1=ones(1,length(t1))*10;
a2=ones(10,length(t2))*15;
a3=ones(15,length(t3))*20;
axis([0 20 4 12])
hold on
plot([10 10],[5,10])
hold off
v1=cumtrapz(a1)*dt;
v2=cumtrapz(a2)*dt;
v3=cumtrapz(a3)*dt;
plot(t1,v1,t2,v2,t3,v3)
and the result i get is:
can anyone please tell me where im going wrong and what I need to do to fix, any help is appreciated, thankyou in advance <3

  0 Comments

Sign in to comment.

Accepted Answer

Jon
Jon on 1 Sep 2020
Edited: Jon on 1 Sep 2020
Hi
First, I am glad that although you are asking for help on homework you have made a good attempt on your own and are now just struggling with some MATLAB issues. That's a good point to come to MATLAB answers for help.
You have a few problems.
First you want all of your accelerations to be 1D vectors. You are assigning some of them to be matrices for example:
a2=ones(10,length(t2))*15;
creates a 10 row by length(t2) number of columns matrix. You want that first argument to just be 1 so that you make a row vector. (or you could use ones(length(t2),1) and make it a column vector, but you'd have to be consistent through out so the time vectors should then also be columns)
Next your accelerations magnitudes are not correct. Of course acceleration is change in velocity per unit time. So the first segment the acceleration is 10m/s/(10s) = 1m/s^2, The second segement it is zero, etc
Finally when you create the overall solution you have to consider the initial condition for the velocity. The way you are doing it you are calculating it as if you are starting at zero velocity for each segment. Of course the start velocity of the second segment is the end velocity of the first segment, etc.
So you could use something like
v2 = v1(end) + cumtrapz(a2)*dt
.and something similar for the final segment.
Hope this helps.
Note to format your code nicely you can use the code button in the MATLAB answers toolbar.

  3 Comments

Blake Sewell
Blake Sewell on 1 Sep 2020
Thanks heaps ill be sure to give it a whirl when iv woken up abit more
Blake Sewell
Blake Sewell on 1 Sep 2020
It works !!! thankyou very much for your help I appreciate the in deoth run-down of what needed to be changed <3

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!