Least squares regression outputting function handle

In summary: I was confused.In summary, the conversation discusses the function y(u,v) that represents the number of twists required to break a rod based on the percentages of two chemical components, u and v. The function is proposed as y(u,v) = a1 + a2 exp(u^2) + a3 sqrt(v) + a4 uv, and the coefficients a1, a2, a3, and a4 need to be determined by fitting data from a 3 x N cell array called CellData. The task is to complete a MATLAB function called BreakRod, which will determine the coefficient vector P and return a function handle to the vectorized function defined in equation (1). The subfunction Adef is
  • #1
gfd43tg
Gold Member
950
50

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

[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:
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
  • #2
nevermind, apparently I did it correctly
 

1. What is "Least squares regression"?

Least squares regression is a statistical method used to find the best fit line or curve for a set of data points. It is used to predict the relationship between two or more variables by minimizing the sum of the squared differences between the actual data points and the estimated values on the line or curve.

2. What is a "function handle" in the context of least squares regression?

A function handle is a way to represent a mathematical function as a variable in a programming language. In the context of least squares regression, a function handle is used to define the mathematical equation that represents the best fit line or curve for the data points.

3. What is the purpose of outputting a function handle in least squares regression?

The purpose of outputting a function handle in least squares regression is to allow for further analysis and manipulation of the mathematical equation that represents the best fit line or curve. This can include using the function to make predictions for new data points or to compare it to other regression models.

4. How is a function handle created in least squares regression?

A function handle is typically created by first defining the mathematical equation for the best fit line or curve, and then using a command in a programming language (such as MATLAB or Python) to create a function handle object. This object can then be used to represent the mathematical equation in the code.

5. Are there any limitations to using a function handle in least squares regression?

One limitation of using a function handle in least squares regression is that it assumes a linear relationship between the variables being analyzed. This means that it may not be suitable for data sets that have a non-linear relationship. Additionally, the accuracy of the function handle depends on the quality of the data and the assumptions made in the regression model.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
842
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
486
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Advanced Physics Homework Help
Replies
6
Views
311
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top