Create a function out of two vectors

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)

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

Hello! Thanks for your reply. I see what you mean, it surely does give a much smoother plot than polyfit does, however, I am not quite sure how to find the function for the graph plotted. Unfortunately I'm not that familiar with functions in general in Matlab coding, how do I get past that part?
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.
Try again - your c only has 57 elements, not 100.

Sign in to comment.

Categories

Products

Asked:

on 10 Dec 2017

Commented:

on 11 Dec 2017

Community Treasure Hunt

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

Start Hunting!