Defining variables inside functions
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 15 Jun 2012
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"
More Answers (0)
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!