Plotting iterations vs error for false position method

조회 수: 13 (최근 30일)
Nicholas Fornaciari
Nicholas Fornaciari 2019년 6월 4일
편집: KALYAN ACHARJYA 2019년 6월 4일
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

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 4일
편집: KALYAN ACHARJYA 2019년 6월 4일
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!

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by