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

10 ビュー (過去 30 日間)
Tsu Ming Chang
Tsu Ming Chang 2023 年 6 月 2 日
コメント済み: Tsu Ming Chang 2023 年 6 月 2 日
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 件のコメント
Matt J
Matt J 2023 年 6 月 2 日
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 2023 年 6 月 2 日
編集済み: Dyuman Joshi 2023 年 6 月 2 日
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.

サインインしてコメントする。

採用された回答

VBBV
VBBV 2023 年 6 月 2 日
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 件のコメント
VBBV
VBBV 2023 年 6 月 2 日
編集済み: VBBV 2023 年 6 月 2 日
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 2023 年 6 月 2 日
thanks! it's working but how do I output the predicted ellipse area graph?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by