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!

답변 (1개)

Image Analyst
Image Analyst 2017년 12월 10일

2 개 추천

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

Selina Smith
Selina Smith 2017년 12월 10일
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?
Image Analyst
Image Analyst 2017년 12월 10일
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
Image Analyst 2017년 12월 11일
Try again - your c only has 57 elements, not 100.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

제품

질문:

2017년 12월 10일

댓글:

2017년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by