Average Position vs Time Graph?
Show older comments
I need to make a position vs time graph with data collected from a subject during walking. I have two variables, 'position' and 'time.' They are both matrices, with the position/time data from each step in its own column (position(x,y) corisponds to time(x,y)). The steps very in duration and the number of postion measurements. I normalized the time so that all steps are the same length. Right now I can produce ~30 lines showing each individual step. I need to average the data and create one line representing all the steps. Whats the best way to do this? I was told to use the spline function to create the graph but having trouble. Thanks ~Dan
2 Comments
Daniel
on 13 Jun 2011
Mitch Nyandoro
on 7 May 2018
Hi Daniel, i hope you managed to solve this problem i am currently working on something similar please assist with code for the position vs time graph if you do not mind
Accepted Answer
More Answers (1)
Laura Proctor
on 13 Jun 2011
If you know the model that you would like to use, and it is one equation for all of the data, then linear regression would be the way to go.
However, if you simply wish to fit a line or a polynomial, it makes it even easier; you can use the polyfit function. So, for example, say that you have t and x data that you would like to fit with a quadratic, simply type in the command
c = polyfit(t,x,2);
and you will have the coefficients of the best-fit polynomial returned in c. If you want to take it further and plot that polynomial, then use the polyval function like this:
plot(t,polyval(c,t))
8 Comments
Daniel
on 13 Jun 2011
Walter Roberson
on 13 Jun 2011
Does the line "curve back on itself" ? If so then polyfit() is inherently unsuitable. polyfit() also tends to give bad fits towards the extreme of the data (especially the larger values of x).
Daniel
on 13 Jun 2011
Daniel
on 13 Jun 2011
Walter Roberson
on 13 Jun 2011
polyfit() is unlikely to be able to give you those kinds of curves unless you had a much finer sampling interval and a much higher order polynomial (higher order ==> more numeric problems.)
Laura Proctor
on 13 Jun 2011
For that graph, POLYFIT will not work, as Walter indicated. If you want a smooth line and you do not need a corresponding equation to the line, then create a variable tt that is more finely meshed than t, and plug it into the SPLINE function to return the corresponding x values:
tt = linspace(t(1),t(end),1e3);
xx = spline(t,x,tt);
Daniel
on 13 Jun 2011
Daniel
on 14 Jun 2011
Categories
Find more on Splines 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!