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
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
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
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?
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!