MATLAB Matlab and Root Mean Square help

AI Thread Summary
To find the best first-order least squares fit for the given data in MATLAB, use the polyfit function with the syntax Fit1 = polyfit(year, time, 1), which yields coefficients for the linear equation. The resulting coefficients indicate that the best fit line is time = -0.0185*year + 47.6837. To visualize this fit, create a new time vector using the coefficients and plot it over the original data. To calculate the root mean square error, sum the squares of the differences between the actual data and the fitted values, then take the square root of that sum. The standard deviation calculated from this method is approximately 6.7437, which aligns with the visual comparison of the fit and original data.
ndnbolla
Messages
19
Reaction score
0
Matlab and Root Mean Square help please...

So I have this given data:

year = [1928:4:1936 1948:4:1956 1964:4:2000];

time = [12.2 11.9 11.5 11.9 11.5 11.5 11.4 11.08 11.07 11.08 11.06 10.97 10.54 10.82 10.94 10.75];

and after plotting it (time vs. year), I am told to find the best first order least squares fit to the data by a line using the polyfit function.

I am then told to find the root mean square error in this line and then plotting that best line?

How would I go about doing this?

When I did this, I used the function Fit1=polyfit(year, time, 1);

Fit1 came out to equal -0.0185 47.6837 which, when plotted on the same graph as the original data resembled nothing like it.

What am I doing wrong?
 
Physics news on Phys.org
You did everything right, you probably just didnt interpret the answer right

Fit1 is a vector of the polynomial coefficient in descending order

In this case its telling you the best linear fit for the data is
time = -0.0185*year + 47.6837

One easy way of plotting this thing would be to plot your old data first and then make a new time vector corresponding to the linear fit

timefit = Fit1(1)*year + Fit1(2);

hold on;
plot(yearvect,timevect,'g')

This will plot the linear fit over the original data in green

To find the standard deviation we need to sum up the squares of the differences from the linear fit to the actual data, and then take the square root of all that junk.

for n = 1:length(year)
vect_error(n) = abs(time(n)^2 - timevect(n)^2);
end

stan_dev = sqrt(sum(vect_error));

(I get 6.7437 for the standard deviation - which seems reasonable from the plot of the two when comparing)
 
Back
Top