Create a function out of two vectors
Show older comments
Hello!
I have two data files containing two vectors v and c (both 1x100 double) - where v represents the speed of a moving car (km/h) and c represents the consumption of the energy (Wh/km)
What I want to do is create a (continuous) function out of these vectors in order to be able to find the consumption of the energy by plugging in any given velocity within the interval. This should obviously also be applied for velocities "inbetween" the original set of data.
So I basically I want to make a function c(v) where c is the consumption and v is the velocity.
I have tried to tackle this issue using polyfit and also by manually constructing an equation that gives a similar graph - though I am not sure if this is the appropriate method as I am positive that there has to be some easier way to proceed with this kind of operation.
Thanks in advance!
Answers (1)
Image Analyst
on 10 Dec 2017
2 votes
I'd probably use spline() since that will go through all your training points whereas polyfit won't. See attached demo for spline(). You can plug in any value you want to the spline and get out the fitted value.

In the above picture, the red line shows the training points, and the blue line shows a bunch of points where it estimates the fit using spline interpolation.
3 Comments
Selina Smith
on 10 Dec 2017
Image Analyst
on 10 Dec 2017
I'm leaving for the night soon, but if you post your two vectors and tell me at what points you want their value, I'll see what I can do. You might be able to use roots() if your data exactly fits a polynomial, but not if it's noisy.
Image Analyst
on 11 Dec 2017
Try again - your c only has 57 elements, not 100.
Categories
Find more on Spline Postprocessing 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!