Results 1 to 6 of 6

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

  1. #1
    Newbie
    Joined
    Apr 2009
    Posts
    9

    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.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    What algorithm are you trying to implement here?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    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
    Last edited by CaptainBlack; April 2nd 2010 at 01:38 AM.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Apr 2009
    Posts
    9
    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.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Apr 2009
    Posts
    9
    Quote Originally Posted by CaptainBlack View Post
    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!
    Last edited by marty830; April 2nd 2010 at 01:57 AM.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Newbie
    Joined
    Apr 2009
    Posts
    9
    It worked! Thank you for your help!

    (PS: linespace should be linspace)
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. [SOLVED] Least squares approximation
    Posted in the Differential Geometry Forum
    Replies: 4
    Last Post: May 22nd 2011, 10:01 AM
  2. Matlab: Integration Approximation
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 11th 2010, 11:10 PM
  3. Replies: 0
    Last Post: November 30th 2009, 05:18 AM
  4. Least Squares Fit- Matlab
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 30th 2008, 01:26 AM
  5. [SOLVED] Best approximation/least squares/inner product
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: June 19th 2008, 06:44 AM

Search Tags


/mathhelpforum @mathhelpforum