Undefined variable when performing nonlinear curve fitting

1 回表示 (過去 30 日間)
matnewbie
matnewbie 2019 年 1 月 6 日
編集済み: matnewbie 2019 年 1 月 14 日
I have the following code:
clc,clear all,close all
filename='d5D21Re50.csv';
M=csvread(filename,1,0);
d=5e-3; % particle diameter (m)
U0=10e-3; % superficial velocity (m/s)
eps=0.47; % average porosity
Uavg=U0/eps; % average axial velocity (m/s)
C0=6; % inlet tracer concentration (mM)
cellsize=2.5e-4; % grid size (m)
nx=320; % number of cells in the x (axial) direction
ny=86; % number of cells in the y direction
nz=86; % number of cells in the z direction
L=nx*cellsize; % length of the column (m)
R=0.5*(ny-2)*cellsize; % radius of the column (m)
Ri=0.5e-3; % radius of the tracer tube (m)
flag=reshape(M(:,1),[nx,ny,nz]);
Ux=reshape(M(:,2),[nx,ny,nz]);
Uy=reshape(M(:,3),[nx,ny,nz]);
Uz=reshape(M(:,4),[nx,ny,nz]);
C=reshape(M(:,6),[nx,ny,nz]);
N=29; % number of intervals
radp=1:N;
radgrid=[0 radp];
i=20; % index for the axial position
for m=1:N
jcen=ny/2;
kcen=nz/2;
centerValue=C(i,jcen,kcen);
r1=radgrid(m);
conc=0;
counter=0;
for j=1:ny
for k=1:nz
r=sqrt((j-jcen)^2+(k-kcen)^2);
r2=radgrid(m+1);
if r>=r1 && r<=r2 && (flag(i,j,k)==0 || flag(i,j,k)==0.5)
conc=conc+C(i,j,k);
counter=counter+1;
end
end
end
radCprof(m)=conc/counter;
end
radCprof=[centerValue radCprof];
radialpos=R*radp/max(radp);
myArray=[0 radialpos];
roots=30;
z=i*cellsize;
beta=besselzero(1,roots,1);
value=0;
for i=1:roots
value=value+besselj(1,beta(i)*Ri/R)*besselj(0,beta(i)*r/R)/(beta(i)*(besselj(0,beta(i)))^2)*(z/R)*exp(-beta(i)^2*z/(PeT*R));
end
modelfun=@(PeT,r)C0*(1+(2*R/Ri)*value);
x=lsqcurvefit(modelfun,12,myArray,radCprof);
And I receive the following error when I launch it:
Undefined function or variable 'PeT'.
Error in TransverseDispersionBessel (line 70)
value=value+besselj(1,beta(i)*Ri/R)*besselj(0,beta(i)*r/R)/(beta(i)*(besselj(0,beta(i)))^2)*(z/R)*exp(-beta(i)^2*z/(PeT*R));
How could I solve this problem?

採用された回答

Geoff Hayes
Geoff Hayes 2019 年 1 月 7 日
mathnewbie - the error message is telling you that PeT is undefined. Nowhere in the code have you tried to define this variable until you use it within the following for loop
for i=1:roots
value=value+besselj(1,beta(i)*Ri/R)*besselj(0,beta(i)*r/R)/(beta(i)*(besselj(0,beta(i)))^2)*(z/R)*exp(-beta(i)^2*z/(PeT*R));
end
You will need to initialize this variable before you try and use it... Though this is the same input variable in your anonymous function
modelfun=@(PeT,r)C0*(1+(2*R/Ri)*value);
so you may need to clarify what youa are trying to do. It may be that you want to create a nested function like the following
function myMainFunction
filename='d5D21Re50.csv';
M=csvread(filename,1,0);
d=5e-3; % particle diameter (m)
% etc.
function [x] = myNestedFunction(PeT,r)
value=0;
for i=1:roots
value=value+besselj(1,beta(i)*Ri/R)*besselj(0,beta(i)*r/R)/(beta(i)*(besselj(0,beta(i)))^2)*(z/R)*exp(-beta(i)^2*z/(PeT*R));
end
x = C0*(1+(2*R/Ri)*value);
end
x=lsqcurvefit(@myNestedFunction,12,myArray,radCprof);
end
I haven't tried the above but I hope you get the idea of how your nested function - which has access to all the local variables defined in the main function - can be used for the least squares curve fitting problem...

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 1 月 7 日
Change
value=0;
for i=1:roots
value=value+besselj(1,beta(i)*Ri/R)*besselj(0,beta(i)*r/R)/(beta(i)*(besselj(0,beta(i)))^2)*(z/R)*exp(-beta(i)^2*z/(PeT*R));
end
modelfun=@(PeT,r)C0*(1+(2*R/Ri)*value);
to
value = @(PeT,r) sum( besselj(1,beta(i)*Ri/R)*besselj(0,beta(i)*r/R)/(beta(i)*(besselj(0,beta(i)))^2)*(z/R)*exp(-beta(i)^2*z/(PeT*R));
end
modelfun=@(PeT,r)C0*(1+(2*R/Ri)*value(PeT,r));

カテゴリ

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

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by