Multi-dimensional Fitting

85 views (last 30 days)
Muhammet Dabak
Muhammet Dabak on 25 Dec 2019
Commented: Muhammet Dabak on 27 Dec 2019
I have 3 parameters for my function. Let's say f(x,y,z)=(a*x+b)*exp(-y/c)*(z^2+d) (Or i have n parameters).
I constructed the custom function because I know the behavior of the function (that I desire).
I have many samples(around 5000). For example f(1000,10,2)= 35;
Is there a method to fit these samples into a shape,solid (for 3 parameters case) ?
Or is there a method that to find the coefficients (a,b,c,d in this case) for my custom function using all my samples?
The answer doesnt have to be a specific for 3 parameter case, i need actually a solution for n parameters case.
(I know Matlab has curve fitting and surface fitting tools but no more dimensions.)
Any support will help me, thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Dec 2019
You cannot use cftool it supports at most two independent variables and on dependent variable. You can, however, use your custom equation with fit() from the Curve Fitting Toolbox, or you could use nonlinear least squares https://www.mathworks.com/help/optim/nonlinear-least-squares-curve-fitting.html
fit() is often surprisingly efficient at what it does, and often generates coefficients that are close enough to optimal to be "good enough" for practical purposes.
However, in equations that have two or more major basins of attraction, fit() will typically go with the larger basin even when the smaller basin has a significantly better fit. Adding upper and lower bounds on the "reasonable" values of parameters can help a lot.
I would predict that the in your sample equation, the c value would tend to vary a lot. You do not have have an additive constant so the fitter would tend try to raise or lower the overall height by driving c large, but large values of c lead to small -y/c leading to near 1 value of exp() and relatively large changes in c have small effects, making it difficult to locate the best c value. This is a common problem for equations with exp() and a multiplicative coefficient inside.
  5 Comments
Walter Roberson
Walter Roberson on 27 Dec 2019
If the first term cannot be have a negative coefficient then put in bounds on the values.
Muhammet Dabak
Muhammet Dabak on 27 Dec 2019
With the way that you show in lsqcurvefit for each variable i created a vector than I gather them and it worked.
xyz = [x.', y.', z.'];
my final equation like this:
(c1*x+c2).*exp(-y/500).*(1-c3./(c4+exp(-15*(z-0.5))))
lsqcurvefit gave me
c1=13.5195
c2=162.8643
c3=7.8244e-09
c4=1.0062e-08
These values solve my problem perfectly.
Additionally, I set lower band [0;0;0;0] to avoid possible errors.
Now, I have a function that includes linearity, exponentiality and sigmoid property.
Thank you for everything.
See you later.

Sign in to comment.

More Answers (2)

Muhammet Dabak
Muhammet Dabak on 25 Dec 2019
Edited: Walter Roberson on 26 Dec 2019
You said I cannot use cftool because it allows 2 variables most, that is ok. But, 'use your custom equation with fit() from the Curve Fitting Toolbox' , here is the fit definition in Matlab: Fit a curve or surface to data. Means no variables more than 2.
lsqcurvefit and lsqnonlin can be used if I have just x as a variable(1 parameter).
I couldn't get what you mean by these.
And my exact function is
f(x,y,z)=(a*x+b)*exp(-y/500)*(1-c/(d+exp(-z/e))); %[ 3 unknowns,5 coefficients]
I can set some coefficients by trying , that is not the deal actually(thanks for consideration in the sample equation).
I need a way to find coefficients for at least 3 unknowns.
If you see a way to do it just give me a very simple example so I could understand you.(3unknowns,3 coefficients
for ex. f(x,y,z)=(x+a)(y+b)(z+c);
Thanks anyway.
//Edit//
Note: I tried 'polyfitn' and 'regress' functions but they supply just polynomial and linear expressions ,however, i have exponential terms. One can use these functions if your function does not contain exponential expressions. 'polyfitn' produces n variables-m degree expression for you. But you have to download it.
// This note is written for people who has similar issues about this topic.//
  1 Comment
Walter Roberson
Walter Roberson on 26 Dec 2019
You are right, fit() cannot be used for 3 or more independent variables. You will need to use a nonlinear least squares such as https://www.mathworks.com/help/optim/ug/lsqcurvefit.html .
Unfortunately with the random data I generated, lsqcurvefit did not do a good job. I am experimenting further.

Sign in to comment.


Muhammet Dabak
Muhammet Dabak on 26 Dec 2019
With 'polyfitn' function describing a function as a summation of all variables in the degree that you choose.
For example:
p=polyfitn([x,y,z],t,'constant x^2 y^3 z z.*x')
formula=polyn2sym(p);
Which gives
formula= c1*x^2 + c2*y^3 + c3*z + c4*z*x + c5
Which is very useful if you can describe your function as a summation and if it just includes polynomial and linear components. If I cannot find a way to build a custom equation for my case, I will try to convert my function to eliminate exponential terms and try again.
Thank you.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!