Least squares regression outputting function handle

AI Thread Summary
The discussion centers on implementing a MATLAB function, BreakRod, to determine coefficients for a model predicting the number of twists required to break a rod based on two chemical components. The function utilizes least squares regression to fit the model defined by the equation y(u, v) = a1 + a2 exp(u^2) + a3√v + a4uv, using data from a 3 × N cell array. Participants discuss the creation of the A matrix and the proper formatting of the function handle to include the coefficients. Clarifications are made regarding whether actual coefficient values can be directly placed in the function handle instead of using P(1), P(2), etc. The thread concludes with a confirmation that the implementation was correct.
gfd43tg
Gold Member
Messages
947
Reaction score
48

Homework Statement


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

\begin{Bmatrix}y_{1} &amp; y_{2} &amp; \dots &amp; y_{N} \\<br /> u_{1} &amp; u_{2} &amp; \dots &amp; u_{N} \\<br /> v_{1} &amp; v_{2} &amp; \dots &amp; v_{N}\end{Bmatrix}

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:
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

Homework Equations


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:
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:
cellData{:}

ans =

     1     4     5     6ans =

    1.0000    2.0000    3.0000    0.5000ans =

     2     1     7     3
Code:
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:
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:
Physics news on Phys.org
nevermind, apparently I did it correctly
 

Similar threads

Replies
7
Views
1K
Replies
3
Views
2K
Replies
8
Views
2K
Replies
10
Views
2K
Replies
15
Views
3K
Replies
4
Views
5K
Back
Top