Azzera filtri
Azzera filtri

How to find positive x-root of this function?

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Otto
Otto il 2 Nov 2012
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!
Gang-Gyoo
Gang-Gyoo il 4 Nov 2012
you shound put the fprintf statement inside the while statement as
fprintf('%f %f %f \n', fl, fu, xr);

Accedi per commentare.

Risposta accettata

Jonathan Epperl
Jonathan Epperl il 1 Nov 2012
Modificato: Jonathan Epperl il 1 Nov 2012
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 Commenti
Otto
Otto il 2 Nov 2012
what you suggested caused an infinite loop. and all I get is zeros again. what should I do now?
Thank you for your interest!
Jonathan Epperl
Jonathan Epperl il 2 Nov 2012
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

Accedi per commentare.

Più risposte (1)

Matt Fig
Matt Fig il 2 Nov 2012
Why not just use:
R = roots([1 0 0 0 0 0 0 0 0 0 -1]);

Categorie

Scopri di più su Financial Toolbox in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by