I need help with this false position script

1 visualización (últimos 30 días)
AJ516
AJ516 el 18 de Oct. de 2019
Editada: Stephan el 19 de Oct. de 2019
I'm not sure where the issue is. Note the percent notes below the script has some commands that need to copy and pasted into the command box.
function root= fp(func,xl,xu,es,max)
if func(xl)*func(xu)>0
error('input error')
end
if nargin<5
max=50;
end
if nargin<4
es=0.001;
end
iter=0;
xr=xl;
while(1)
xrold=xr;
xr=xu-func(xu)*(xl-xu)/(func(xl)-func(xu));
iter=iter+1;
if xr~=0
ea=abs((xr-xrold)/xr)*100;
end
test=func(xl)*func(xr);
if test<0
xu=xr;
elseif test>0
xl=xr;
else
ea=0;
end
if ea<=es||iter>=max
break
end
end
root=xr;
%Type the following commands below
%format long
%fx=@(x)-12-(21*x)+(18*x^2)-(2.75*x^3)
%fp(fx,-1,0,0.001)

Respuestas (1)

Stephan
Stephan el 19 de Oct. de 2019
Editada: Stephan el 19 de Oct. de 2019
No idea what your problem is, code runs for me:
format long
fx=@(x)-12-(21*x)+(18*x^2)-(2.75*x^3)
a =sprintf('%.8f', fp(fx,-1,0,0.001))
function root= fp(func,xl,xu,es,max)
if func(xl)*func(xu)>0
error('input error')
end
if nargin<5
max=50;
end
if nargin<4
es=0.001;
end
iter=0;
xr=xl;
while(1)
xrold=xr;
xr=xu-func(xu)*(xl-xu)/(func(xl)-func(xu));
iter=iter+1;
if xr~=0
ea=abs((xr-xrold)/xr)*100;
end
test=func(xl)*func(xr);
if test<0
xu=xr;
elseif test>0
xl=xr;
else
ea=0;
end
if ea<=es||iter>=max
break
end
end
root=xr;
end

Categorías

Más información sobre Fluid Dynamics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by