4

In the past I've fit polynomials by solving the set of equations. I can fit $f(x) = \exp(a+bx)$ to point $A$ and $B$ where I know $x$ and $f(x)$ for both points.

If I want to fit to a specific gradient at point $A$ and $B$, I can use $f(x) = \exp(a+bx+cx^2 +dx^3)$ - so I know $x, f(x)$ and $f'(x)$ at point $A$ and point $B$.

Do I have to solve this using the Levenberg-Marquardt algorithm (or something similar) or is there a simpler/more efficient way?

Cheers

coldnumber
  • 3,721

1 Answers1

2

You have $$y = \exp(a+bx+cx^2 +dx^3)$$ $$y'=(b+2cx+3dx^2)\,\exp(a+bx+cx^2 +dx^3)$$ and you know ($x_1,y_1,y'_1$), ($x_2,y_2,y'_2$) so the four equations are $$y_1 = \exp(a+bx_1+cx_1^2 +dx_1^3)$$ $$y_1'=(b+2cx_1+3dx_1^2)\,\exp(a+bx_1+cx_1^2 +dx_1^3)$$ $$y_2 = \exp(a+bx_2+cx_2^2 +dx_2^3)$$ $$y_2'=(b+2cx_2+3dx_2^2)\,\exp(a+bx_2+cx_2^2 +dx_2^3)$$ So $$r_1=\frac{y_1'}{y_1}=b+2cx_1+3dx_1^2$$ $$r_2=\frac{y_2'}{y_2}=b+2cx_2+3dx_2^2$$ These two equations allow to eliminate $b$ and $c$.

Similarly, since $b$ and $c$ have been already expressed as linear functions of $d$, solving $$\log(y_1)=a+bx_1+cx_1^2 +dx_1^3$$ $$\log(y_2)=a+bx_2+cx_2^2 +dx_2^3$$ would give $a$ and $d$.

For sure, you could do faster using matrix calculations since you reduced the problem to four linear equations for four unknowns $a,b,c,d$ namely $$r_1=\frac{y_1'}{y_1}=b+2cx_1+3dx_1^2$$ $$r_2=\frac{y_2'}{y_2}=b+2cx_2+3dx_2^2$$ $$r_3=\log(y_1)=a+bx_1+cx_1^2 +dx_1^3$$ $$r_4=\log(y_2)=a+bx_2+cx_2^2 +dx_2^3$$

Hoping no mistakes, the expressions are rather simple $$d=\frac{r_1+r_2}{(x_1-x_2)^2}+\frac{2 (r_4-r_3)}{(x_1-x_2)^3}$$ $$c=\frac{r_1-r_2}{2 (x_1-x_2)}-\frac{3}{2} d (x_1+x_2)$$ $$b=-2 c x_1-3 d x_1^2+r_1$$ $$a=-b x_2-c x_2^2-d x_2^3+r_4$$

  • Hi Claude, thank you for that. I should have spotted the y' there myself. I've implemented your method in fortran and I'll try to post that here. Thank you again! – benpalmer Aug 03 '15 at 17:56
  • You are very welcome ! If you see what I added, you have the analytical formulae for $d,c,b,a$ in a cascaded manner. Glad to know you code in Fortran (I started in 1960 and this is my only programming language). Cheers. – Claude Leibovici Aug 04 '15 at 04:40
  • Hi Claude, that's great thank you. I've used a few languages, but I have to say I really enjoy Fortran. I can't paste the code here, but I've put a link to my website and I've credited you for your help. [link] (http://www.benblogging.co.uk/index.php?page_id=w9jVrI_X0Jg-&blogID=tNzSrquXj6srF51Zk5FHhevaoMvCq6acjqBZeLnByd.g3d_ew9i1sw--) – benpalmer Aug 04 '15 at 10:21
  • Hi Ben ! Thanks for that. Let me confess : I speak in Fortran, I dream in Fortran ! When it was presented to me in 1960 by IBM trainers, it was supposed to be a curiosity ... with no future ! – Claude Leibovici Aug 04 '15 at 10:29
  • Hi Ben ! I visited your website. Interesting material, for sure. If I may suggest, there are much simpler ways to solve the double decay problem. – Claude Leibovici Aug 04 '15 at 10:39
  • Hi Claude, I'd definitely be interested to know any other ways. Can the other methods be extended to three or more exponential terms? I was only born in 1983, but even with the other languages available I really enjoy using Fortran. – benpalmer Aug 04 '15 at 11:27
  • Do you want to go to the chat room ? – Claude Leibovici Aug 04 '15 at 11:30
  • Hi Claude I've just moved over to the chatroom – benpalmer Aug 04 '15 at 11:38