1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Least squares regression outputting function handle

  1. Aug 13, 2014 #1

    Maylis

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    The number of twists ##y## required to break a certain rod is a function of the percentages ##u## and ##v##
    of each of two chemical components present in the rod. The following function is proposed
    ##y(u, v) = a_{1} + a_{2} exp(u^{2}) + a_{3}\sqrt{v} + a_{4}uv## (1)
    where the coefficients a1, a2, a3 and a4 need to be determined by fitting the data contained in a
    3 × N cell array CellData described as follows

    [itex]\begin{Bmatrix}y_{1} & y_{2} & \dots & y_{N} \\
    u_{1} & u_{2} & \dots & u_{N} \\
    v_{1} & v_{2} & \dots & v_{N}\end{Bmatrix}[/itex]

    Assume that CellData is available in the workspace.
    Complete the missing or incomplete lines of MATLAB function BreakRod shown on the next page
    that:

    (a) Determines the coefficient vector ##p = [a_{1}\hspace{0.05in} a_{2}\hspace{0.05in} a_{3}\hspace{0.05in} a_{4} ]^{T}##
    by fitting the data contained in a 3 × N cell array CellData using least squares regression.
    (b) Returns a function handle to the vectorized function defined in Eq. (1) using the coefficients
    determined in (a). The subfunction Adef defines the matrix A needed to perform the least squares regression.

    Code (Text):
    function yh = BreakRod(CellData)
    % Determines the 4 x 1 coefficient vector P = [ a1 a2 a3 a4 ]'
    % Returns vectorized function handle to Eq. (1). Uses sub-function Adef

    % Create the N x 1 double arrays Y, U and V from CellData

    Y =
     _____________________________________________________________________
    U =
    _____________________________________________________________________

    V =
    _____________________________________________________________________
    % Create A matrix
    A = Adef(U,V);

    % Determine coefficient vector P using least squares

    P =
    _____________________________________________________________________
    % Return function handle yh

    yh =
    _____________________________________________________________________

    function A = Adef(U,V)
    % initialize A

    N =
    _____________________________________________________________________

    A = ones(N, );
    ______________________

    % Define second column of A corresponding to coefficient a2

    _____________________________________________________________________
    % Define third column of A corresponding to coefficient a3

     _____________________________________________________________________
    % Define fourth column of A corresponding to coefficient a4
    2. Relevant equations



    3. The attempt at a solution
    I am wondering, when I do this is there a way for the actual numbers of P to be in the function handle, or is what I have with P(1), P(2), etc equivalent and you can't put the actual values into the function handle?

    Code (Text):
    function yh = BreakRod(cellData)
    Y = cellData{1,:}';
    U = cellData{2,:}';
    V = cellData{3,:}';
    A = Adef(U,V);
    P = A\Y;
    yh = @(U,V) P(1) + P(2)*exp(U.^2) + P(3)*sqrt(V) + P(4)*U.*V;
    end

    function A = Adef(U,V)
    N = size(U,1);
    A = ones(N,4);
    A(:,2) = exp(U.^2);
    A(:,3) = sqrt(V);
    A(:,4) = U.*V;
    end
    Code (Text):
    cellData{:}

    ans =

         1     4     5     6


    ans =

        1.0000    2.0000    3.0000    0.5000


    ans =

         2     1     7     3
     
    Code (Text):
    yh = BreakRod(cellData)

    yh =

        @(U,V)P(1)+P(2)*exp(U.^2)+P(3)*sqrt(V)+P(4)*U.*V
    I am not sure if it was intended that it would be
    Code (Text):
    P

    P =

       30.4842
        0.0299
       -3.4979
      -12.3093
    With the values for P in place of P(1), P(2), etc. and how I will get those actual values into the handle.
     
    Last edited: Aug 13, 2014
  2. jcsd
  3. Aug 13, 2014 #2

    Maylis

    User Avatar
    Gold Member

    nevermind, apparently I did it correctly
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Least squares regression outputting function handle
Loading...