[Top][All Lists]

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

How to implement a summation function for numerical solving?

From: andrewcd
Subject: How to implement a summation function for numerical solving?
Date: Sun, 12 Jun 2011 23:47:18 -0700 (PDT)

Hi All,  First post here, and apologies if this topic is searchable -- I am a
relative newcommer to octave, and don't know the appropriate terms to search

My problem:
I need to solve a nonlinear system of equations containing a summation term
running the length of a vector with length (lx).  I have no idea how to
implement this.  I can make it work by brute force -- manually typing in the
expansion of the summation.  Of course, this is not flexible or especially
useful as (lx) changes.  

What I want to do is solve for (lam1) and (lam2).  Here is my non-working
function fcns = eqns(z)
mu = 3.5
variance = 2.9
lx = 6


for n=1:lx
        f1(n) = n

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

mu = ones(1:lx)*mu

fcns(1) = (f1-mu)'*p
fcns(2) = (f2-variance)'*p


What I want to do, but what Octave doesn't seem to let me do, is to make a
vector (p) that contains (lam1) and (lam2), and then solves for them from
fcns(1) and fcns(2).

When I try to run this code, I get the following error:
error: operator -: nonconformant arguments (op1 is 1x6, op2 is 1x2x3x4x5x6)
error: called from:
error:   eqns at line 29, column 9
error:   /usr/share/octave/3.2.3/m/optimization/fsolve.m at line 177, column
error:   /home/andrew/Documents/test.m at line 36, column 8

In addition, scrolling up, I see that Octave calculated (p) as (p = 1 1 1 1
1 1).  This is NOT the vector of unsolved functions that I want to later

There must be a way to do it, but it probably involves some programming that
I haven't learned yet.  I've spent a long time trying to figure this out,
but I'm not sure where to start, and it is difficult to know which terms to
search for, as I don't know the jargon yet.

Any help is greatly appreciated.

(Context: I am trying to teach myself how to program max ent using a 6-sided
die as an example.  I'll apply this to some other stuff later -- this is
just for the purpose of learning how to get the code working).

View this message in context:
Sent from the Octave - General mailing list archive at

reply via email to

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