MATLAB: How to tabulate this results using fprintf

figuretable

clc
clear all
close all
f=@(x) ((x^3)-x-3)
Ea=100
i=0
xl=0
xu=3
fl=f(xl);
fu=f(xu);
display('i xl xu f(xl) f(xu) xr f(xr) Ea')
while Ea > 10^-3
xr=(xl+xu)/2
fr=f(xr)
if fl*fr>0
Ea=abs((xl-xr)/xl*100);
xl=xr
fprintf('%2.0f %12.5f %12.5f %12.5f %12.5f\n' ...
, i + 1, xl, xu, fl, fu, xr, fr, Ea);
else
Ea=abs((xu-xr)/xu*100);
xu=xr
end
i=i+1
fxr(i) = fr;
Eav(i) = Ea;
iv(i) = i;
end
figure
plot(iv,Eav,'-o')
xlabel('i');
ylabel('|Ea|');
title('|Ea| versus i');
figure
plot(iv,fxr,'-o')
xlabel('i');
ylabel('fxr');
title('fxr versus i');

Best Answer

You only have 5 format fields in your fprintf call.
Try this (with all 8):
fprintf('%2.0f %12.5f %12.5f %12.5f %12.5f %12.5f %12.5f %12.5f\n', i + 1, xl, xu, fl, fu, xr, fr, Ea);
Experiment to get the result you want.