Plotting iterations vs error for false position method

13 views (last 30 days)
I am trying to plot the error per iteration, the graph keeps coming back blank.
clear all
clear vars
p = 1.23;
Q = input('Flow ');
v = (4*Q)/(pi*0.5^2);
d = 0.5;
u= 1.79*10^(-5);
re = (p*v*d)/u;
xl=.00001;
xu=1;
imax=100;
f =@(x) ((4*log10((re)*sqrt(x)))-.4)-(1/(sqrt(x)));
icurrent=0;
error = 100;
xr=xu-((f(xu)*(xl-xu))/(f(xl)-f(xu)));
disp(' i xl xu xr xro error')
while icurrent<imax
xrOLD=xr;
icurrent=icurrent+1;
f(xl);
f(xu);
xr=xu-((f(xu)*(xl-xu))/(f(xl)-f(xu)));
test=f(xr)*f(xl);
if icurrent > 1
error = abs(((xr-xrOLD)/xr)*100);
end
if icurrent <= 2
error = abs(((xr-xrOLD)/xr)*100);
end
fprintf(1,'\t%g\t%12g\t%12g\t%12g\t%12g\t%12g\n',icurrent,xl,xu,xr,xrOLD,error)
if test>0
xl=xr;
end
if test<0
xu=xr;
end
hold on
plot(icurrent,error)
end

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 4 Jun 2019
Edited: KALYAN ACHARJYA on 4 Jun 2019
Both icurrent and error are scalars, how can you expect a graph. Try to make vector, so that you can plot.
Here I am trying to show you how vcan you generate plot parameters, please ensure that I have just check the code to generate plot only (Please check the data and changes as per your requirements)
p = 1.23;
Q = input('Flow ');
v = (4*Q)/(pi*0.5^2);
d = 0.5;
u= 1.79*10^(-5);
re = (p*v*d)/u;
xl=.00001;
xu=1;
imax=100;
f =@(x) ((4*log10((re)*sqrt(x)))-.4)-(1/(sqrt(x)));
icurrent=[];
error=[];
icurrent(1)=0;
error(1)= 100;
i=1;
xr=xu-((f(xu)*(xl-xu))/(f(xl)-f(xu)));
disp(' i xl xu xr xro error')
while icurrent(i)<imax
xrOLD=xr;
icurrent(i+1)=icurrent(i)+1;
f(xl);
f(xu);
xr=xu-((f(xu)*(xl-xu))/(f(xl)-f(xu)));
test=f(xr)*f(xl);
if icurrent(i) > 1
error(i+1)= abs(((xr-xrOLD)/xr)*100);
end
if icurrent(i) <= 2
error(i+1)= abs(((xr-xrOLD)/xr)*100);
end
fprintf(1,'\t%g\t%12g\t%12g\t%12g\t%12g\t%12g\n',icurrent(i+1),xl,xu,xr,xrOLD,error(i+1))
if test>0
xl=xr;
end
if test<0
xu=xr;
end
hold on
i=i+1;
end
plot(icurrent,error);
yy.png
Hope it Helps!

Categories

Find more on App Building 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!