MATLAB Answers

What is wrong with my Gompertz Equation?

19 views (last 30 days)
Police6969 on 26 Oct 2020
Commented: Police6969 on 2 Nov 2020 at 17:06
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>
%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];
p0 = [.01 1000 554419273];
[p,error]=lsqcurvefit(@gompertz,p0,years,pops,[],[],options); %#ok<ASGLU>
%plot solution
modelpops = gompertz(pops,years);


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 26 Oct 2020
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.


Show 3 older comments
Police6969 on 2 Nov 2020 at 10:09
Hello Alex Sha, i have fixed the value for 2015 to be 1406847870. Could you perhaps show me how you had managed to graph the equation?
Alex Sha
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
Police6969 on 2 Nov 2020 at 17:06
Thanks for your help, but could you send me your entire code?, I am still slightly confused.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by