| Re: Solving nonlinear equations [message #36836 is a reply to message #36835] |
Sat, 01 November 2003 15:03  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Thierry Savin <savin@mit.edu> writes:
> Hi all,
>
> Is it possible to create a function called let's say "solvef":
>
> IDL> x=solvef(a,'f')
>
> that returns x the solution of f(x,a)=0 ?
Either MPFIT or TNMIN from my web page can do this kind of job. Both
are designed to be function minimizers. MPFIT will minimize the
square of any function [ in fact the sum of squares of N functions ].
Although people don't normally think of it as so, MPFIT is actually an
equation solver, in addition to a least squares solver.
Your set of fixed parameters, A, would normally passed using
FUNCTARGS. Your notation of X (the varying parameter) and A (the
fixed parameters) are actually reversed from the notation used in
MPFIT or MPFITFUN, where P is the varying parameter and X are the
fixed parameters. Example:
function fx, a, x
return, a(0)+a(1)*x+a(2)*x^2
end
print, mpfitfun('fx', [-2d,10d,4d], [0,0,0], 10, [0d], /quiet)
0.18614066
[ Not sure why an uncertainty estimate of 10 is needed though, hmmm. ]
Or, one can define a new function which is the square of the desired
function, and use TNMIN:
function fffsq, x, a=a
return, ( a(0)+a(1)*x+a(2)*x^2)^2
end
print, tnmin('fffsq', [0d], functargs={a:[-2d,10d,4d]},/autoderiv, /quiet)
0.18614066
Both of these numbers are close to the exact value.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
|