2015-06-07 37 views
0

當邊界值問題和邊界條件寫入不同的文件並在函數bvp4c中調用時,看起來這些外部文件中命名的變量無法識別。全局變量如何在Matlab中用於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 

給出當更換變量DiffEpsilonRatio分別爲1,3和100,在圖中給出了一個很好的解決方案。

在寫這一點與變量,增加

global Diff Epsilon Ratio 

到主文件,即使,或所有文件,我得到的錯誤,如

未定義的函數或變量「小量」。

處理變量的首選方法是在主文件中定義它們一次。有沒有辦法做到這一點?

+1

「當更換變量」被添加? ['bvp4c'](http://www.mathworks.com/help/matlab/ref/bvp4c.html)是一個數值求解器。所有參數必須有解算器工作的定義。它看起來好像你正確使用['global'](http://www.mathworks.com/help/matlab/ref/global.html?refresh=true);儘管你可能想看看在函數之間共享數據的[更優雅的方式](http://www.mathworks.com/help/matlab/math/parameterizing-functions.html)。 – TroyHaskin

回答

0

除了主要功能,該命令應在「BC」和「BVP」功能太

全球DIFF小量比