MATLAB: How to find positive x-root of this function

root estimation

Hi All,
I want to find positive root x of this function;
x^10=1;
I wrote a program using modified false postion algorythm but i don't know how to use it to solve equation written above. Here is the Code;
function ModFalsePos=eqn(xl,xu,es,xr,ea)
f=@(x)x^10-1
xl=0
xu=1.3
es=0.01
fl=f(xl)
fu=f(xu)
while (1)
xr=xu-fu*(xl-xu)/(fl-fu)
xrold=xr
fr=f(xr)
if xr<0
elseif xr>0
ea=abs((xr-xold)/xr)*100
end
test=fl*fr
if test<0
xu=xr
fu=f(xu)
iu=0
il=il+1
if il>=2
fl=fl/2
end
elseif test>0
xl=xr
fl=f(xl)
il=0
iu=iu+1
if iu>=2
fu=fu/2
end
else
ea=0
end
if ea<es
break
end
end
ModFalsePos=xr
end
Could anyone please help me for solving this equation using this code? What's wrong here?
Thanks for any Help!

Best Answer

Okay, in this line here:
ea=abs((xr-xold)/xr)*100
ea is always going to be zero, since a few lines above, you assigned xold=xr. Thus your algorithm always exits after the first iteration. So you need to flip the first lines after while:
xrold = xr;
xr = xu - fu*(xl-xu)/(fl-fu);
Then, you need to initialize iu, il and xr outside of the loop.
xu=1.3;
xr=xu;
iu = 0; il = 0;
Also, remove the function line and the last end, what you have here is a script, not a function. If you want it to be a function, at least f should be an input argument, too, otherwise it doesn't make much sense.
And now it should be running.
Related Question