Fitting data with a prescribed function

Hello,
My question is about the fitting function. I would like to fit 5 data points to a prescribed function: A + B*x + C * log(x). The code I use is:
myfit = fittype('a + b*x + c*log(x)','dependent',{'y'},'independent',{'x'},'coefficients',{'a','b','c'});
fit1=fit(xfit,yfit,myfit)
The data points I use are:
xfit = 2.1902, 2.5940, 3.0203, 3.5356, 4.2762,
yfit = 0.5000, 1.0000, 2.0000, 4.0000, 9.0000
Now, I do get an answer, but when comparing it to the values for a,b and c I found using both Python or Wolfram Mathematica, it seems very off.
Any idea what went wrong?
The expected values are: 2.5303 + 0.0627895 x + 0.538228 Log[x]
EDIT:
clear all
close all
color='red';
xfit = [2.1902, 2.5940, 3.0203, 3.5356, 4.2762];
yfit = [0.5000, 1.0000, 2.0000, 4.0000, 9.0000];
scatter(xfit,yfit,color)
hold on
myfit = fittype('a + b*x + c*log(x)','dependent',{'y'},'independent',{'x'},'coefficients',{'a','b','c'});
fit1 = fit(xfit',yfit',myfit,'StartPoint',[2.5,0,0.5]);
plot(fit1,color)
ylim([0,10])
xlim([0,5])
This is a matlab file that should do the job. However, it still doesn't work. Adding the point [0,0] to the beginning of the list does not make any difference (it results in errors, due to log(0) = -Inf ).

 采纳的回答

Hi,
have you verfied the other results you are getting? What kind of LOG are you using in Python or Mathematica? In MATLAB LOG is the Natural logarithm.
When verfiying the given curve it looks very wrong:
f = @(x) 2.5303 + 0.0627895.*x + 0.538228.*log(x)
xfit = [2.1902, 2.5940, 3.0203, 3.5356, 4.2762];
yfit = [0.5000, 1.0000, 2.0000, 4.0000, 9.0000];
scatter(xfit,yfit,'red')
hold on
plot(0:0.1:5,f(0:0.1:5))
ylim([0,10])
xlim([0,5])

4 个评论

Hey, thanks for the reply
I can imagine it being very wrong when showing it that way. I have used the y-axis to be on the horizontal axis. Than you would get a pretty decent match, as you can see in the figure below. The dots are shown here, but they are really small.
EDIT:
So If we would change the plot code in the following line, it does show the correct graph:
f = @(x) 2.5303 + 0.0627895.*x + 0.538228.*log(x)
xfit = [2.1902, 2.5940, 3.0203, 3.5356, 4.2762];
yfit = [0.5000, 1.0000, 2.0000, 4.0000, 9.0000];
scatter(xfit,yfit,'red')
hold on
plot(f(0:0.1:5),0:0.1:5)
ylim([0,10])
xlim([0,5])
What you do is mathematically wrong. You cannot simply swap X-Y axis for the function plot and keep the xfit and yfit data the same. If you swap X-Y axis then do it for all of it. So also change the way you call scatter.
You can get the fit by changing the call to fit in MATLAB to:
fit1 = fit(yfit',xfit',myfit,'StartPoint',[2.5,0,0.5]);
However that doesn't make much sense from a mathematical point of view.
I think we went wrong on the formulation, here.
In Mathematica, where I found the a, b and c value for the formula, I used the values of the Y-axis to be on the horizontal axis and the values of the X-axis to be on the vertical axis.
Could this solve our problem in matlab as well?
Okay, we did went wrong on the formulation of X and Y axis. When we now adjust the scatter function (scatter(yfit,xfit,'red') ), and we continue on the way we did before, we get a perfect match. Thanks for helping!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by