I am working on my homework, which is exercise 11.2b) from "Numerical Linear Algebra" by Trefethen.
The exercise asks me to fit a function $f(x) = \dfrac{1}{x}$ by a linear combination of the functions $e^x, \sin(x)$ and gamma function $\Gamma(x)$ on $[0,1]$ using Matlab. They also state that the following fact might be helpful: if $g(x)=\dfrac{1}{\Gamma(x)}$, then $g'(0)=1$.
I have written a Matlab code solving the least-square problem. I let $x$ is linspace(0,1,1000) and $A$ is matrix whose columns are $e^x, \sin(x), \Gamma(x)$. Then, the coefficients are computed by using this code on Matlab: $A\backslash b$. However, it turns out to be wrong. Moreover, if I did it this way, I did not use the fact they give me.
Can somebody please give me any idea on this? Any help is really appreciated!
As Lutz Lehmann described, we need the Gamma function in order remove the singularity at $0$. The removal of the singularity is crucial for the success of this approximation. Most probably, you can use the hint for arguing that $\frac 1 x -\Gamma(x)$ is bounded for $x\to 0$. However, I was not able to use this hint properly. Instead I used the Laurent expansion of the Gamma function at $0$. In case, you are not familar with Laurant expansions, it is a similar representation as the Taylor expansion but we also allow negative indices in the sum. So as shown in here, we have the following representation $$\Gamma(x)=\frac 1 x -\gamma+ \mathcal O(x)\qquad \text{for }x\to 0,$$ while $\gamma = \Gamma'(1)\approx 0.57721$, also known as the Euler-Mascheroni-constant, and $\mathcal O$ denotes the Landau symbol. Thus, we get as direct consequence $$\lim_{x\to 0_+} \frac 1 x -\Gamma(x)= \lim_{x\to 0_+}\gamma+ \mathcal O(x)=\gamma \approx 0.57721.$$ Hopefully this solution is still satisfying, even though it uses more advanced information than stated in the exercise.
However, as stated in the comment of Lutz Lehmann we can now approximate $\frac 1 x -\Gamma(x)$ by $e^x$ and $\sin(x)$. As you sad in your post, I tried it as well with a least square routine. I got an absolute error of approx $0.1723$ and the following coefficients for $e^x$ and $\sin(x)$, respectively, $$0.625935943114312$$ $$-1.817300815880439.$$