MATLAB Answers


Calculating Mean Square Error

Asked by Maria
on 19 Apr 2014
Latest activity Commented on by Image Analyst
on 28 Feb 2017
What will be the code of the function for calculating Mean Square Error


Is this your homework? Is so, you were supposed to tag it as homework. It really sounds like homework since real world problems are usually not as easy as this.
What happens when the returned value is NaN? I'm using 2 arrays of observational measurements and then simulation measurements and trying to find the MSE, but upon using this algorithm I get a NaN back.
What do you want to do when there's a nan. You know there is a 'omitnans' option to mean() don't you?

Sign in to comment.


1 Answer

Answer by Image Analyst
on 20 Apr 2014
 Accepted Answer

OK so we know the desired signal, at least you could if you made up the equation for it. But what is the actual signal? Do you have that in some array, perhaps that you read in from some kind of position sensor or image analysis? To calculate MSE you need to have two signals - the desired/true signal, and your actual/test signal. Then just do
MSE = mean((desired - mean).^2);


OK, looks like you need a full blown demo.
% Demo to plot a point's height as it revolves around a circle.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 22;
xCenter = 12;
yCenter = 10;
% Make a timeline of 40 seconds with samples every 0.01 second.
t = 0 : 0.01 : 40;
% Let's say that there is 8 revolutions in that time.
numberOfRevolutions = 8;
% Produce the angles. They will go from 0 to numberOfRevolutions * 2*pi.
theta = linspace(0, numberOfRevolutions * 2 * pi, length(t));
radius = 5;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y, 'LineWidth', 3);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
title('The circlular path it revolves around', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% m = -40;
% velocity = 0.25;
% ft = t;
% azimuth = 2 * 3.14 * ft/m;
% Plot azimuth (the y coordinate) as a function of time.
plot(t, y, 'b-', 'LineWidth', 3);
grid on;
ylim([0, yCenter+radius]);
title('Height of a point as it revolves around', 'FontSize', fontSize);
xlabel('time', 'FontSize', fontSize);
ylabel('Y, or Azimuth', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Dear Mr Image Analyst many thanks to you , I will try it and feed you back . thanks
Mick, not sure what your recent edit was, but if I helped you could you mark the answer as "Accepted"? Thanks.

Sign in to comment.