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

조회 수: 2 (최근 30일)
Otto
Otto 2012년 10월 27일
답변: Onur Aytan 2015년 10월 24일
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