How to find positive x-root of this function?
Show older comments
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
Jonathan Epperl
on 1 Nov 2012
if make_your_code_readable(you)
help(we, you)
else
why
end
Otto
on 1 Nov 2012
Jonathan Epperl
on 1 Nov 2012
- Paste your code
- Highlight your code
- Hit the "{ } Code" button right above the field your a typing the text into.
Otto
on 1 Nov 2012
Jonathan Epperl
on 1 Nov 2012
See, much better! Do you get an error, an infinite loop, or simply a wrong result?
Gang-Gyoo
on 2 Nov 2012
% 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
Jonathan Epperl
on 2 Nov 2012
You never change xold in the loop, so this will only work, if test<0 through every iteration.
Otto
on 2 Nov 2012
Gang-Gyoo
on 4 Nov 2012
you shound put the fprintf statement inside the while statement as
fprintf('%f %f %f \n', fl, fu, xr);
Accepted Answer
More Answers (1)
Matt Fig
on 2 Nov 2012
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!