Data fitting with custom equation

Hi
I am facing some issue in the data fitting with the equation. I wanted to fit to my data with eqauation attached in the image. please also suggest what type of fuction we can fit or not fit. my x and y are as fillow.
x = 1.00E-07 2.00E-07 5.00E-07 1.00E-06 2.00E-06 5.00E-06 1.00E-05 2.00E-05 5.00E-05 1.00E-04 2.00E-04 5.00E-04 1.00E-03 2.00E-03 5.00E-03
y = 2.3800 4.4800 6.4500 9.0200 13.2400 16.2600 18.9700 22.9900 26.3300 28.7800 30.3100 31.6500 31.9500 32.0500 32.3400
if possible let me kno the code also.

 Accepted Answer

William Rose
William Rose on 15 Apr 2021
fmincon() is a very good general purpose routine to find the parameters that give a good fit to your data.
To use fmincon(), you need to write a function that returns the error between your data and the predictions from your equation. Usually the error is defined as the sum of the squared errors between the data and the predicitons. You need to pass to fmincon() the name of that function, and an initial guess for the parameters, and some bounds on the parameters (i.e a vector of the maximum acceptable value of each parameter and a vector of the minimum acceptable value for each). Then fmincon() searches parameter space to find the set of parameters that minimize the error and which obey the maximum and minimum constraints.
You already have a function to fit the data (in your image file). It has 3 adjustable parameters: . Use that to fit the data. Do you need another functionto compare? If so, try a straight line.

8 Comments

Your x and y data are identical, so the model that works, and which fits perfecttly, is simply . I wonder if you intended to post a different set of y values.
@William Rose I have edited the x y data value.
Okay I will try to fit with fmincon().
Isn't it possible to fit it with our regular data fitting toolbox?
I think of fmincon() as being a regular tool for dat afitting. It is part of the Optimization Toolbox. I rcommend fmincon() because I hav used it a lot. Maybe the fit() command in the curve-fitting toolbox will do it. I have never used fit().
If you don't want to learn how fmincon() works, and if fit() is not a suitable solution, you could transform your data so that points on the predictor function transform into a straigt line. Then you fit a straight line to the transformed data with the standard least-squares approach, and then you transform the slope and intercept of the best-fit straight line back to parameters of the original model. This approach has a long history in statistics, especially in the past, when computer programs to do nonlinear fits were not as common. It is usualy not recommend now, because if the errors are normally distributed in the raw data, then they won't be after transformation, and if the errors aren't normally distributed, then least squares fitting may not be optimal. Your model aopears to have three parameters, so the two parameters of a staright line are not enough to cover all three. You would have to assume a value for one of the parameters.
The image you proved give this equation:
Do the x-values which you provided correspond to t, and the y-values correpsond to ? What is the "i" in the equation? Do all the x values and all the y values have the same value of i, so that there is one value of to be fitted, and one value of to be fitted? Here's a fit I got:
and this was the display on the console
>> SomnathMain
Local minimum possible. Constraints satisfied.
fmincon stopped because the size of the current step is less than the default value of the step size tolerance and constraints are satisfied to within the default value of the constraint tolerance. <stopping criteria details>
Best fit: Pmax=35.769, alpha=0.532, tau=7.18e-06
>>
@Somnath KaleThe code for the fit above is attached.
The provided x-values correspond to t, and the y-values correpsond to detlaP(t). Here is the index for different pupose as you pointed out its unique for the one set of x y values as well. thanks for the detailed reply.
I tried to run the attached code file but it has shown one error
>> SomnathMain
Unrecognized function or variable
'getIpOptions'.
Error in fmincon (line 859)
options =
getIpOptions(options,sizes.nVar,mEq,flags.constr,defaultopt,10,0.01);
Error in SomnathMain (line 4)
p=fmincon(@SomnathsFunc,p0,[],[],[],[],[],[]);
is there some issue in function argument declaration?
Somnath, I will look at it later today.
@Somnath Kale, I do not get an error when I run it. I am running Matlab 2018b. I see the following output in the console window:
>> SomnathMain
Local minimum possible. Constraints satisfied.
fmincon stopped because the size of the current step is less than
the default value of the step size tolerance and constraints are
satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
Best fit: Pmax=35.769, alpha=0.532, tau=7.18e-06
>>
Try changing line 4 in SomnathMain.m to
p=fmincon(@SomnathsFunc,p0);
That works equally well for me. Or replace line 4 with the following two lines. Note that there are seven [ ] in the second line. This also works fine for me, and gives the same results as above.
options=optimset;
p=fmincon(@SomnathsFunc,p0,[],[],[],[],[],[],[],options);
Try it.

Sign in to comment.

More Answers (1)

Somnath Kale
Somnath Kale on 17 Apr 2021
Edited: Somnath Kale on 17 Apr 2021
I tried both the suggetions, in R2018b, R2020b and R2021a version showing same error. I checked in the community also several peoples faced this issue "Not enough input arguments". Is it because of some installation issue? here I have attached screenshot with all suggestion and respective errors. please take a look if this helps to juge where it going wrong or I am making some mistake!

4 Comments

None of your versions of Matlab are able to find the function called GetIpOptions(), which is called by fmincon(). Maybe the folder containing this function has not been added to the Matlab path. (Although if that is true, then I would expect you to get other "function not found" errors.) On my Windows computer, the file getIpOptions.m is in folder
C:\Program Files\MATLAB\R2018b\toolbox\optim\optim\
Look for that file on your computer. When you find it, add the folder it is in to the Matlab path. See here for how to add a directory to the Matlab path.
If that doesn't work, contact your sysadmin, or contact Matlab Support here, and proceed as if you have an installation issue (which may be true). That way you should get help even if you are a student.
Thanks @William Rose it worked !
Thank u so much!
I am mainly working in the instrument controlling in Matlab. I was new to this fitting business. You not only helped me but also make me familar to the lots of new things. I loved your approch of explananing the thing from base to top for new comers. Thank you very much. Looking forword to receving help in future if needed!
(I can also help for any problem regrading Instrument control)
@Somnath Kale, You're welcome. Thank you very much for your kind comments. If I have problems with instrument control, I will be sure to conatct you!
@William Rose I am facing issue in fitting on complicated fuction. i have posted new question.
please take look and help if possible!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!