MATLAB: How to use a variable from a script in another script

MATLABroot findingscriptsvariables

I am trying to write a script that demonstrates the methods of finding roots in graphs (regula falsi, bisection and newton raphson) and want to demonstrate the number of steps each one took at the end. Currently i am trying to do this by creating a variable of the number of steps at the end of the functions for example my for regula falsi:
function root = regula_falsi(a,b)
% implementation of the regula falsi method starting
% with x=a and x=b as the brackets
% check that the two points bracket the root
if(~(f(a)*f(b)<0.0))
disp('Root not bracketed!');
return;
end
% set the tolerance: how close to the root
% does the function have to be?
tol = 1.0e-9;
% keep count of the number of steps
step = 1;
while(abs(f(a))>tol && abs(f(b))>tol)
% print out for use
fprintf('step %d, a = %d, b = %d\n',step,a,b)
% find midpoint
m = a - (b-a)/(f(b)-f(a))*f(a);
% which are the new brackets?
if(~(f(a)*f(m)<0.0))
a=m;
else
b=m;
end
step = step + 1;
end
if a==m
fprintf('root is at %d',a)
else
fprintf('root is at %d',b)
end
c=step
however when i try to use the variable c in another script it is not defined so how could i change this so that the variable c is in my workspace

Best Answer

Hi Isaac,
An alternative option for your case is to use assignin() within your function file to take out whichever variable/variables you want to save in your workspace and use them later for your analyses.
Good luck.
Related Question