Function output variable is unrecognized

2 ビュー (過去 30 日間)
Matthew Palermo
Matthew Palermo 2023 年 11 月 25 日
編集済み: Voss 2023 年 11 月 25 日
So in this function, which is part of a larger problem, I am trying to determine the eigenvalues that I need. As far as I am aware, the output variable BB should simply return the eigenvalues as an array, but I keep getting the error that the varaible "eig" is undefined. I have tried moving the definition around and in and out of th for loops, but I keep getting this error. I don't think I need to paramterize it at the beginning. ANy help is appreciated.
Thanks!
Here is the function:
%feigR13.m
%March 24, 2014.
%R is the ratio R2/R1, num =number of eigenvalues, Bi=Biot number
% R must be greater than 1
%Author: Satish Nallapaneni
function BB = feigR13(num,R,Bi)
x=R-1;
Cc=Bi*x;
p=1.5;
for j= 1:num
R11=j*pi/x+0.155*(x)*(j-2)/j^2/(x+2.32)^1.8- 4*(j-1)*(0.062*x/(x+2)^1.56-0.0011)/j^2;
R12=(j-1/2)*pi/(x)- ((0.305+0.01*x^0.2+0.001*x)/(1+0.5*x)-0.12*(0.2*x^1)/(x^2+1.4)+0.067*x/(x^2+49))/j^1.5;
lambi=R11*Cc^p/(j*pi+Cc^p)+j*pi*R12/(j*pi+Cc^p);
lambi=roundn((lambi*10^10)/(10^10),-20);
x0 = lambi; dx=pi/x/10;
for pp=1:6
UL = (x0 + dx);
LL = (x0 - dx);
ML = (UL + LL)/2;
f0=(-ML*bessely(1,ML*R)+Bi*bessely(0,ML*R))*besselj(0,ML)-(-ML*besselj(1,ML*R)+Bi*besselj(0,ML*R))*bessely(0,ML);
f1=(-LL*bessely(1,LL*R)+Bi*bessely(0,LL*R))*besselj(0,LL)-(-LL*besselj(1,LL*R)+Bi*besselj(0,LL*R))*bessely(0,LL);
f2=(-UL*bessely(1,UL*R)+Bi*bessely(0,UL*R))*besselj(0,UL)-(-UL*besselj(1,UL*R)+Bi*besselj(0,UL*R))*bessely(0,UL);
fp = (f2 - f1)/2/dx;
fpp = (f1 + f2 - 2*f0)/dx^2;
h = -f0/fp;
eps=-fpp*h^2/2/(fp + h*fpp);
x0 = x0 + h + eps;
dx = h^2;
if dx<10^-10
break
end
end
eig(j)=x0;
end
BB=eig;
end

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 25 日
編集済み: Dyuman Joshi 2023 年 11 月 25 日
eig is a built-in function in MATLAB. And there are many other toolbox functions, with the same name.
It's not a good idea to name variables (and scripts and user-defined functions for that matter) with existing function names. You should rename the variable.
You can also directly use the output variable, see below. And if you are working with large values of num, consider preallocating the output variable.
The function runs without an error for the given input.
If you still encounter an error, please provide the values used to call the function.
%Sample values
num = 100;
R = 1.5;
Bi = 6.9;
%Call the function
BB = feigR13(num,R,Bi)
BB = 1×100
4.9695 10.5316 16.4619 22.5540 28.7210 34.9268 41.1551 47.3975 53.6491 59.9072 66.1700 72.4363 78.7052 84.9763 91.2490 97.5230 103.7981 110.0742 116.3510 122.6284 128.9064 135.1849 141.4639 147.7431 154.0227 160.3026 166.5827 172.8631 179.1436 185.4243
function BB = feigR13(num,R,Bi)
x=R-1;
Cc=Bi*x;
p=1.5;
%Preallocation
BB = zeros(1,num);
for j= 1:num
R11=j*pi/x+0.155*(x)*(j-2)/j^2/(x+2.32)^1.8- 4*(j-1)*(0.062*x/(x+2)^1.56-0.0011)/j^2;
R12=(j-1/2)*pi/(x)- ((0.305+0.01*x^0.2+0.001*x)/(1+0.5*x)-0.12*(0.2*x^1)/(x^2+1.4)+0.067*x/(x^2+49))/j^1.5;
lambi=R11*Cc^p/(j*pi+Cc^p)+j*pi*R12/(j*pi+Cc^p);
lambi=roundn((lambi*10^10)/(10^10),-20);
x0 = lambi; dx=pi/x/10;
for pp=1:6
UL = (x0 + dx);
LL = (x0 - dx);
ML = (UL + LL)/2;
f0=(-ML*bessely(1,ML*R)+Bi*bessely(0,ML*R))*besselj(0,ML)-(-ML*besselj(1,ML*R)+Bi*besselj(0,ML*R))*bessely(0,ML);
f1=(-LL*bessely(1,LL*R)+Bi*bessely(0,LL*R))*besselj(0,LL)-(-LL*besselj(1,LL*R)+Bi*besselj(0,LL*R))*bessely(0,LL);
f2=(-UL*bessely(1,UL*R)+Bi*bessely(0,UL*R))*besselj(0,UL)-(-UL*besselj(1,UL*R)+Bi*besselj(0,UL*R))*bessely(0,UL);
fp = (f2 - f1)/2/dx;
fpp = (f1 + f2 - 2*f0)/dx^2;
h = -f0/fp;
eps=-fpp*h^2/2/(fp + h*fpp);
x0 = x0 + h + eps;
dx = h^2;
if dx<10^-10
break
end
end
%Assign directly to output variable
BB(j)=x0;
end
end

