Solving a non-linear equation [message #88241] |
Wed, 02 April 2014 07:33  |
Reetsspacey
Messages: 3 Registered: June 2009
|
Junior Member |
|
|
Hi,
My problem is this: I have a non-linear equation, of the form y = exp(x)+x. I have a large matrix of Y values, and for each cell (Y-value) I would like to calculate a best-fit X. How can I do this???
I have tried defining a function (exp(x)+x), which I could then get the root of by a number of methods. However I would need to first subtract my Y, before I found the root. The problem then becomes, how can I tell IDL to find the root of exp(x)+x-Y, for a range of Y values?
Eeek my brain!
|
|
|
Re: Solving a non-linear equation [message #88242 is a reply to message #88241] |
Wed, 02 April 2014 08:36   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On Wednesday, April 2, 2014 10:33:43 AM UTC-4, Reetsspacey wrote:
> Hi,
>
>
>
> My problem is this: I have a non-linear equation, of the form y = exp(x)+x. I have a large matrix of Y values, and for each cell (Y-value) I would like to calculate a best-fit X. How can I do this???
The easiest way is to use Newton's method.
If you're trying to find the root of f(x), given a trial solution of x0, the next best solution is,
x = x0 - f(x0) / f'(x0)
where f'(x0) is the derivative of the function. Now you have a new x0 and can try again for multiple iterations.
For you, this would be, in IDL language,
for i = 0, n_iter-1 do begin
x = x0 - (x + exp(x) - y)/(1 + exp(x))
x0 = x
endfor
Your function is very well behaved (no extrema) so you should be able to pick x0=0 as your starting point and then keep iterating until you achieve the desired precision.
Craig
|
|
|
Re: Solving a non-linear equation [message #88243 is a reply to message #88242] |
Wed, 02 April 2014 10:45  |
Reetsspacey
Messages: 3 Registered: June 2009
|
Junior Member |
|
|
On Wednesday, 2 April 2014 11:36:15 UTC-4, Craig Markwardt wrote:
> On Wednesday, April 2, 2014 10:33:43 AM UTC-4, Reetsspacey wrote:
>
>> Hi,
>
>>
>
>>
>
>>
>
>> My problem is this: I have a non-linear equation, of the form y = exp(x)+x. I have a large matrix of Y values, and for each cell (Y-value) I would like to calculate a best-fit X. How can I do this???
>
>
>
> The easiest way is to use Newton's method.
>
>
>
> If you're trying to find the root of f(x), given a trial solution of x0, the next best solution is,
>
> x = x0 - f(x0) / f'(x0)
>
> where f'(x0) is the derivative of the function. Now you have a new x0 and can try again for multiple iterations.
>
>
>
> For you, this would be, in IDL language,
>
> for i = 0, n_iter-1 do begin
>
> x = x0 - (x + exp(x) - y)/(1 + exp(x))
>
> x0 = x
>
> endfor
>
>
>
> Your function is very well behaved (no extrema) so you should be able to pick x0=0 as your starting point and then keep iterating until you achieve the desired precision.
>
>
>
> Craig
Brilliant, that makes sense! Thank you!
|
|
|