![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/584386/image.png)
Data fitting with custom equation
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Somnath Kale
am 15 Apr. 2021
Kommentiert: Somnath Kale
am 27 Jun. 2021
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.
0 Kommentare
Akzeptierte Antwort
William Rose
am 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/584386/image.png)
8 Kommentare
William Rose
am 16 Apr. 2021
Bearbeitet: William Rose
am 16 Apr. 2021
@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.
Weitere Antworten (1)
Somnath Kale
am 17 Apr. 2021
Bearbeitet: Somnath Kale
am 17 Apr. 2021
4 Kommentare
William Rose
am 18 Apr. 2021
@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!
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!