Function output variable is unrecognized
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew Palermo
am 25 Nov. 2023
Bearbeitet: Voss
am 25 Nov. 2023
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
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 25 Nov. 2023
Bearbeitet: Dyuman Joshi
am 25 Nov. 2023
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)
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
0 Kommentare
Weitere Antworten (1)
Voss
am 25 Nov. 2023
Bearbeitet: Voss
am 25 Nov. 2023
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)
% feigR13 gives an error when num < 1:
result = feigR13(0,2,1)
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Mathematics and Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!