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