필터 지우기
필터 지우기

I need help with this false position script

조회 수: 1 (최근 30일)
AJ516
AJ516 2019년 10월 18일
편집: Stephan 2019년 10월 19일
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)

답변 (1개)

Stephan
Stephan 2019년 10월 19일
편집: Stephan 2019년 10월 19일
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

카테고리

Help CenterFile Exchange에서 Fluid Dynamics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by