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 个评论
Jonathan Epperl
2012-11-1
if make_your_code_readable(you)
help(we, you)
else
why
end
Otto
2012-11-1
Jonathan Epperl
2012-11-1
- Paste your code
- Highlight your code
- Hit the "{ } Code" button right above the field your a typing the text into.
Otto
2012-11-1
Jonathan Epperl
2012-11-1
See, much better! Do you get an error, an infinite loop, or simply a wrong result?
Gang-Gyoo
2012-11-2
% 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
2012-11-2
You never change xold in the loop, so this will only work, if test<0 through every iteration.
Otto
2012-11-2
Gang-Gyoo
2012-11-4
you shound put the fprintf statement inside the while statement as
fprintf('%f %f %f \n', fl, fu, xr);
采纳的回答
更多回答(1 个)
Matt Fig
2012-11-2
Why not just use:
R = roots([1 0 0 0 0 0 0 0 0 0 -1]);
类别
在 帮助中心 和 File Exchange 中查找有关 Octave 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!