How to calculate the predicted ellipse area and draw the graph?

10 views (last 30 days)
I use this code but there is an error in execution
chisquare = chi2inv(0.95,2); %inverse of the chi-square cumulative distribution function with 2 degrees of freedom at P = 0.95
[vec,val] = eig(cov(x,y)); %calculation of eigenvalues
pea = pi*chisquare*prod(sqrt(svd(val))) %area calculation
% End of script part of m-file
%====================================================================================
% Now define a function.
function a = ComputePEA(x,y,P)
%
%a = PEA(x,y,P): plots a prediction ellipse of the center of
%pressure (COP) data separated into x and y components with probability
%value P and calculates the prediction ellipse area (PEA)
%
%inputs:
% x,y: data (column vectors)
% P : a value of the interval (0,1)
%
%output:
% a : represents the area of the ellipse
%
%type PEA without inputs to plot 95% prediction ellipse of exemplary data
%%
%exemplary data illustration
if nargin==0 %case of no input arguments
x = [2,4,6,2,3,4,2,3,3,4,5,5,0,8,3,7]; %exemplary x component
y = [1,2,3,4,3,3,1,1,2,1,4,5,0,0,1,1]; %exemplary y component
a = ComputePEA(x,y,0.95);
axis([-4 11 -3 7])
title('95% PEA of exemplary data')
text(-3,6,'blue points = data')
text(5.5,2,'major axis')
text(4.3,0,'minor axis')
text(7,-2,['PEA: ' num2str(a)])
return %end of function
end
%%
%begin of function
chisquare = chi2inv(P,2); %inverse of the chi-square cumulative distribution function with 2 degrees of freedom at P
x = x(isfinite(x));
y = y(isfinite(y));
mx = mean(x);
my = mean(y);
[vec,val] = eig(cov(x,y)); %calculation of eigenvalues
a = pi*chisquare*prod(sqrt(svd(val))); %area calculation
hold on
%COP data
plot(x,y,'b.');
%ellipse
N = 100; %fixed value (the higher the smoother the ellipse)
t = linspace(0,2*pi,N);
elip = sqrt(chisquare)*vec*sqrt(val)*[cos(t); sin(t)] + repmat([mx; my],1,N);
elip = elip';
line(elip(:,1),elip(:,2),'Color', [0 0 0], 'LineWidth', 1);
%major and minor axes
ax1 = sqrt(chisquare)*vec*sqrt(val)*[-1,1; 0,0] + repmat([mx; my],1,2);
ax2 = sqrt(chisquare)*vec*sqrt(val)*[0,0; -1,1] + repmat([mx; my],1,2);
ax_dat = [ax1'; NaN,NaN; ax2'];
line(ax_dat(:,1),ax_dat(:,2),'Color',[0 0 0],'LineWidth', 1);
axis equal
end % end of function
and then
Unrecognized function or variable 'x'.
Error in pea_test (line 2)
[vec,val] = eig(cov(x,y)); %calculation of eigenvalues
  2 Comments
Matt J
Matt J on 2 Jun 2023
Neither x or y is defined in the code you've posted, so the error message seems to make a lot of sense.
Dyuman Joshi
Dyuman Joshi on 2 Jun 2023
Edited: Dyuman Joshi on 2 Jun 2023
The error is quite clear. You have not defined "x" (nor "y" as well).
You are asking a function to calculate the output but you have not defined the input.

Sign in to comment.

Accepted Answer

VBBV
VBBV on 2 Jun 2023
chisquare = chi2inv(0.95,2); %inverse of the chi-square cumulative distribution function with 2 degrees of freedom at P = 0.95
x = [2,4,6,2,3,4,2,3,3,4,5,5,0,8,3,7]; %exemplary x component
y = [1,2,3,4,3,3,1,1,2,1,4,5,0,0,1,1]; %exemplary y component
[vec,val] = eig(cov(x,y)); %calculation of eigenvalues
pea = pi*chisquare*prod(sqrt(svd(val))) %area calculation
pea = 57.4753
% End of script part of m-file
%====================================================================================
% Now define a function.
function a = ComputePEA(x,y,P)
%
%a = PEA(x,y,P): plots a prediction ellipse of the center of
%pressure (COP) data separated into x and y components with probability
%value P and calculates the prediction ellipse area (PEA)
%
%inputs:
% x,y: data (column vectors)
% P : a value of the interval (0,1)
%
%output:
% a : represents the area of the ellipse
%
%type PEA without inputs to plot 95% prediction ellipse of exemplary data
%%
%exemplary data illustration
if nargin==0 %case of no input arguments
x = [2,4,6,2,3,4,2,3,3,4,5,5,0,8,3,7]; %exemplary x component
y = [1,2,3,4,3,3,1,1,2,1,4,5,0,0,1,1]; %exemplary y component
a = ComputePEA(x,y,0.95);
axis([-4 11 -3 7])
title('95% PEA of exemplary data')
text(-3,6,'blue points = data')
text(5.5,2,'major axis')
text(4.3,0,'minor axis')
text(7,-2,['PEA: ' num2str(a)])
return %end of function
end
%%
%begin of function
chisquare = chi2inv(P,2); %inverse of the chi-square cumulative distribution function with 2 degrees of freedom at P
x = x(isfinite(x));
y = y(isfinite(y));
mx = mean(x);
my = mean(y);
[vec,val] = eig(cov(x,y)); %calculation of eigenvalues
a = pi*chisquare*prod(sqrt(svd(val))); %area calculation
hold on
%COP data
plot(x,y,'b.');
%ellipse
N = 100; %fixed value (the higher the smoother the ellipse)
t = linspace(0,2*pi,N);
elip = sqrt(chisquare)*vec*sqrt(val)*[cos(t); sin(t)] + repmat([mx; my],1,N);
elip = elip';
line(elip(:,1),elip(:,2),'Color', [0 0 0], 'LineWidth', 1);
%major and minor axes
ax1 = sqrt(chisquare)*vec*sqrt(val)*[-1,1; 0,0] + repmat([mx; my],1,2);
ax2 = sqrt(chisquare)*vec*sqrt(val)*[0,0; -1,1] + repmat([mx; my],1,2);
ax_dat = [ax1'; NaN,NaN; ax2'];
line(ax_dat(:,1),ax_dat(:,2),'Color',[0 0 0],'LineWidth', 1);
axis equal
end % end of function
  2 Comments
VBBV
VBBV on 2 Jun 2023
Edited: VBBV on 2 Jun 2023
As the error states, Unrecognized function or variable 'x'.
define the example values for x, y before using them in any function
Tsu Ming Chang
Tsu Ming Chang on 2 Jun 2023
thanks! it's working but how do I output the predicted ellipse area graph?

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!