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

To compute the best fitting function of the form f(t) = a/t + bt using the least squares method in MATLAB, we can follow these steps:

  1. Define the input data as vectors t and f.
  2. Create a matrix A with two columns, where the first column is 1./t and the second column is t.
  3. Use the backslash operator (i.e., A \ f) to compute the least squares solution x such that Ax is closest to f in a mean squared error sense.
  4. Extract the coefficients a and b from x.
  5. Evaluate the function f at t = -7 using the computed coefficients.

Here's the MATLAB code to implement these steps:

main.m
t = [1, -1, -5, -4, 9, -9];
f = [6, 3, -3, 4, -3, 7];
A = [1./t', t'];
x = A \ f';
a = x(1);
b = x(2);
t_eval = -7;
f_eval = a/t_eval + b*t_eval;
146 chars
9 lines

The computed value of f(-7) is stored in the variable f_eval.

gistlibby LogSnag