how to use this m-file to solve an equation?

Hi All,
I'm a very beginner in MATLAB. I've written a program using modified false position method to calculate positive x-value that satisfies x^10=1 equation but I don't know how to get this result using this m-file. should I use fsolve or some another command?
Here is the code;
function ModFalsePos=eqn(xl,xu,es,xr,ea)
f=@(x)x^10
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
initial guesses are xl=0 and xu=1.3, is that code true or something wrong in this code too?
Thanks for any help!

1 件のコメント

Jan
Jan 2012 年 10 月 27 日
編集済み: Jan 2012 年 10 月 27 日
Hi Otto. Welcome to this forum. To make your code readable, you can apply a simple code formatting method: 1 empty line before and after the code, 2 leading spaces in each line.
Do you have the impression, that your code is wrong? If so, what detail let you assume this?

サインインしてコメントする。

回答 (2 件)

Gang-Gyoo
Gang-Gyoo 2012 年 10 月 28 日

0 投票

This is the Bisection method for finding the solution
function test x= eqn(@(x)x^10-1,0,1.3,1e-6,50) end
function x2= eqn(fun,x0,x1,eps,nmax)
for i= 1: nmax
x2= (x0+x1)/2;
if abs(x2-x1) <= eps
return;
end
fx0= fun(x0);
fx2= fun(x2);
if fx0*fx2 < 0
x1= x2;
else
x0= x2;
end
end
disp('Completed unsuccessfully');
end

1 件のコメント

Otto
Otto 2012 年 10 月 28 日
Thank you for your interest, but solving this problem with modified false position method is mandatory. So, I've written this code using Modified false position algorythm but I don't know how to use it to solve x^10=1 and not sure if the code is correct.
Thanks for your interest again!

サインインしてコメントする。

Onur Aytan
Onur Aytan 2015 年 10 月 24 日

0 投票

Hello, I too need help for the same question, which is originally: f(x)=x^10-1, locate the root between x=0 and x=1.3 using the modified false position method. I worked on it but could not possibly run without any problem, I can't deal with programming stuff. It has a mind blowing complexity,:/ Please can anybody help for this problem as this question becomes more interesting for more people to be solved?

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2012 年 10 月 27 日

回答済み:

2015 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by