Defining variables inside functions
4 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I'm having a lot of trouble getting my MATLAB script to work. It's a function which uses the bisection method to find the solution to an equation. The programme looks like this:
eps_abs=1E-4;
eps_step=2.9E6;
a=550E6;
b=650E6;
while ((b-a)>=eps_step || (abs(neuber2(a))>=eps_abs && abs(neuber2(b))>=eps_abs))
c=(a+b)/2;
if(neuber2(c)==0)
break;
elseif(neuber2(a)*neuber2(c)<0)
b=c
else
a=c
end
end
The function neuber2 is defined in the following MATLAB .m file:
function y = neuber2(DS)
y=(DS/E)+2*(DS/2*k)^(1/n)-(DSe*DEe/DS);
The values E, k, n, DSe and DEe have all been previously calculated in a third .m file and made global. After running this file and calculating the values of E, k, n, DSe and DEe, I then run the programme which performs the bisection method. I instantly get a warning saying that the variables in the function neuber2 are undefined.
Inserting the numerical values of the variables directly into the function makes the bisection mathod script run properly, but that's not how I want it to work, because E, k, n etc. can be different depending on how they're calculated beforehand. So, I want the function neuber2 to have these variables declared symbolically (as shown in the code) and call their current value from the original script that calculated them.
I hope you can help :)
0 个评论
采纳的回答
Walter Roberson
2012-6-15
Variables need to be declared as global in every routine that uses them.
Note: a message about a variable being undefined is an "error", not a "warning"
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!