Unrecognized function or variable 'FalsePosition'.
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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 ')
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Electrical Block Libraries 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
