# Thread: Matlab - Least squares approximation of f(x) = e^x

1. ## Matlab - Least squares approximation of f(x) = e^x

Hello,

I'm having some trouble with finding the least squares approximation (with given n) of $\displaystyle f(x) = e^x$ in [−1, 1] using matlab. The approximation should be of form $\displaystyle P_n(x) = a_0 + a_1x + a_2x^2 + · · · + a_nx_n$.

My Matlab code:
Code:
function z = leastsq(n)
• Plot $\displaystyle f(x) = e^x$
Code:
x = (-1.5:0.001:1.5);
y = exp(x);
plot(x, y);
xlim([-1.5 1.5])
• Integral of $\displaystyle f_j = \int^1_{-1} 2x\,dx$

Code:
matrixf = zeros(n,1);
end
• Problem: matrix with coefficients of $\displaystyle P_n(x)$

Code:
    for (count = 0:n)
if (count == 0 && mod((1+gradex),2) ~= 0)
elseif (count ~= 0)
end
end
The code I used here succesfully calculates the coefficients for $\displaystyle f(x) = x$. I can't find how to make it work for $\displaystyle f(x) = e^x$...

The next step would be to calculate the coefficients $\displaystyle c_0$ to $\displaystyle c_n$:

Code:
coef = matrixa\matrixf
Finally the function $\displaystyle P_n(x) = a_0 + a_1x + a_2x^2 + · · · + a_nx_n$ should be constructed and plotted.

Any hints or tips would be greatly appreciated.

2. What algorithm are you trying to implement here?

CB

3. Here is some code which I believe may be for the algorithm (linear regression) you are trying to use:

Code:
>> x=linespace(-1,1,10);  %sample points for the fit
>> n=4;                   %order of polynomial
>> xx=zeros(length(x),n); %coefficient array
>> for idx=0:3;xx(:,idx+1)=(x.').^(idx);end;
>> a=exp(x.');            %right hand side vector
>> coeffs=xx\a;           %linear least squares solution for coeffs
>> pp=zeros(size(x));     %evaluate the polynomial
>> for idx=1:n;pp=pp+coeffs(idx)*x.^(idx-1);end;
>> plot(x,pp);
>> hold on;plot(x,exp(x));hold off

4. I'm trying to find the solution for

$\displaystyle f_j = \int^1_{-1} (f(x)-p_n(x))^2\,dx$

(So I'm not trying to approximate a vector of data points! For which I would use $\displaystyle \sum_{j=1}^{N} (y_j-(ax_j+b_j))^2$)

I'm trying to find the miminum solution for

$\displaystyle \int^1_{-1} (f(x)-a_0 + a_1x + a_2x + ... + a_nx^n)^2\,dx$

I already wrote the code to take the integrals of the n amount of f_j's.

These are all placed in matrixf.

Next I should calculate the coefficient matrix (matrixa) and solve the system by using

Code:
coef = matrixa\matrixf
coef will then be a vector of length n+1 containing the coefficients $\displaystyle c_0$ to $\displaystyle c_n$. These will be used to describe the approximating polynomial $\displaystyle P_n(x)$

I hope I made it a bit clearer. My apologies, all those lines of code make it hard to express myself properly.

5. Originally Posted by CaptainBlack
Here is some code which I believe may be for the algorithm (linear regression) you are trying to use:
Thanks! I believe that is what I'm trying to do...
I'll try it out!

6. It worked! Thank you for your help!

(PS: linespace should be linspace)