COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### plot and curve fit

• Follow

```I have a formula I am interested in plotting and fitting to a curve. I am interested in modelling a simple peak and fit to the following formula; c*x^a * exp(-x/b)    where x is the independent variable. a,b,c are initilized to 1.8, 2, and 10.
I am using the fittype and fit functions in Matlab and am having troulble it seems with the fit not properly finishing and going to inf.(Inf computed by model function.) .. and I am interested in knowing a way to avoid this as it works for some datasets, but not all.

I am fairly new to Matlab so if there is an easy way to avoid this I maybe unaware at this point???

```
 0
Reply John 1/3/2011 10:00:23 PM

```
"John " <sangria13@hotmail.com> wrote in message
news:iftgtn\$6la\$1@fred.mathworks.com...
> I have a formula I am interested in plotting and fitting to a curve. I am
> interested in modelling a simple peak and fit to the following formula;
> c*x^a * exp(-x/b)    where x is the independent variable. a,b,c are
> initilized to 1.8, 2, and 10.
> I am using the fittype and fit functions in Matlab and am having troulble
> it seems with the fit not properly finishing and going to inf.(Inf
> computed by model function.) .. and I am interested in knowing a way to
> avoid this as it works for some datasets, but not all.
>
> I am fairly new to Matlab so if there is an easy way to avoid this I maybe
> unaware at this point???

Show a small sample of your data and the code that you're using to try to
perform this fitting.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

```
 0
Reply Steven_Lord 1/3/2011 10:07:18 PM

```Sure,
Here is a sample code and data..

plot(duptimes,dupdensity,'o');
hold on
ft = fittype('c*x^a*exp(-x/b)','independent','x');

[fit2 gof1] = fit(duptimes,dupdensity,ft,'StartPoint',[1.8 2 10],'Algorithm','Levenberg-Marquardt');
plot(fit2)

arrays...
duptimes Array-------------------------------------------------------------------------
7.1510
8.0920
9.0360
9.9430
10.831
11.692
12.544
13.389
14.243
15.089
15.940
16.784
17.645
18.484
19.348
20.205
21.054
21.901
22.756
23.610
24.455
25.302
26.163
27.021
27.885
28.733
29.601
30.468
31.333
32.196
33.064
33.839
34.706
35.542
36.336
37.088
37.917
38.764
39.533
40.342
41.125
41.942
42.758
43.591
44.429
45.278
46.144
47.015
47.876
48.739
49.607
50.477
51.345
52.211
53.086
53.954
54.829
55.694
56.571
57.452
58.344
59.218
60.095
60.980
61.857
62.730
dupdensity Array
137.31
138.79
175.01
254.54
347.28
434.09
525.59
536.31
450.00
428.00
376.02
333.92
309.16
284.37
267.06
236.34
238.45
225.00
222.42
241.08
222.24
232.02
233.84
228.61
248.34
228.27
244.41
230.92
229.29
233.16
221.24
225.57
219.03
228.24
217.80
207.05
213.29
226.67
208.53
208.60
215.48
202.46
207.82
197.14
215.61
206.45
203.23
212.45
194.58
209.59
206.50
198.31
208.71
201.19
192.67
201.32
197.76
192.81
206.38
197.32
194.87
201.58
196.15
193.11
201.98
194.62

the end----------------------------------------------------------------------------------------------

"Steven_Lord" <slord@mathworks.com> wrote in message <iftham\$3a2\$1@fred.mathworks.com>...
>
>
> "John " <sangria13@hotmail.com> wrote in message
> news:iftgtn\$6la\$1@fred.mathworks.com...
> > I have a formula I am interested in plotting and fitting to a curve. I am
> > interested in modelling a simple peak and fit to the following formula;
> > c*x^a * exp(-x/b)    where x is the independent variable. a,b,c are
> > initilized to 1.8, 2, and 10.
> > I am using the fittype and fit functions in Matlab and am having troulble
> > it seems with the fit not properly finishing and going to inf.(Inf
> > computed by model function.) .. and I am interested in knowing a way to
> > avoid this as it works for some datasets, but not all.
> >
> > I am fairly new to Matlab so if there is an easy way to avoid this I maybe
> > unaware at this point???
>
> Show a small sample of your data and the code that you're using to try to
> perform this fitting.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
```
 0
Reply John 1/3/2011 10:33:05 PM

