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

• Apr 1st 2010, 07:43 AM
marty830
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 $f(x) = e^x$ in [−1, 1] using matlab. The approximation should be of form $P_n(x) = a_0 + a_1x + a_2x^2 + · · · + a_nx_n$.

My Matlab code:
Code:

function z = leastsq(n)
• Plot $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 $f_j = \int^1_{-1} 2x\,dx$

Code:

matrixf = zeros(n,1);     for (grade = 0:n)         matrixf(grade+1,1) = quad(@(x) exp(x)*exp(grade),-1,1);     end
• Problem: matrix with coefficients of $P_n(x)$

Code:

    for (count = 0:n)         if (count == 0 && mod((1+gradex),2) ~= 0)             matrixa(gradex+1, 1) = 2/(count+1+gradex);         elseif (count ~= 0)             matrixa(gradex+1, count+1) = quad(@(x) exp((count)*x)*exp(gradex),-1,1);         end     end
The code I used here succesfully calculates the coefficients for $f(x) = x$. I can't find how to make it work for $f(x) = e^x$...

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

Code:

coef = matrixa\matrixf
Finally the function $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.
• Apr 1st 2010, 11:39 PM
CaptainBlack
What algorithm are you trying to implement here?

CB
• Apr 2nd 2010, 12:26 AM
CaptainBlack
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
• Apr 2nd 2010, 12:43 AM
marty830
I'm trying to find the solution for

$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 $\sum_{j=1}^{N} (y_j-(ax_j+b_j))^2$)

I'm trying to find the miminum solution for

$\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 $c_0$ to $c_n$. These will be used to describe the approximating polynomial $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. (Sleepy)
• Apr 2nd 2010, 12:45 AM
marty830
Quote:

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... (Happy)
I'll try it out!
• Apr 2nd 2010, 01:21 AM
marty830
It worked! Thank you for your help!

(PS: linespace should be linspace)