Sunday, 15 February 2015

Breaking out of recursive function calls in matlab -



Breaking out of recursive function calls in matlab -

the next simple illustration generalize , illustrate problem having.

if have function following:

function newtraph(initialguess,funct,dfunct) ht = funct(initialguess); if abs(ht) < 10^(-6) disp(initialguess); homecoming elseif abs(ht) > 10^6 disp('fix guess'); homecoming end newtraph(initialguess-(ht/dfunct(initialguess)), funct, dfunct);

the way (that aware of) exit out through utilize of homecoming statements. but, want assign output functions of variety variables in base of operations workspace. want thing like:

function out = newtraph(initialguess,funct,dfunct) ht = funct(initialguess); if abs(ht) < 10^(-6) out = initialguess; homecoming elseif abs(ht) > 10^6 disp('fix guess'); homecoming end newtraph(initialguess-(ht/dfunct(initialguess)), funct, dfunct);

this doesn't work, return prevents out beingness assigned.

output argument "out" (and maybe others) not assigned

some ideas have solution using globals or evalin. there simpler way missing. want pass output functions of style base of operations workspace?

a test case, in case:

funct=@(x) -x-cos(x); dfunct=@(x) sin(x)-1; initialguess=1;

thanks time.

well, idiot. case of forgetting final assignment:

function out = newtraph(initialguess,funct,dfunct) ht = funct(initialguess); if abs(ht) < 10^(-6) %tolerance out = initialguess; homecoming elseif abs(ht) > 10^6 out=0; homecoming end out = newtraph(initialguess-(ht/dfunct(initialguess)), funct, dfunct);

thanks quick help!

your illustration function doesn't work there. need assign

out = newtraph(...)

on lastly line can capture output.

you need assign out = 0 or dummy value when study "fix guess" branch of code homecoming value.

matlab

No comments:

Post a Comment