Given the following datapoints
I'm trying to find the best fitting model using the method of least squares.
Two models are given.
My approach was to rewrite the to equations into the following.
I wrote the following MATLAB code to compute the coefficients a,b for the different equations.
For the first equation I wrote the following code to evaluate the coefficient a
x = [150 200 300 500 1000 2000]';
y = [2 3 4 5 6 7]';
func =@(x) (1/x-1/8);
yy=arrayfun(func,y);
A = 1./x;
c= A\yy; yanp= A*c; error = yy-yanp;
rms(error) % Root mean square error.
Giving me a= 48.4692 with a root mean square error of 0.0310.
For the second equation I wrote the following code to evaluate the coefficients a,b.
x = [150 200 300 500 1000 2000]';
y = [2 3 4 5 6 7]';
yy = log(8-y);
A = [ones(6,1) log(x)];
c= A\yy; yanp= A*c; error= yy-yanp;
a = exp(c(1)); %Converting back
b= c(2);
rms(error)
Giving me the a=174.5247, b= -0.6640 with a root mean square error of 0.0756
My results suggests that the first equation is the better approximation since the error is smaller, however my fellow students claim that the second equation gives the smaller error and hence is the better approximation. I suspect I've made a mistake somewhere in my calculations and I'm looking for guidance.
In your second case you''re not computing your error correctly. You need to convert yanp
back to "true units" and compare with the input y
:
error = y-(8-exp(yanp));