How to find positive x-root of this function?

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!

10 Comments

if make_your_code_readable(you)
help(we, you)
else
why
end
I'm beginner in this forum. I don't know how to make my code "readable". Could you please help me also for this?
  • Paste your code
  • Highlight your code
  • Hit the "{ } Code" button right above the field your a typing the text into.
ok now you can see the code clearly.
See, much better! Do you get an error, an infinite loop, or simply a wrong result?
Otto
Otto on 1 Nov 2012
Edited: Otto on 1 Nov 2012
I want to get a loop that stops when the error criterion is met. (if Ea<Es) and finding results while initial guesses are xl=0,xu=1.3 let say xr1=.. xr2=...,fu1=... fu2=..., continious results until criterion is met. but i can't get this results. if you copy this code to your MATLAB screen you will understand what i mean.
% I moved two statements before 'while'. The code results the answer
% (x= 1) but you still check your code whether is as same as
% the algorithm.
function main
ModFalsePos= eqn(0,1.01,0.01)
end
function ModFalsePos=eqn(xl,xu, es)
f=@(x)x^10-1;
fl=f(xl);
fu=f(xu);
xold= xl;
iu=0;
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);
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
You never change xold in the loop, so this will only work, if test<0 through every iteration.
this code finds the root of equation ModFalsePos=1 but i want to see the change in other results, for example fl,fu,iteration number,xr, xl,xu in tabulated form. this code gives just the root of polynomial. What i want to see is "change" in parameters until the criterion is met.
Thank you for your interest!
you shound put the fprintf statement inside the while statement as
fprintf('%f %f %f \n', fl, fu, xr);

Sign in to comment.

 Accepted 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.

2 Comments

what you suggested caused an infinite loop. and all I get is zeros again. what should I do now?
Thank you for your interest!
Ah yeah, I also forgot that I had to change this line
ea=abs((xr-xold)/xr)*100
to this
ea=abs((xr-xrold)/xr)*100
assuming that you just made a typo, calling on xold instead of xrold. Here is the entire code, and it runs. If you want more outputs, then just remove the semicolon ";" from the end of the respective line.
Another thing that you didn't do: Initialize ea.
Without further ado, here's my complete code, that should really run now, with different values for xl and xu:
f=@(x)x^10-1
xl=-.5
xu=14
xr = xu;
es=0.01
fl=f(xl)
fu=f(xu)
ea = inf;
iu = 0; il = 0;
while (1)
xrold = xr;
xr = xu - fu*(xl-xu)/(fl-fu)
fr = f(xr);
if xr<0
elseif xr>0
ea=abs((xr-xrold)/xr)*100 % relative change in xr?
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

Sign in to comment.

More Answers (1)

Why not just use:
R = roots([1 0 0 0 0 0 0 0 0 0 -1]);

Categories

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