Unrecognized function or variable 'FalsePosition'.

조회 수: 13 (최근 30일)
Fatih Güler
Fatih Güler 2020년 12월 13일
댓글: Walter Roberson 2020년 12월 13일
V=40;
rho=1.23;
D=0.005;
mu=1.79E-5;
epsilon=0.0015E-3;
Re=rho*V*D/mu;
f=@(x)(1./sqrt(x)+2*log10(epsilon./(3.7*D)+2.51./(Re*sqrt(x))));
xplot=0.008:0.001:0.08;
plot(xplot,f(xplot))
xlabel("f")
ylabel('y(f)')
grid on
a=0.008;
b=0.08;
tol=0.0001;
disp('iterNo lowerB upperB f(a) f(b) approxRoot f(r) error ')
fstats=FalsePosition(f,a,b,tol);
disp(fstats)
norows=size(fstats,1);
nocols=size(fstats,2);
froot=fstats(norows,nocols-2);
fprintf("Root by false position method =%f\n",froot);
function stats=FalsePosition(f,a,b,tol);
i=1;
iterNo=[i];
lowerB=[a];
upperB=[b];
functValueL=[f(a)];
functValueU=[f(b)];
approxRoot=[];
functValueR=[];
error=[];
if f(a)*f(b)>0
disp('The value of f(a)*f(b)<0,choose other values of a and b')
else
xn=f(b);
xn_1=f(a);
r=b-(xn*(b-a))/(xn-xn_1);
err=abs(f(r));
approxRoot(i)=r;
functValueR(i)=f(r);
error(i)=err;
while err >tol
i=i+1;
iterNo(i)=i;
if xn_1*f(r)<0
b=r;
else
a=r;
end
r=b-(xn*(b-a))/(xn-xn_1);
err=abs(f(r));
upperB(i)=b;
lowerB(i)=a;
functValueU(i)=f(b);
functValueL(i)=f(a);
approxRoot(i)=r;
functValueR(i)=f(r);
error(i)=err;
end
end
stats=[iterNo;lowerB;upperB;functValueL;functValueU;approxRoot;functValueR]
end

채택된 답변

Alan Stevens
Alan Stevens 2020년 12월 13일
It works just fine for me! (I copied it into a script, saved the script and then clicked the Run arrow).
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 12월 13일
The plot shows a zero near 0.03 but the reported false position is near -0.001 . This is because the error compared to the root (last row) is being displayed instead of the position of the root (second last row)
V=40;
rho=1.23;
D=0.005;
mu=1.79E-5;
epsilon=0.0015E-3;
Re=rho*V*D/mu;
f=@(x)(1./sqrt(x)+2*log10(epsilon./(3.7*D)+2.51./(Re*sqrt(x))));
xplot=0.008:0.001:0.08;
plot(xplot,f(xplot))
xlabel("f")
ylabel('y(f)')
grid on
a=0.008;
b=0.08;
tol=0.0001;
disp('iterNo lowerB upperB f(a) f(b) approxRoot f(r) error ')
iterNo lowerB upperB f(a) f(b) approxRoot f(r) error
fstats=FalsePosition(f,a,b,tol);
disp(fstats)
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 0.0080 0.0080 0.0080 0.0080 0.0234 0.0284 0.0284 0.0284 0.0284 0.0288 0.0288 0.0290 0.0290 0.0290 0.0290 0.0800 0.0570 0.0413 0.0307 0.0307 0.0307 0.0299 0.0294 0.0291 0.0291 0.0290 0.0290 0.0290 0.0290 0.0290 5.8343 5.8343 5.8343 5.8343 0.7447 0.0720 0.0720 0.0720 0.0720 0.0137 0.0137 0.0012 0.0012 0.0012 0.0012 -2.7416 -1.9559 -1.0987 -0.1884 -0.1884 -0.1884 -0.1083 -0.0522 -0.0132 -0.0132 -0.0046 -0.0046 -0.0028 -0.0015 -0.0006 0.0570 0.0413 0.0307 0.0234 0.0284 0.0299 0.0294 0.0291 0.0288 0.0290 0.0290 0.0290 0.0290 0.0290 0.0290 -1.9559 -1.0987 -0.1884 0.7447 0.0720 -0.1083 -0.0522 -0.0132 0.0137 -0.0046 0.0012 -0.0028 -0.0015 -0.0006 -0.0000
norows=size(fstats,1);
nocols=size(fstats,2);
froot=fstats(norows,nocols-2);
fprintf("Root by false position method =%f\n",froot);
Root by false position method =-0.001498
function stats=FalsePosition(f,a,b,tol);
i=1;
iterNo=[i];
lowerB=[a];
upperB=[b];
functValueL=[f(a)];
functValueU=[f(b)];
approxRoot=[];
functValueR=[];
error=[];
if f(a)*f(b)>0
disp('The value of f(a)*f(b)<0,choose other values of a and b')
else
xn=f(b);
xn_1=f(a);
r=b-(xn*(b-a))/(xn-xn_1);
err=abs(f(r));
approxRoot(i)=r;
functValueR(i)=f(r);
error(i)=err;
while err >tol
i=i+1;
iterNo(i)=i;
if xn_1*f(r)<0
b=r;
else
a=r;
end
r=b-(xn*(b-a))/(xn-xn_1);
err=abs(f(r));
upperB(i)=b;
lowerB(i)=a;
functValueU(i)=f(b);
functValueL(i)=f(a);
approxRoot(i)=r;
functValueR(i)=f(r);
error(i)=err;
end
end
stats=[iterNo;lowerB;upperB;functValueL;functValueU;approxRoot;functValueR];
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Electrical Block Libraries에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by