Asked by Maria
on 19 Apr 2014

What will be the code of the function for calculating Mean Square Error

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);

Image Analyst
on 20 Apr 2014

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; subplot(1,2,1); 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. subplot(1,2,2); 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]);

Maria
on 21 Apr 2014

Dear Mr Image Analyst many thanks to you , I will try it and feed you back . thanks

Image Analyst
on 9 May 2014

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Image Analyst (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/126373-calculating-mean-square-error#comment_208951

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.

## Christy Lentz (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/126373-calculating-mean-square-error#comment_432466

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.

## Image Analyst (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/126373-calculating-mean-square-error#comment_432515

What do you

wantto do when there's a nan. You know there is a 'omitnans' option to mean() don't you?Sign in to comment.