```I should also state that the first value for each of these arrays is calculated to be zero by subtracting the 1st element from all elements of the array, sorry. I gave uncorrected data but that is what I wold do firstly.

"Steven_Lord" <slord@mathworks.com> wrote in message <iftham\$3a2\$1@fred.mathworks.com>...
>
>
> "John " <sangria13@hotmail.com> wrote in message
> news:iftgtn\$6la\$1@fred.mathworks.com...
> > I have a formula I am interested in plotting and fitting to a curve. I am
> > interested in modelling a simple peak and fit to the following formula;
> > c*x^a * exp(-x/b)    where x is the independent variable. a,b,c are
> > initilized to 1.8, 2, and 10.
> > I am using the fittype and fit functions in Matlab and am having troulble
> > it seems with the fit not properly finishing and going to inf.(Inf
> > computed by model function.) .. and I am interested in knowing a way to
> > avoid this as it works for some datasets, but not all.
> >
> > I am fairly new to Matlab so if there is an easy way to avoid this I maybe
> > unaware at this point???
>
> Show a small sample of your data and the code that you're using to try to
> perform this fitting.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
```
 0
Reply John 1/3/2011 10:38:07 PM

```"John " <sangria13@hotmail.com> wrote in message <iftj4f\$3bq\$1@fred.mathworks.com>...
> I should also state that the first value for each of these arrays is calculated to be zero by subtracting the 1st element from all elements of the array, sorry. I gave uncorrected data but that is what I wold do firstly.
>
> "Steven_Lord" <slord@mathworks.com> wrote in message <iftham\$3a2\$1@fred.mathworks.com>...
> >
> >
> > "John " <sangria13@hotmail.com> wrote in message
> > news:iftgtn\$6la\$1@fred.mathworks.com...
> > > I have a formula I am interested in plotting and fitting to a curve. I am
> > > interested in modelling a simple peak and fit to the following formula;
> > > c*x^a * exp(-x/b)    where x is the independent variable. a,b,c are
> > > initilized to 1.8, 2, and 10.
> > > I am using the fittype and fit functions in Matlab and am having troulble
> > > it seems with the fit not properly finishing and going to inf.(Inf
> > > computed by model function.) .. and I am interested in knowing a way to
> > > avoid this as it works for some datasets, but not all.
> > >
> > > I am fairly new to Matlab so if there is an easy way to avoid this I maybe
> > > unaware at this point???
> >
> > Show a small sample of your data and the code that you're using to try to
> > perform this fitting.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com

it does not appear that your suggested curve fits the data very well.I have come up with this:
f = @(v,x)v(3).*x.^v(1).*exp(-x./v(2));
v0 = [1.8 2 10];
[fitEqn,n,r] = lsqcurvefit(f,v0,t,den);

g =  f(fitEqn,t);
subplot 211
plot(t,den,'o')
hold on
plot(t,g,'g:')
hold off
subplot 212
plot(t,r)

where  t is your duptimes and den is your dupdensity array given in previous posts.  Are you sure the fitting function is correct?  can you change the format of the fitting function?
```
 0
Reply proecsm 1/4/2011 12:16:06 AM

```"proecsm" wrote in message <iftos6\$cln\$1@fred.mathworks.com>...
> "John " <sangria13@hotmail.com> wrote in message <iftj4f\$3bq\$1@fred.mathworks.com>...
> > I should also state that the first value for each of these arrays is calculated to be zero by subtracting the 1st element from all elements of the array, sorry. I gave uncorrected data but that is what I wold do firstly.
> >
> > "Steven_Lord" <slord@mathworks.com> wrote in message <iftham\$3a2\$1@fred.mathworks.com>...
> > >
> > >
> > > "John " <sangria13@hotmail.com> wrote in message
> > > news:iftgtn\$6la\$1@fred.mathworks.com...
> > > > I have a formula I am interested in plotting and fitting to a curve. I am
> > > > interested in modelling a simple peak and fit to the following formula;
> > > > c*x^a * exp(-x/b)    where x is the independent variable. a,b,c are
> > > > initilized to 1.8, 2, and 10.
> > > > I am using the fittype and fit functions in Matlab and am having troulble
> > > > it seems with the fit not properly finishing and going to inf.(Inf
> > > > computed by model function.) .. and I am interested in knowing a way to
> > > > avoid this as it works for some datasets, but not all.
> > > >
> > > > I am fairly new to Matlab so if there is an easy way to avoid this I maybe
> > > > unaware at this point???
> > >
> > > Show a small sample of your data and the code that you're using to try to
> > > perform this fitting.
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > > comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> > > To contact Technical Support use the Contact Us link on
> > > http://www.mathworks.com
>
> it does not appear that your suggested curve fits the data very well.I have come up with this:
> f = @(v,x)v(3).*x.^v(1).*exp(-x./v(2));
> v0 = [1.8 2 10];
> [fitEqn,n,r] = lsqcurvefit(f,v0,t,den);
>
> g =  f(fitEqn,t);
> subplot 211
> plot(t,den,'o')
> hold on
> plot(t,g,'g:')
> hold off
> subplot 212
> plot(t,r)
>
> where  t is your duptimes and den is your dupdensity array given in previous posts.  Are you sure the fitting function is correct?  can you change the format of the fitting function?

Firstly, this looks great! Yes, the (suppposed to be correct) gamma variate I want here is to just pick out the first large peak which is what it does here nicely.
I then it will be used when I plot a 2 compartment fit to another dataset (same t array though) with the following formula:  c*x^a*d*exp(-x/h) ; (same variables with the added d) which I want to plot in the same way.

I am wondering what the major differences are from what I had earlier and what you have suggested as far as the fit methods???. fit and fittype were nice,  but can you use similar methods with lsqcurve like grabbing the latest variable values(coeffvalues())?
```
 0
