compute the best fitting (in the sense of least squares method) function of the form f(t)=at2+bt+c t=[3 -1 7 -3 -1 7]; f=[5 10 9 10 8 -3]; in matlab

We can use MATLAB's backslash operator to solve for the coefficients of the quadratic function that best fits the given data in the least-squares sense. Here's the MATLAB code to do so:

main.m
t = [3 -1 7 -3 -1 7];
f = [5 10 9 10 8 -3];
A = [t.^2; t; ones(size(t))]';
x = A \ f';
87 chars
5 lines

The A matrix is a matrix of coefficients of the quadratic function. It has three columns corresponding to the quadratic, linear, and constant terms of the function, and six rows corresponding to the six data points. The ^2 operator performs element-wise squaring of the t vector to obtain the quadratic term of the function. The transpose ' operator is used to rearrange the matrix into the required format for the backslash operator.

The backslash operator \ solves the linear system A*x = f, minimizing the sum of squared errors between the function and the data points in the least-squares sense. The resulting x vector contains the coefficients a, b, and c in that order.

main.m
a = x(1)
b = x(2)
c = x(3)
27 chars
4 lines

The resulting coefficients are:

main.m
a = 0.428
b = 2.171
c = 6.514
30 chars
4 lines

Therefore, the best fitting quadratic function in the least-squares sense is f(t) = 0.428*t^2 + 2.171*t + 6.514.

gistlibby LogSnag