Defining variables inside functions

4 views (last 30 days)
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 :)

Accepted Answer

Walter Roberson
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"
  1 Comment
fsgeek
fsgeek on 15 Jun 2012
OK. I tried declaring them as global in every routine but it still wasn't working. I have combined everything into a single .m file using the bisection function as an anonymous function.
function [Sa Ea DS DE] = neuberfun(Sae,E,k,n)
%Reads elastic data and performs the Neuber correction on it
%The function call is neuberfun(Sae,E,k,n)
DSe=2*Sae; Eae=Sae/E; DEe=DSe/E;
%Newton's method on cyclic stress-strain curve
an=0; S0=200E6; error=1;
while abs(error)>=0.0001
an= S0-((S0/E)+(S0/k)^(1/n)-(Sae*Eae/S0))/((1/E)+((S0/k)^((1/n)-1))/(k*n)+(Sae*Eae)/(S0)^2);
error=S0-an;
S0=an;
end
Sa=an;
Ea=(Sae*Eae)/Sa;
%FYI
fprintf('The true elastic-plastic stress and strain is %f Pa and %f.\n',Sa,Ea)
%Bisection method on hysteresis loop stress-strain curve
bisect=@(DS)(DS/E)+2*(DS/2*k)^(1/n)-(DSe*DEe/DS);
eps_abs=1E-4;
eps_step=2.9E6;
a=550E6;
b=650E6;
while ((b-a)>=eps_step || (abs(bisect(a))>=eps_abs && abs(bisect(b))>=eps_abs))
c=(a+b)/2;
if(bisect(c)==0)
break;
elseif(bisect(a)*bisect(c)<0)
b=c;
else
a=c;
end
end
DS=a;
DE=(DSe*DEe)/DS;
fprintf('The true elastic-plastic stress-strain range across the closed hysteresis loop is %f Pa and %f.\n',DS,DE);
end
It now asks the user to input the variables bisection variables E, k and n. DSe and DEe are then calculated from these inputs. Since everything is now in one file I haven't used global variables.
When I run the script like this, the bisection method loops for ever and I have to use ctrl+c to stop, but ff I put the values of E, k, n, DSe and DEe directly into the anonymous equation it works fine :s Really don't know why it doesn't work if the anonymous function is written out symbolically, because the bisection method never alters the variables in any way except for DS.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!