Reply John 1/4/2011 1:14:20 AM

```> Firstly, this looks great! Yes, the (suppposed to be correct) gamma variate I want here is to just pick out the first large peak which is what it does here nicely.
>  I then it will be used when I plot a 2 compartment fit to another dataset (same t array though) with the following formula:  c*x^a*d*exp(-x/h) ; (same variables with the added d) which I want to plot in the same way.
>
> I am wondering what the major differences are from what I had earlier and what you have suggested as far as the fit methods???. fit and fittype were nice,  but can you use similar methods with lsqcurve like grabbing the latest variable values(coeffvalues())?

Ok, I am not sure what you are asking.  adding variable d is unlikely to improve your fit.  I will try to post a suggestion later...
As to fit & fittype, I do not have the curve fitting toolbox, so I don't know if you can

" use similar methods with lsqcurve like grabbing the latest variable values(coeffvalues())?"
not sure how to interpret above statement
```
 0
Reply proecsm 1/4/2011 3:16:04 AM

```"proecsm" wrote in message <ifu3dk\$ud\$1@fred.mathworks.com>...
> > Firstly, this looks great! Yes, the (suppposed to be correct) gamma variate I want here is to just pick out the first large peak which is what it does here nicely.
> >  I then it will be used when I plot a 2 compartment fit to another dataset (same t array though) with the following formula:  c*x^a*d*exp(-x/h) ; (same variables with the added d) which I want to plot in the same way.
> >
> > I am wondering what the major differences are from what I had earlier and what you have suggested as far as the fit methods???. fit and fittype were nice,  but can you use similar methods with lsqcurve like grabbing the latest variable values(coeffvalues())?
>
> Ok, I am not sure what you are asking.  adding variable d is unlikely to improve your fit.  I will try to post a suggestion later...
> As to fit & fittype, I do not have the curve fitting toolbox, so I don't know if you can
>
>  " use similar methods with lsqcurve like grabbing the latest variable values(coeffvalues())?"
> not sure how to interpret above statement

here is a refined fit where a constant was added...the first plot is the original data and the the residuals the second is the refined fit...
f = @(v,x)v(3).*x.^v(1).*exp(-(x./v(2)));
v0 = [1.8 2 10];
[fitEqn,n,r] = lsqcurvefit(f,v0,t,den);

g =  f(fitEqn,t);
subplot 211
title('Fit');
plot(t,den,'o',t,g,'g:')
subplot 212
t
plot(t,r)

%% add to fitting function
% constant is v(4)
figure(2)
v1 = [v0 1];
f2 = @(v,x)v(3).*x.^v(1).*exp(-(x./v(2)))+v(4);
[fitEqn2,n2,r2] = lsqcurvefit(f2,v1,t,den);

g2 =  f2(fitEqn2,t);
subplot 211
plot(t,den,'o',t,g2,'g:')
subplot 212
plot(t,r2)

As you can see, the 2nd fit is much better
```
 0
Reply proecsm 1/4/2011 4:26:05 AM

7 Replies
354 Views

(page loaded in 0.111 seconds)

Similiar Articles:

7/20/2012 11:38:07 AM

 Reply: