19 views (last 30 days)

Hello, I was wondering what is wrong with my code below, it states that there is an error in line 2, which is V=p(1)*(p(2)/p(1))^exp(-p(3)*t)

function V =gompertz(p,t) %#ok<*FNDEF>

V=p(1)*(p(2)/p(1))^exp(-p(3)*t);

%p(1)=K, p(2)=initial population and p(3)=r

%define data

years=[1950, 1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019];

pops=[554419273,569909117,582576491,593365877,603052324,612241554,621363234,630677645,640295772,650212734,660408056,670952695,682102655,694339083,708254597,724218968,742414885,762581176,784074709,805985939,827601394,848759710,869485964,889485372,908464198,926240885,942685411,957891272,972205442,986132202,1000089235,1014022212,1027948987,1042431412,1058171976,1075589361,1095014109,1116095476,1137724227,1158357397,1176883674,1192897284,1206711244,1218817055,1230020031,1240920535,1251636186,1261996012,1271982350,12815148321290550765,1299129752,1307352257,1315303521,1323084641,1330776380,1338408647,1345993888,1353569484,1361169419,1368810615,1376497639,1384206401,1391883330,1399453965,140684870,1414049351,1421021791,1427647786,1433783686];

p0 = [.01 1000 554419273];

options=optimset('MaxFunEvals',10000,'MaxIter',5000);

[p,error]=lsqcurvefit(@gompertz,p0,years,pops,[],[],options); %#ok<ASGLU>

%plot solution

modelpops = gompertz(pops,years);

plot(x,y,'o',x,modelpops);

end

Walter Roberson
on 26 Oct 2020

V=p(1)*(p(2)/p(1)).^exp(-p(3)*t);

Notice the .^ instead of ^ .

Your t is a vector, and the ^ operator is the matrix power operator, the generalization of repeated algebraic matrix multiplication

A^3 -> A*A*A but that is A inner-product A inner-product A

The ^ operator cannot be used with non-scalar power.

You want element-by-element power.

Note that your gompertz function expects to be passed in a vector with three values as the first parameter, but that when you go to plot the solution, you are passing it in over 30 values.

Alex Sha
on 2 Nov 2020 at 12:56

Hi, the Gompertz function will be as "y=p1*exp(p2*exp(p3*x))", or as you used one "y=p1*(p2/p1)^exp(-p3*x)"?, generaly, the former is right one.

If use function type as "y=p1*exp(p2*exp(p3*x))"， the result will be:

Root of Mean Square Error (RMSE): 21238286.4659304

Sum of Squared Residual: 3.15745368406247E16

Correlation Coef. (R): 0.997214434090321

R-Square: 0.994436627558079

Adjusted R-Square: 0.994270556738917

Parameter Best Estimate

---------- -------------

p1 1796330144.59206

p2 -1.35298148327126E22

p3 -0.0260125710402262

While if use "y=p1*(p2/p1)^exp(-p3*x)", the result is:

Root of Mean Square Error (RMSE): 61518184.3459848

Sum of Squared Residual: 2.6491409036586E17

Correlation Coef. (R): 0.976698363677716

R-Square: 0.953939693610727

Adjusted R-Square: 0.952564759091645

Parameter Best Estimate

---------- -------------

p1 187801397010.571

p2 2.30183479705941E-312

p3 0.00249872332405853

Opportunities for recent engineering grads.

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

Start Hunting!
## 0 Comments

Sign in to comment.