[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Non linear equations: howto?
From: |
Thomas Shores |
Subject: |
Re: Non linear equations: howto? |
Date: |
Mon, 20 Oct 2008 21:54:39 -0500 |
User-agent: |
Thunderbird 2.0.0.16 (X11/20080723) |
Fabrice DELENTE wrote:
Hello.
I'd like to solve numerically 6 equations like
f(1)=0
f(2)=2
f(3)=3
f(4)=4
f(5)=2.5
f(6)=-4
where f(x) is defined by
f(x)=(a*x+b)*exp(c*x+d)+e
a, b, c, d and e being the unknowns of my system.
I tried using octave with these commands:
function y = f (x)
y(1) = (x(1)*1+x(2))*exp(x(3)*1+x(4))+x(5)
y(2) = (x(1)*2+x(2))*exp(x(3)*2+x(4))+x(5)-1
y(3) = (x(1)*3+x(2))*exp(x(3)*3+x(4))+x(5)-3
y(4) = (x(1)*4+x(2))*exp(x(3)*4+x(4))+x(5)-4
y(5) = (x(1)*5+x(2))*exp(x(3)*5+x(4))+x(5)-2.5
y(6) = (x(1)*6+x(2))*exp(x(3)*6+x(4))+x(5)+4
endfunction
[x,info]=fsolve("f",[-10;10])
but octave answers
error: invalid vector index = 3
error: evaluating binary operator *' near line 2, column 32
error: evaluating binary operator +' near line 2, column 34
error: evaluating argument list element number 1
error: evaluating binary operator *' near line 2, column 23
error: evaluating binary operator +' near line 2, column 40
error: evaluating assignment expression near line 2, column 8
error: called from f'
error: fsolve: evaluation of user-supplied function failed
How can I give a vector size? Is octave a good tool for solving this kind of
problem? Do you know of any tool that would do the job?
Thanks!
Fabrice,
As has been already noted, you have no reason to think there is a unique
solution or, for that matter, any solution at all to this overdetermined
system of 6 equations in 5 unknowns. Also, as noted, the variable x(4)
as you have it is irrelevant. Factor out exp(d) in your expression for
f(x)=(a*x+b)*exp(c*x+d)+e and it becomes a factor of a and b, so is not
needed. You now have a system of 6 equations in 4 unknowns. Also, make
up your mind as to what f(2) should be. Your first list says 'f(2) =
2', but the function f that you define says that the value is 1.
Next, fsolve is complaining that you do not have the correct number of
variables in your initial approximation to x in the calling parameters.
You have [-10;10], when you should have something like [-1; 0; 1; 0]
for your initial guess. Even so, fsolve won't work correctly for your
problem; I can't find much documentation on fsolve, but I suspect that
the problem is due to the fact that the number of unknowns doesn't match
the number of equations. You will get an answer, but it's quite poor.
A better approach might be for you to minimize the sum of squares of the
residual vector. Do something like this: first fix your definition of
f(x) to have only x(1), x(2), x(3), x(4) (your original a,b,c,e). Then
create a new function by
function retval = fsquares(x), retval = sumsq(f(x)); end
Now use syntax like
[X, OBJ, INFO, ITER, NF, LAMBDA] = sqp ([-1,0,1,0]','fsquares')
This is a minimization routine ('sequential quadratic program') from
which you'll get a modestly decent answer. To check it, build a function
for your expression using these parameters from X and compare its values
to your specified list of values of f(x). Different starting points
will give different answers for the parameters, suggesting that this
function has many local minima. Experiment a bit and see what you can
find. Of course, what you want to find is a minimum very close to 0.
There are many reasons why this simple programming may not give
particularly good answers. Issues like poor scaling of the variables,
etc. etc. Optimization is a big subject. Good luck.
Thomas Shores
- Non linear equations: howto?, Fabrice DELENTE, 2008/10/19
- Re: Non linear equations: howto?, Søren Hauberg, 2008/10/19
- Re: Non linear equations: howto?, Fabrice DELENTE, 2008/10/19
- Re: Non linear equations: howto?, Søren Hauberg, 2008/10/19
- Re: Non linear equations: howto?, Fabrice DELENTE, 2008/10/19
- Re: Non linear equations: howto?, Dmitri A. Sergatskov, 2008/10/19
- Re: Non linear equations: howto?, Fabrice DELENTE, 2008/10/20
- Message not available
- Non linear equations: howto?, Dmitri A. Sergatskov, 2008/10/20
- Re: Non linear equations: howto?, Ben Abbott, 2008/10/20
- Re: Non linear equations: howto?, Ivan Sutoris, 2008/10/19
Re: Non linear equations: howto?,
Thomas Shores <=