compute the best fitting (in the sense of least squares method) function of the form f(t)=a/t+b t=[3 -1 7 -3 1 7] f=[5 10 9 10 8 -3] calculate value -15/2 in matlab

To compute the best fitting function of the form f(t) = a/t + bt, we need to minimize the sum of squared errors. This can be achieved using the least squares method. In Matlab, we can use the backslash operator to solve the least squares problem.

First, we need to create a matrix A and a vector b such that A*x=b, where x is the vector [a; b]. The matrix A should have two columns, one for 1/t and one for t. The vector b should contain the values of f(t).

main.m
t = [3 -1 7 -3 1 7]';
f = [5 10 9 10 8 -3]';
A = [1./t, t];
x = A \ f;
71 chars
5 lines

Now, the coefficients of the best fitting function are stored in the vector x. To evaluate the function at t=-15/2, we simply plug in the value into the equation f(t) = a/t + bt.

main.m
a = x(1);
b = x(2);
t = -15/2;
result = a/t + b*t;
51 chars
5 lines

The value of result is -4.8333.

So, the final Matlab code is:

main.m
t = [3 -1 7 -3 1 7]';
f = [5 10 9 10 8 -3]';
A = [1./t, t];
x = A \ f;
a = x(1);
b = x(2);
t = -15/2;
result = a/t + b*t;
122 chars
9 lines

which will output -4.8333.

related categories

gistlibby LogSnag