MATLAB Answers

Make a curve fit for function for 2D array

31 views (last 30 days)
V
V on 22 Jun 2020
Answered: V on 1 Jul 2020
I have around 200 value set for equation:
z = function(x,y)
Have 200 set values for which I know values of x,y & z.
Do matlab have some option to make equation/curve fit for it?
If yes, which matlab component/tools required during installation?

Answers (2)

John D'Errico
John D'Errico on 22 Jun 2020
There are infinitely many possible functions that could be used to fit any possible set of data. Some will fit better than others. In fact, there are infinitely many possible functions that will pass EXACTLY through every point. All of these possible functions would do any of infinitely many arbitrary things between the points. So knowing which one is the one you would want is impossible.
When performing this task, you need to choose a model for the surface that makes sense to you, in context of what the data means, and what you will do with it in in the end. So you might choose some sort of interpolating spline, that will "smoothly" in some sense interpolate the data. However, that spline will have no simple form you can write down. You can evaluate it and plot it, use it to predict the value at any point. But if you want to let someone look at an marvel at the coefficients, don't bother. It won't make any sense to you anyway, except that you can use it.
Alternatively, you might choose some general family of models. Perhaps polynomials, sine & cosine series, Chebychev polynomials, Bessel functions, whatever floats your boat. Some of those choices may be easier to implement than others depending on the form of your data. For example, it becomes easy enough to use sines and cosines if you have a 2-d gridded array of data, because then you just throw it into an fft. You then have a great deal of theory and tools to help you to work with an fft, as long as you understand what you are doing. You can truncate the fft, throwing away the high frequency components, etc. However, it sounds as if an fft would not work for you, since your data sounds as if it is scattered. There are tools to fit polynomial models to scattered data (for example, my polyfitn, found on the file exchange) or you can use the curve fitting toolbox. It helps of course if you understand even polynomial modeling, because far too many people seem to overfit their polynomial models.
Or you can use tools to fit a gridded surface to your data, thus my gridfit, also found on the file exchange. Again, you won't get a function you can write down, just a surface that can be plotted and interpolated as you might wish.
Finally, I would point out that people who are new to curve and surface modeling often seem to fall into some simple traps, either overfitting their data, or trying to fit a model that is wholly inappropriate for their data, or wanting to get a great fit from highly noisy data (which typically redcuces to their overfitting the data), or not understanding that some forms of data are not amenable to fitting a surface at all. For example, if your data forms the surface of a sphere, then it does not represent a single valued function. That invalidates the basic assumptions for almost every surface fitting tool you will find. Other basic traps people fall into may involves data that is noisy, but is not even remotely normally distributed. A basic example is proportional noise.
So really, if you are just learning about these ideas, then you need to spend some time to learn about modeling techniques in general.
Without seeing your data, without a clue as to what you will do with this model when you have it, we (ok, I) cannot realistically offer a recommendation which tool might be right for you. I'd probably start with the curve fitting toolbox, as it offers a great deal of flexibility and models you can employ. And, not to promote my own code since I get nothing from it, but gridfit offers a great deal of value, and it is free.
  2 Comments
John D'Errico
John D'Errico on 26 Jun 2020
I know you did. But I don't do consulting via e-mail. In the end, that would turn me into the guy people just contact directly. Why use Answers, just ask John directly. And that would end up overwhelming me. On Answers, I can freely choose to answer a question or not. If I don't have the time some day, I can not look at Answers, and allow someone else to take a shot at it.
Anyway, what would happen even in this case, if you walked into my long time fully retired consulting office with this question?
What you really need is a consultant. Someone who will sit down with you. Figure out what problem you are trying to answer. Someone who will prompt you to tell them what you need in the end, and how you will be using it.
Because even in the e-mail you sent me, I was confused as to exactly what you were doing. What you needed to compute, because what you asked for was not anything I saw in the spreadsheet of data you posted. How is thermistor temperature related to thermopyle temperature? I might guess the answer to that.
But how will you be using this? And how is that even related to your question? You sent an array with 1071 pieces of information, not the 200 you indicated in your question here. So that becomes a matter of confusion.
Again, how are the various temperatures related to each other? What do the axes in the spreadsheet mean in context of your question? How will you be using this function you want to create? Do you want some pretty formula you can write down, something with nice coefficients people can marvel at in a paper you will write? Do you want to create a pretty plot? Will you be performing an optimization or solving a differential equation, using this function repeatedly for possibly thousands or even millions of calls? Is it just a one time thing, where you have a pair of voltages, and you merely want to interpolate the grid you showed?
As your consultant, these are the questions I would prompt you to answer. That is because my response would depend on how you answered the questions I just posed.
Odds are, you would probably be sufficiently well served were I to teach you about interp2, and how it can be used. Or possibly you need the function griddedInterpolant. At this point, these would just be guesses on my part. Either might suffice, or I might be heading in the wrong direction, on a wild guess chase.
But I won't do consulting via e-mail.

Sign in to comment.


V
V on 1 Jul 2020
Hi john,
I had sent on ur email,since i didnt want to share data here. That was the only reason. Not asking you to reply via PM or email.
I have been to develop a third order equation for 1071 data points. which fits very well with +-0.1 error, which is good for my project. Developed by third party software though.
But as soon as i moved to entire range of sensor which has around 40000 points data doesnot fit any more, even I try to break data into 10 parts for better fit.
Need to make a function Z=Fun(x,y)
Attached is notepad file,
  1. Column 1 is Z
  2. Column 2 is x
  3. Column 3 is y

Tags

Community Treasure Hunt

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

Start Hunting!