help-octave
[Top][All Lists]

## 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: Wed, 15 Jun 2011 22:46:12 -0700

```On Jun 15, 2011, at 7:21 AM, andrewcd wrote:

> Thanks everybody for all of your help so far.  Now I have a new problem --
> fsolve is highly sensitive to the initial "guess" vector.  I guess that it
> is not iterating enough, or something.  Or, since it is guessing at the
> value of an exponent, small changes by its standards lead to big changes in
> my results.  Any thoughts on how I can get around this?  Increase the number
> of iterations somehow?  Or is there a different way to solve simultaneous
> nonlinear equations?
>
> To get a flavor for my problem, try to run my code below, using guess =
> [.5,0] and guess = [0,0].  The code *should* eventually spit out a normal
> curve with a mean and variance of 2.  But the wrong guess gives the wrong
> curve.
>
> Thanks again, and thanks in advance.
>
> Code:
> ------------------------------------------------------------------------------------------------
> clear all
>
> global mu;
> global x;
> global lx;
> global xmin;
> global variance;
> AT = .5;
> xmin = -10;
> xmax = 10;
> stp = .1;
> x = xmin:stp:xmax;
> lx = length(x);
> mu = 2;
> variance = 2;
>
> guess = [.5,0]
>
> function fcns = eqns(z);
> global x;
> global lx;
> global mu;
> global variance;
> lam1=z(1);
> lam2=z(2);
> f1 = x;
> f2 = (x-mu).^2;
> p = exp(-lam1*f1 - lam2*f2) ;
> fcns = [(f1-mu)*p', (f2-variance)*p'];
> end
>
> result = fsolve(@eqns, guess)
>
> p = e.^(-result(1)*x - result(2)*(x-mu).^2);
>
> Z = sum(p);
> P=p./Z;
>
> plot( x, P);
> sum(P)
> axis([xmin-1,xmax+1,0,max(P)*2])
>
> c = cumsum(P);
> for n = 1:length(c);
>       if c(n)<AT;
>               EV(n) = c(n);
>               endif
>       end
>
> L = length(EV);
> EV = stp*L+xmin
>
Hi,
Since no one else has answered your question, I thought I'd take a crack at it.
But, I don't know what your problem actually is. I ran the two cases you
suggested with the default tolerance. The results are:
-------------------------------------------------------
guess =
0.50000   0.00000

result =
8.0663e-08   2.5000e-01

FVEC =
-7.5485e-06   7.7026e-05

INFO =  2
OUTPUT =
iterations =  185
successful =  184
funcCount =  213

ans =  1
EV =  2
-----------------------------
guess =
0   0

result =
-6.5087e-09   2.5000e-01

FVEC =
-1.3681e-06   1.6720e-05

INFO =  2
OUTPUT =
iterations =  70
successful =  69
funcCount =  90

ans =  1.0000
EV =  2

The central part of the plot is:
``` PastedGraphic-1.pdf
```Here the the [.5,0] guess is plotted as a solid red line while the [0,0] guess