All functions in a script must be closed with an 'end'.

5 visualizzazioni (ultimi 30 giorni)
hello~
I'm having trouble calculating the prediction ellipse
Can someone help me?
this message error :
Error: File: PEA.m Line: 67 Column: 24
All functions in a script must be closed with an 'end'.
the script such as
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
function a = PEA(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 = PEA(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
return %end of function

Risposta accettata

Matt J
Matt J il 1 Giu 2023
Replace "return" by "end"
  4 Commenti
Matt J
Matt J il 2 Giu 2023
Modificato: Matt J il 2 Giu 2023
It's certainly possible that you still have mistakes in your code, but I've demonstrated that my answer fixes the particular mistake that you asked about in your post. Therefore, you should Accept-click the answer and raise other problems you are having in a different thread.

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 1 Giu 2023

Delete or comment out everything from the "function" line.

  • Your script never invokes the function
  • functions inside a script cannot be reached from outside the script (unless somehow the outside gets a handle to it)
  • your function is named PEA but your script is also named PEA and you assign to a variable named PEA. Once you execute the script PEA the calling context would have a variable PEA and variables with a name have a higher priority than scripts or functions with the same name, so the function would be unreachable anyhow.
  2 Commenti
Tsu Ming Chang
Tsu Ming Chang il 2 Giu 2023
I change it but still wrong
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
Unrecognized function or variable 'x'.
Error in pea_test (line 2)
[vec,val] = eig(cov(x,y)); %calculation of eigenvalues

Accedi per commentare.


Image Analyst
Image Analyst il 1 Giu 2023
Modificato: Image Analyst il 1 Giu 2023
Try this:
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
P = whateverItIs;
a = PEA(x,y,P)
% End of script part of m-file
%====================================================================================
% Now define a function.
function a = PEA(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 = PEA(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
  4 Commenti
Tsu Ming Chang
Tsu Ming Chang il 2 Giu 2023
Modificato: Tsu Ming Chang il 2 Giu 2023
I change my file name call "pea_test.m" but it said this
Error: File: pea_test.m Line: 3 Column: 1
Declaring a variable with the same name as the local function "PEA" is not supported in
scripts.
Image Analyst
Image Analyst il 2 Giu 2023
You didn't notice these lines of code:
a = PEA(x,y,P)
% End of script part of m-file
%====================================================================================
% Now define a function.
function a = PEA(x,y,P)
Note how I took the result of calling PEA and put it into "a"? You didn't do that. You still have
PEA = pi*chisquare*prod(sqrt(svd(val))) %area calculation
You can't define a new variable called PEA when you already have a function of that name. And, as with all computer languages that I know of, the function is on the right side of the = sign, and the result gets put into the variable on the left side of the = sign.

Accedi per commentare.

Categorie

Scopri di più su Trigonometry in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by