help-octave
[Top][All Lists]
Advanced

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

fsolve bug? and strange behavior


From: Matthias Heiler
Subject: fsolve bug? and strange behavior
Date: 12 Jun 2005 18:31:33 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Hello,

(I'm new to octave.  I use matlab at work but for a private project I
recently tried octave and my first impression is quite good.  However,
if I'm doing strange things I'd appreciate any hints.)

For functions that are very flat fsolve sometimes returns completely
wrong answers (e.g. a x for which f(x) \approx 80 and info == 1).  Run
the code below with "calc_yield(85, 80, 0, 1, 10)" under ubuntu 5.04
(octave 2.1.64) for a demo.

Second, in the script below, if the function fn is already defined
before calling calc_yield then this definition is used instead of the
one inside the script.  This seems wrong since it makes it essentially
impossible to securely write functions with fsolve inside.  (You never
can be sure which function happens to get solved.)

Third, the global definition in the script doesn't make the variables
really global.  I.e., they are not available on top level after the
script is executed.  I think, this is a good thing, but contrasts to
the description given by "help global".

Yours,

  Matthias

===
function lambda = calc_yield(P, F, C, m, n)
  ## function lambda = calc_yield(P, F, C, m, n)
  ## 
  ## Yield of a bond.
  ##
  ## P: price
  ## F: Face value
  ## C: yearly coupon
  ## m: #payments/year
  ## n: years to maturity

  ## calc_yield(85, 80, 0, 1, 10)

  ## Copyright (c) 2005 by Matthias Heiler 
  ## $Id:$

global P_ F_ m_ n_ C_
P_ = P; 
F_ = F; 
m_ = m;
n_ = n; 
C_ = C;

ll = linspace(0, 2);
plot(ll, fn(ll));

for l0 = 0.1:0.2:2.0
  [lambda, info] = fsolve("fn", l0)
  if lambda > 0.0 && info == 1 #&& abs(fn(lambda)) < 0.1
    perror ("fsolve", info)
    break;                      # ***BREAK***
  else
    lambda = NaN;
  end
end
endfunction

function y = fn(ll)
  global P_ F_ m_ n_ C_ 
  y = (P_ - F_./((1+ll./m_).^n_) - C_./ll.*(1 - 1./((1 + ll./m_).^n_)));
endfunction
=== calc_yield.m





-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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