Root Mean Square Error tutorial – MATLAB

Last updated on February 4th, 2014

Here’s how to calculate the root mean square error.

Assume you have one set of numbers that represent the Actual values you want to predict.

Actual = [1 2 3 4];

Then assume you have another set of numbers that Predicted the actual values.

Predicted = [1 3 1 4];

How do you evaluate how close Predicted values are to the Actual values?

Well you could use the root mean square error (RMSE) to give a sense of the Predicted values error.

Here’s some MATLAB code that does exactly that.

% rmse tutorial.
 
% The actual values that we want to predict.
Actual = [1 2 3 4];
 
% The values we actually predicted.
Predicted = [1 3 1 4]; 
 
% One way is to use the Root Mean Square function and pass in the "error" part.
rmse = rms(Predicted-Actual)
 
% That's it! You're done. 
% But for those of you who are the curious type, 
% here's how to calculate the root-mean-square-error by hand.
 
% First calculate the "error".
err = Actual - Predicted;
 
% Then "square" the "error".
squareError = err.^2;
 
% Then take the "mean" of the "square-error".
meanSquareError = mean(squareError);
 
% Then take the "root" of the "mean-square-error" to get 
% the root-mean-square-error!
 
rootMeanSquareError = sqrt(meanSquareError)
 
% That's it! You have calculated the RMSE by hand.
 
% So, this is true.
rootMeanSquareError == rmse

7 thoughts on “Root Mean Square Error tutorial – MATLAB”

Questions/comments? If you just want to say thanks, consider sharing this article or following me on Twitter!