当边界值问题和边界条件写入不同的文件并在函数bvp4c中调用时,似乎无法识别这些外部文件中命名的变量。
更具体地说,让我举一个例子,我现在正在处理的问题。主文件是
clearvars
% Creating a grid of x values for MATLAB to solve on and an initial guess for y(x) and y'(x)
solinit=bvpinit(linspace(0,1,100),[1 0]); % solinit = bvpinit(x,yinit) forms the initial guess for a boundary value problem solver.
solution=bvp4c(@bvp,@bc,solinit);
plot(solution.x,solution.y);
边界值probem(bvp.m)是
% Diff*y''+2*i*Epsilon*y=0
% Diff*y2'+2*i*Epsilon*y1=0, y2=y1'
% y1'= y2
% y2'=-2*i*Epsilon/Diff*y1
function yprime=bvp(t,y)
% Treating y1,y2 as components of y, [y1,y2], yprime as their derivative, [y1',y2']
yprime=[y(2);-2*i*Epsilon/Diff*y(1)];
和边界条件(bc.m)由
给出% Boundary conditions
function res=bc(y0,y1)
% Return the residue
res=[-Ratio*y0(2)-y1(1);Ratio*y1(2)-y0(1)]; % The vectors y0 and y1 are the solutions at x=0 and x=1
将变量Diff
,Epsilon
和Ratio
分别替换为1,3和100时,图表中给出了一个很好的解决方案。
使用变量写出来时,即使添加
global Diff Epsilon Ratio
到主文件或所有文件,我都会收到错误,例如
未定义的函数或变量'Epsilon'。
处理变量的首选方法是在主文件中定义一次。有没有办法做到这一点?
答案 0 :(得分:0)
除了main函数之外,还应在'bc'和'bvp'函数中添加此命令
全球差异比率