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

2 次查看(过去 30 天)
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
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
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?

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by