Logarithmic Curve Fitting without use of functions

8 views (last 30 days)
I have the logarithmic fit equation f(fit) = log(a1 + a2*x) and need to apply it to some data without using the fit function. So far I have come up with:
phi =[ones(size(xx)),(xx)];
aa=phi\exp(yy);
yfit =log(phi*aa);
plot(xx, yy, '', xx, yfit, 'r-')
But it does not come out with the correct fit when plotted. Can anyone see where I am going wrong?
Thanks
  2 Comments
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 11 Mar 2014
Is it like you have a set of data and you want to find a1 and a2 as such that f(x)=log(a1+a2*x) fit the set of data?! If so? then what's the data!
Josh
Josh on 11 Mar 2014
Edited: Josh on 11 Mar 2014
That's exactly it. The data is
3.4051
4.5940
7.9954
10.7201
14.6408
18.3000
21.4829
24.9677
29.1671
35.9833
43.9310
48.6303
53.5545
58.0928
66.1397
76.8433
80.6221
93.3233
102.1019
112.4451
121.9873
132.9979
144.1910
154.0537
165.6450
which I have set as the variable yy, and xx are their positions along the x axis
52
136
219
300
382
463
544
630
779
862
945
1026
1107
1188
1344
1425
1506
1664
1745
1903
1984
2066
2223
2304
2386

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 11 Mar 2014
It took some experimenting to understand what you are doing and what your data were and how they were generated. You didn’t post the results were that you were getting (the aa coefficients you were calculating were -62E+69 and 83E+66), but assuming I got your xx and yy vectors correct, see if this code produces the result you want:
xx = [52 136 219 300 382 463 544 630 ... 2386];
yy = [3.4051 4.5940 7.9954 10.7201 14.6408 18.3000 21.4829 24.9677 ... 165.6450];
xx = xx';
yy = yy';
phi = [ones(size(xx)),(xx)];
aa = phi\log(yy);
yfit = exp(phi*aa);
figure(1)
plot(xx,yy,'.b', xx,yfit,'r-')
grid
(Row vectors take up fewer lines in the Editor.)
This produces aa coefficients of 2 and 1.5E-3, and an acceptable fit.
Your theory seems correct to me, but your data don’t appear to match your model.
When all else fails, experiment! Consider:
yy2 = log(3 + 5*xx);
aa2 = phi\exp(yy2);
yfit2 = log(phi*aa2);
figure(2)
plot(xx,yy2,'.b', xx,yfit2,'-r')
grid
That works as you intended it to.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!