Matlab and Root Mean Square help

In summary, the conversation discusses how to find the best first order least squares fit for a given dataset using the polyfit function in Matlab. The Fit1 vector contains the polynomial coefficients for the linear fit, and can be used to plot the fit over the original data. To find the root mean square error, the squared differences between the linear fit and the actual data must be summed and then square rooted. This can be used to determine the standard deviation and compare it to the plotted data.
  • #1
ndnbolla
19
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
  • #2
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)
 
  • #3


First of all, great job on using the polyfit function to find the best first order least squares fit for the given data. However, it seems like you may have made a small mistake in plotting the line.

To plot the best fit line, you can use the polyval function with the coefficients obtained from the polyfit function. So in this case, it would be something like y_fit = polyval(Fit1, year).

Now, to calculate the root mean square error (RMSE), you can use the following steps:
1. Calculate the residuals (difference between the actual data points and the predicted values from the best fit line), i.e. res = time - y_fit.
2. Square each residual value, i.e. res_squared = res.^2.
3. Take the mean of the squared residuals, i.e. mean_res_squared = mean(res_squared).
4. Finally, take the square root of the mean squared residuals to get the RMSE, i.e. RMSE = sqrt(mean_res_squared).

This will give you the RMSE for the best fit line. You can then plot this line along with the original data points to see how well it fits.

Hope this helps!
 

1. What is Matlab and how is it used in scientific research?

Matlab is a high-level programming language and interactive environment commonly used in scientific research for data analysis, visualization, and mathematical computations. It allows scientists to easily manipulate and analyze large datasets and perform complex calculations.

2. What is Root Mean Square (RMS) and why is it important?

Root Mean Square is a statistical measure used to determine the average magnitude of a set of values. It is commonly used in signal processing and data analysis to quantify the variability or fluctuations in a dataset. It is important because it provides a better representation of the data compared to other measures, such as the arithmetic mean, especially when dealing with non-linear or highly variable data.

3. How do I calculate RMS using Matlab?

To calculate RMS in Matlab, you can use the 'rms' function. Simply input the data as a vector or matrix, and the function will return the RMS value. Alternatively, you can use the formula 'sqrt(sum(x.^2)/n)' where 'x' is the data and 'n' is the number of elements in the data.

4. Can Matlab be used for time series analysis?

Yes, Matlab has built-in functions and tools specifically designed for time series analysis. These include functions for data preprocessing, trend analysis, and forecasting. Additionally, there are various toolboxes available for more advanced time series analysis techniques.

5. Is it possible to plot RMS values in Matlab?

Yes, you can plot RMS values in Matlab using the 'plot' function. First, calculate the RMS values for your dataset, then use the 'plot' function to create a line plot. You can also customize the plot by adding labels, titles, and adjusting the axes.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
19K
Replies
1
Views
6K
  • Calculus and Beyond Homework Help
Replies
3
Views
10K
  • Introductory Physics Homework Help
Replies
3
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
15K
  • Introductory Physics Homework Help
Replies
1
Views
2K
  • STEM Educators and Teaching
Replies
11
Views
31K
Back
Top