その他の回答 (1 件)

Voss
Voss 2023 年 11 月 25 日
編集済み: Voss 2023 年 11 月 25 日
Calling the function feigR13 with the first input (i.e., the variable num) less than 1 will produce that error because the outer for loop will not iterate so the variable eig will not be defined.
% feigR13 runs when num >= 1:
result = feigR13(3,2,1)
result = 1×3
1.9153 4.8532 7.9404
% feigR13 gives an error when num < 1:
result = feigR13(0,2,1)
Unrecognized function or variable 'eig'.

Error in solution>feigR13 (line 42)
BB=eig;
So your code may be calling feigR13 with a number less than 1 as its first input. You should check why that's happening and how to prevent it, or, if you want to allow num to take some value(s) less than 1 (in particular, say, zero), then you can pre-allocate eig before the outer for loop, e.g.:
eig = zeros(1,num);
Then eig will be defined for any given valid num and you won't get this error. In particular, eig will be empty when num is less than or equal to 0. (It's a good idea to pre-allocate for other reasons as well; see: https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html)
% define the function feigR13:
%feigR13.m
%March 24, 2014.
%R is the ratio R2/R1, num =number of eigenvalues, Bi=Biot number
% R must be greater than 1
%Author: Satish Nallapaneni
function BB = feigR13(num,R,Bi)
x=R-1;
Cc=Bi*x;
p=1.5;
for j= 1:num
R11=j*pi/x+0.155*(x)*(j-2)/j^2/(x+2.32)^1.8- 4*(j-1)*(0.062*x/(x+2)^1.56-0.0011)/j^2;
R12=(j-1/2)*pi/(x)- ((0.305+0.01*x^0.2+0.001*x)/(1+0.5*x)-0.12*(0.2*x^1)/(x^2+1.4)+0.067*x/(x^2+49))/j^1.5;
lambi=R11*Cc^p/(j*pi+Cc^p)+j*pi*R12/(j*pi+Cc^p);
lambi=roundn((lambi*10^10)/(10^10),-20);
x0 = lambi; dx=pi/x/10;
for pp=1:6
UL = (x0 + dx);
LL = (x0 - dx);
ML = (UL + LL)/2;
f0=(-ML*bessely(1,ML*R)+Bi*bessely(0,ML*R))*besselj(0,ML)-(-ML*besselj(1,ML*R)+Bi*besselj(0,ML*R))*bessely(0,ML);
f1=(-LL*bessely(1,LL*R)+Bi*bessely(0,LL*R))*besselj(0,LL)-(-LL*besselj(1,LL*R)+Bi*besselj(0,LL*R))*bessely(0,LL);
f2=(-UL*bessely(1,UL*R)+Bi*bessely(0,UL*R))*besselj(0,UL)-(-UL*besselj(1,UL*R)+Bi*besselj(0,UL*R))*bessely(0,UL);
fp = (f2 - f1)/2/dx;
fpp = (f1 + f2 - 2*f0)/dx^2;
h = -f0/fp;
eps=-fpp*h^2/2/(fp + h*fpp);
x0 = x0 + h + eps;
dx = h^2;
if dx<10^-10
break
end
end
eig(j)=x0;
end
BB=eig;
end

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by