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: Liam Groener
Subject: Re: How to implement a summation function for numerical solving?
Date: Mon, 13 Jun 2011 01:05:15 -0700

On Jun 12, 2011, at 11:47 PM, andrewcd wrote:

> 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
> for.
> 
> 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
> code:
> ------------------------------------------------------------------------------
> function fcns = eqns(z)
> mu = 3.5
> variance = 2.9
> lx = 6
> 
> lam1=z(1)
> lam2=z(2)
> 
> for n=1:lx
>       f1(n) = n
>       end
> 
> for n=1:lx
>       f2(n) = (n-mu).^2
>       end
>       
> for n = 1:lx
>       p(n) = e.^(-(lam1*f1(n) + lam2*f2(n)))
>       end
> 
> mu = ones(1:lx)*mu
> 
> fcns(1) = (f1-mu)'*p
> fcns(2) = (f2-variance)'*p
> 
> end
> --------------------------------------------------------------------
> 
> 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
> 6
> 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
> solve.
> 
> 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: 
> http://octave.1599824.n4.nabble.com/How-to-implement-a-summation-function-for-numerical-solving-tp3593289p3593289.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave

I'm not completely clear in what you are trying to do. However, looking at your 
code; I expect this is what you wanted:

function fcns = eqns(z)
mu = 3.5;
variance = 2.9;
lx = 6;

lam1=z(1);
lam2=z(2);

f1 = 1:lx;
f2 = (f1-mu).^2;
p = exp(-lam1*f1 -lam2*f2);
mu(1:lx) = mu;
fcns = [(f1-mu)'*p,(f2-variance)'*p];
end




reply via email to

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