Science:Math Exam Resources/Courses/MATH307/December 2010/Question 04 (b)
• Q1 (a) • Q1 (b) • Q1 (c) • Q1 (d) • Q1 (e) • Q2 (a) • Q2 (b) • Q2 (c) • Q2 (d) • Q2 (e) • Q3 (a) • Q3 (b) • Q3 (c) • Q3 (d) • Q4 (a) • Q4 (b) • Q5 (a) • Q5 (b) • Q5 (c) • Q6 (a) • Q6 (b) • Q6 (c) • Q6 (d) • Q6 (e) • Q6 (f) • Q7 (a) • Q7 (b) • Q7 (c) •
Question 04 (b)
In this question we are once again given 4 points (x1, y1), (x2, y2), (x3, y3) and (x4, y4) in the plane. This time we want to find a quadratic function that comes closest to going through the points by doing a least squares fit.
(b) Suppose the points (xi, yi) have been defined in MATLAB/Octave as
X1, ..., X4, Y1, ..., Y4.
Write down the MATLAB/Octave code that plots these points, then computes q(x), and finally plots q(x).
Make sure you understand the problem fully: What is the question asking you to do? Are there specific conditions or constraints that you should take note of? How will you know if your answer is correct from your work only? Can you rephrase the question in your own words in a way that makes sense to you?
If you are stuck, check the hint below. Consider it for a while. Does it give you a new idea on how to approach the problem? If so, try it!
Use the matrix A, and the vector b found in Part (a).
First define A and b as matrix and vector in MATLAB, then use to solve the linear system of the least square equation in MATLAB. From the coefficient vector you can then define the polynomial q(x) and plot as usual.
Checking a solution serves two purposes: helping you if, after having used the hint, you still are stuck on the problem; or if you have solved the problem and would like to check your work.
Found a typo? Is this solution unclear? Let us know here.
Please rate my easiness! It's quick and helps everyone guide their studies.
First, let's define the matrix A and b:
A = [X1^2 X1 1; X2^2 X2 1; X3^2 X3 1; X4^2 X4 1]; b = [Y1; Y2; Y3; Y4];
Next, we solve ATAa = ATb for a:
a = (A'*A)\(A'*b); % or a = inv(A'*A)*A'*b;
The vector a now holds the coefficients of q(x), so we can define the function q(x)
q = @(x) a*x^2 + a*x + a;
and finally plot this anonymous function:
fplot(q, [X1 X4]) % or plot(linspace(X1, X4), q(linspace(X1, X4)))
Note: We assume that the x-values are well-ordered, x1 ≤ x2 ≤ x3 ≤ x4. If this is not the case, replace X1 with min([X1, X2, X3, X4]) and X4 with max([X1, X2, X3, X4]).