Open In App

RMSE – Root Mean Square Error in MATLAB

Last Updated : 18 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

RMSE or Root Mean Squared Error is a general-purpose error estimation that is calculated by computing the square root of the summation of the square of the difference of the prediction of an experiment and its actual/expected value. RMSE is a good error estimation as it tells us how far a line is fit from its actual value. Fields such as Machine Learning and Numerical Analysis have extensive requirements if this measure of error calculation. Let us see how the same can be done in MATLAB. 

Method 1: Manually Calculating the RMSE

The mathematical formula for calculating RMSE is:

RMS = \sqrt{\sum _{N}^{i-0}\left [ Predicated i - Actual i \right ]^{2}}/ N 

Example 1:

Matlab

% The code for calculating the same in MATLAB 
% Data
    expected = [31 23 14 10.5 6.5];
    experimental = [32.5 21.9 15.1 9 5.2];
  
% Calculating rmse
    diff = sum((experimental - expected).^2);
    rm = sqrt(diff/5);
    disp(rm)

                    

Output:

 

Method 2: Calculating RMSE with RMS() 

There is an easier way of doing this in a single step i.e., with the inbuilt RMS() function which takes the error as input and calculates the RMSE of the same.

Matlab

% MATLAB code for RMS()
  expected = [31 23 14 10.5 6.5];
  experimental = [32.5 21.9 15.1 9 5.2];
  
% Error vector
    diff = experimental-expected;
  
% Using the rms() function
    r = rms(diff)

                    

Output:

 

As can be seen in the above two methods that the value of RMSE is the same when calculated manually and when calculated by the built-in RMS() function. So, depending on the user’s choice, either of the methods could be used.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads