help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to implement a summation function for numerical solving?


From: andrewcd
Subject: Re: How to implement a summation function for numerical solving?
Date: Mon, 13 Jun 2011 05:31:10 -0700 (PDT)

Thanks Liam!  You helped me get it working.  I figured out that I had another
couple of bugs, since fixed.  For posterity, here is the code to calculate a
maximum entropy distribution given a mean and a variance.  No guarantees
that it is the most efficient way to do this, but it will suit my purposes
for now.

Code:
-------------------------------------------------------------------------------------------------------------------------------
clear all


mu = 2
xmin = -5
xmax = 10
x = xmin:.2:xmax
lx = length(x)
stp = .2

function fcns = eqns(z)
mu = 2;
variance = 2;
xmin = -5;
xmax = 10;
stp = .2
x = xmin:stp:xmax;
lx = length(x);
lam1=z(1);
lam2=z(2);

f1 = xmin:stp:xmax;
f2 = (f1-mu).^2;

for n = 1:lx;
        p(n) = e.^(-(lam1*f1(n) + lam2*f2(n)));
        end

mu(1:lx) = mu;
fcns = [(f1-mu)*p', (f2-variance)*p'];

end



guess = [0,0 ];

result = fsolve(@eqns, guess);
p = zeros(1,lx);
for n=1:lx;
        p(n) = e.^(-((result(1)*x(n))+result(2)*(x(n)-mu).^2));
        end
        
Z = sum(p);
P=p./Z


plot( x, P);
sum(P)
axis([xmin-1,xmax+1,0,1])

--
View this message in context: 
http://octave.1599824.n4.nabble.com/How-to-implement-a-summation-function-for-numerical-solving-tp3593289p3593738.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]