Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Find the best fit with an exponential curve (or spherical)
Replies: 1   Last Post: Jun 30, 2011 1:26 PM

 Messages: [ Previous | Next ]
 Jan-Erik Posts: 5 Registered: 6/28/11
Find the best fit with an exponential curve (or spherical)
Posted: Jun 28, 2011 6:57 PM

Hello,
Given vectors of data xdata and ydata, I want to find coefficients to find the best fit to some kind of exponential decay equation.
Please have a look at my data. I posted it as two vectors at the end of this message, which you can copy and paste into MATLAB. The ydata vs. xdata plot clearly shows a exponential decay.
I used the command lsqcurvefit with the function
F(p,xdata) = p(1)*exp(p(2)*(xdata+p(3)))

However it fails and I don't know what is wrong woth it. It might be the function or any settings. Its the first time that I use this kind of approach. I only used the command "polyfit" before, but in this case its clearly not a polynomial that I can use to fit the data.
I am also wondering if a spherical fit might work better. But I don't know how to set up a function for that kind of fitting.

I pasted the function and my code that I used below:

% Function:
function F = myfun(p,xdata)
F = p(1)*exp(p(2)*(xdata+p(3)));
end

% Code starts:
p0 = [300; -1; 100]; % Starting guess
[p,resnorm] = lsqcurvefit(@myfun,p0,xdata,ydata);

% Code ends

Thank you in advance for any help.

Data that you can copy into you workspace:

xdata = [5.2328591;
116.11424;
4.0898868;
219.47837;
179.47784;
170.82645;
94.749072;
27.508751;
33.17609;
8.7674245;
246.7401;
212.71554;
58.302878;
110.72984;
244.76086;
192.0557;
172.52603;
166.52404;
218.4356;
77.807433;
16.977611;
72.411915;
93.242017;
57.726031;
36.703077;
89.928049;
34.833701;
30.248739;
25.206788;
39.45225;
32.892571;
319.77661;
200.03084;
421.92151;
483.99245;
25.582254;
50.288599;
75.984644;
46.22965;
60.332806;
11.520648;
409.86849;
19.171935;
290.7248;
20.994762;
59.460544;
35.998656;
85.461232;
35.40774;
66.012621;
319.826;
319.826;
64.655907;
459.3124;
76.080513;
105.23375;
314.91528;
88.421214;
18.357163;
361.566;
206.8902;
77.807433;
118.89685;
40.941978;
96.924023;
219.32908;
76.544708;
62.843373;
149.37665;
132.55196;
79.111732;
32.713623;
264.88224];

ydata = [126.984126984127;
2.68921624286611;
164.41689548018;
7.17000000000007;
4.20000000168;
4;
12.0481927710843;
7.93650793650798;
10;
230.769230769231;
3.40136054421769;
14.8148148148148;
4.739336492891;
6.74157303370787;
25;
10;
9.43396226415095;
2.66666666666667;
7.61904761904762;
19.7863078749505;
96.511122906915;
74.7926963686441;
63.2022480786833;
46.8749999999983;
240.15009380863;
25;
66.6666666666669;
35.7142857142857;
112.119878574172;
35.5392156862819;
61.3496932515337;
13.0252101770708;
6.48148149348422;
2.5;
2.22222222222222;
77.6518092871554;
294.034128229193;
27.7777777777778;
19.8019801980198;
8.33333333333333;
50.7614213197963;
3.98954738584908;
163.157856772133;
9.00900900900901;
50.6047264814534;
22.3164472216023;
48.3619344773791;
54.9915397631134;
215.384615384615;
34.4827586206897;
7.14285714285714;
7.14285714285714;
8.78155872667399;
2.28258388495777;
11.6959064327486;
12.987012987013;
4.34782608695652;
2.32558139534884;
75.1455945895173;
10;
7.14285714285714;
30.3030303030303;
4.59193044756015;
8.16326530612245;
28.5714285714286;
10.3896103896104;
7.49860000000003;
3.69685767097967;
5;
3.65853658536585;
6.89655172413793;
74.2942050520058;
9.61538461538461];

Date Subject Author
6/28/11 Jan-Erik
6/30/11 Tom Lane