MATLAB Answers

How to get a best-fit parabola using elementwise multiplication

8 views (last 30 days)
Robert Lew
Robert Lew on 23 Apr 2021
Answered: Mathieu NOE on 23 Apr 2021
%The same data is used for the activity. These are provided for you.
X = [-2 -1 1 2].'
Y = [3 1 0 1].'
%Use the length() command to determine the size of the column vector X. Store this value in m.
m = length(X)
%Set up the appropriate matrix A to find the best-fit parabola of the form y=C+Dx+Ex^2. The
%first column of A will contain all 1's, using the ones() command. The second column of A
%contains x values that are stored in X. The third column of A contains the squared x values
%that are stored in X. Elementwise multiplication of X by itself, using .* operator, will
%produce the desired values for the third column.
A = [ones(m,1) X X.*X]
%Calculate the matrix products. These are provided for you.
A_transposeA = A.' * A
A_transposeY = A.' * Y
%Use the backslash operation to solve the overdetermined system. Store this in Soln2.
Soln2 = A_transposeA\A_transposeY
%Define the x values to use for plotting the best-fit parabola. This creates a vector x.
%This is provided for you.
x=-4: 0.1 :4
%Define the best-fit parabola, storing it in yquadratic. Elementwise multiplication of the
%x values times themselves to square them is achieved by using .* operator (because x is a vector).
yquadratic = Soln2(1) + Soln2(2)*x.*x %<-- problem is here
%The following sequence of commands plots the data and the best-fit parabola. The command is
%provided for you.
plot(x, yquadratic, X, Y, 'k*');grid;shg

Answers (1)

Mathieu NOE
Mathieu NOE on 23 Apr 2021
hello Robert
simply apply what the exercise description says : you are supposed to fit a parabola of the form y=C+Dx+Ex^2
there are 3 terms in this equation so your code should be :
yquadratic = Soln2(1) + Soln2(2)*x + Soln2(3)*x.*x; %<-- problem solved
and not
yquadratic = Soln2(1) + Soln2(2)*x.*x %<-- problem is here
so updated code :
%The same data is used for the activity. These are provided for you.
X = [-2 -1 1 2].';
Y = [3 1 0 1].';
%Use the length() command to determine the size of the column vector X. Store this value in m.
m = length(X);
%Set up the appropriate matrix A to find the best-fit parabola of the form y=C+Dx+Ex^2. The
%first column of A will contain all 1's, using the ones() command. The second column of A
%contains x values that are stored in X. The third column of A contains the squared x values
%that are stored in X. Elementwise multiplication of X by itself, using .* operator, will
%produce the desired values for the third column.
A = [ones(m,1) X X.*X];
%Calculate the matrix products. These are provided for you.
A_transposeA = A.' * A;
A_transposeY = A.' * Y;
%Use the backslash operation to solve the overdetermined system. Store this in Soln2.
Soln2 = A_transposeA\A_transposeY;
%Define the x values to use for plotting the best-fit parabola. This creates a vector x.
%This is provided for you.
x=-4: 0.1 :4;
%Define the best-fit parabola, storing it in yquadratic. Elementwise multiplication of the
%x values times themselves to square them is achieved by using .* operator (because x is a vector).
% yquadratic = Soln2(1) + Soln2(2)*x.*x %<-- problem is here
yquadratic = Soln2(1) + Soln2(2)*x + Soln2(3)*x.*x; %<-- problem solved
%The following sequence of commands plots the data and the best-fit parabola. The command is
%provided for you.
plot(x, yquadratic, X, Y, 'k*');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!