Unrecognized function or variable 'FalsePosition'.
13 views (last 30 days)
Show older comments
Fatih Güler
on 13 Dec 2020
Commented: Walter Roberson
on 13 Dec 2020
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 Comments
Accepted Answer
Alan Stevens
on 13 Dec 2020
It works just fine for me! (I copied it into a script, saved the script and then clicked the Run arrow).
1 Comment
Walter Roberson
on 13 Dec 2020
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
More Answers (0)
See Also
Categories
Find more on Electrical Block Libraries in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!