Display Results as nicely formatted table

40 views (last 30 days)
in this codes, how do I get the resalts as a nice table showing step by step iterations clarifying more collums would be
itteration, lower, upper, f(lower), f(upper), f(xc), f(c), xupper-xlower
function y = f(x) y = x.^3 - 2;
exists. Then:
>> format long
>> eps_abs = 1e-5;
>> eps_step = 1e-5;
>> a = 0.0;
>> b = 2.0;
>> while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )
c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
end
>> [a b]
ans = 1.259918212890625 1.259925842285156
>> abs(f(a))
ans = 0.0000135103601622
>> abs(f(b))
ans = 0.0000228224229404
  1 Comment
Tadashi
Tadashi on 1 Sep 2018
If your MATLAB version is R2013b or newer, you can use table command.

Sign in to comment.

Accepted Answer

Paulo Silva
Paulo Silva on 23 Feb 2011
I have no idea what f(xc) is so I didn't included it, don't use the following code to benchmark your function because it's slower (datasave isn't preallocated), use it only to get a nice table of values and have another function that doesn't make the table for benchmarks (tic toc or profiler).
Using the format short:
clc
fprintf(' itteration lower upper f(lower) f(upper) f(c) xupper-xlower\n')
clear
f=@(x) x.^3 - 2;
format short
eps_abs = 1e-5;
eps_step = 1e-5;
a = 0;
b = 2.0;
iter=0;
datasave=[];
while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )
iter=iter+1;
c = (a + b)/2;
datasave=[datasave; iter a b f(a) f(b) f(c) b-a];
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
end
disp(datasave)
The output looks like this:
itteration lower upper f(lower) f(upper) f(c) xupper-xlower
1.0000 0 2.0000 -2.0000 6.0000 NaN 2.0000
2.0000 1.0000 2.0000 -1.0000 6.0000 -1.0000 1.0000
3.0000 1.0000 1.5000 -1.0000 1.3750 1.3750 0.5000
4.0000 1.2500 1.5000 -0.0469 1.3750 -0.0469 0.2500
5.0000 1.2500 1.3750 -0.0469 0.5996 0.5996 0.1250
6.0000 1.2500 1.3125 -0.0469 0.2610 0.2610 0.0625
7.0000 1.2500 1.2813 -0.0469 0.1033 0.1033 0.0313
8.0000 1.2500 1.2656 -0.0469 0.0273 0.0273 0.0156
9.0000 1.2578 1.2656 -0.0100 0.0273 -0.0100 0.0078
10.0000 1.2578 1.2617 -0.0100 0.0086 0.0086 0.0039
11.0000 1.2598 1.2617 -0.0007 0.0086 -0.0007 0.0020
12.0000 1.2598 1.2607 -0.0007 0.0039 0.0039 0.0010
13.0000 1.2598 1.2603 -0.0007 0.0016 0.0016 0.0005
14.0000 1.2598 1.2600 -0.0007 0.0004 0.0004 0.0002
15.0000 1.2599 1.2600 -0.0002 0.0004 -0.0002 0.0001
16.0000 1.2599 1.2599 -0.0002 0.0001 0.0001 0.0001
17.0000 1.2599 1.2599 -0.0000 0.0001 -0.0000 0.0000
18.0000 1.2599 1.2599 -0.0000 0.0001 0.0001 0.0000
19.0000 1.2599 1.2599 -0.0000 0.0000 0.0000 0.0000
Using the format long:
clc
fprintf(' itteration lower upper f(lower) f(upper) f(c) xupper-xlower\n')
clear
f=@(x) x.^3 - 2;
format long
eps_abs = 1e-5;
eps_step = 1e-5;
a = 0;
b = 2.0;
iter=0;
datasave=[];
while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )
iter=iter+1;
c = (a + b)/2;
datasave=[datasave; iter a b f(a) f(b) f(c) b-a];
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
end
disp(datasave)
The output looks like this:
itteration lower upper f(lower) f(upper) f(c) xupper-xlower
1.000000000000000 0 2.000000000000000 -2.000000000000000 6.000000000000000 NaN 2.000000000000000
2.000000000000000 1.000000000000000 2.000000000000000 -1.000000000000000 6.000000000000000 -1.000000000000000 1.000000000000000
3.000000000000000 1.000000000000000 1.500000000000000 -1.000000000000000 1.375000000000000 1.375000000000000 0.500000000000000
4.000000000000000 1.250000000000000 1.500000000000000 -0.046875000000000 1.375000000000000 -0.046875000000000 0.250000000000000
5.000000000000000 1.250000000000000 1.375000000000000 -0.046875000000000 0.599609375000000 0.599609375000000 0.125000000000000
6.000000000000000 1.250000000000000 1.312500000000000 -0.046875000000000 0.260986328125000 0.260986328125000 0.062500000000000
7.000000000000000 1.250000000000000 1.281250000000000 -0.046875000000000 0.103302001953125 0.103302001953125 0.031250000000000
8.000000000000000 1.250000000000000 1.265625000000000 -0.046875000000000 0.027286529541016 0.027286529541016 0.015625000000000
9.000000000000000 1.257812500000000 1.265625000000000 -0.010024547576904 0.027286529541016 -0.010024547576904 0.007812500000000
10.000000000000000 1.257812500000000 1.261718750000000 -0.010024547576904 0.008573234081268 0.008573234081268 0.003906250000000
11.000000000000000 1.259765625000000 1.261718750000000 -0.000740073621273 0.008573234081268 -0.000740073621273 0.001953125000000
12.000000000000000 1.259765625000000 1.260742187500000 -0.000740073621273 0.003912973217666 0.003912973217666 0.000976562500000
13.000000000000000 1.259765625000000 1.260253906250000 -0.000740073621273 0.001585548394360 0.001585548394360 0.000488281250000
14.000000000000000 1.259765625000000 1.260009765625000 -0.000740073621273 0.000422512079240 0.000422512079240 0.000244140625000
15.000000000000000 1.259887695312500 1.260009765625000 -0.000158837092386 0.000422512079240 -0.000158837092386 0.000122070312500
16.000000000000000 1.259887695312500 1.259948730468750 -0.000158837092386 0.000131823412403 0.000131823412403 0.000061035156250
17.000000000000000 1.259918212890625 1.259948730468750 -0.000013510360162 0.000131823412403 -0.000013510360162 0.000030517578125
18.000000000000000 1.259918212890625 1.259933471679688 -0.000013510360162 0.000059155646067 0.000059155646067 0.000015258789063
19.000000000000000 1.259918212890625 1.259925842285156 -0.000013510360162 0.000022822422940 0.000022822422940 0.000007629394531
  7 Comments
Paulo Silva
Paulo Silva on 23 Feb 2011
I fixed the code, now the f(c) column is correct.
buxZED
buxZED on 23 Feb 2011
clc
fprintf(' Iteration XL XR f(XL) f(XR) f(XC) |XR-XL|\n')
clear
stu_id=0.2529;
f=@(x) (2.8*x.^3)-(3.5*x.^2)+(1.5*x)-(0.15+(0.1*stu_id));
format long
eps_abs = 1e-5;
eps_step = 1e-5;
XL = 0;
XR = 1.0;
XC=(XL+XR)/2;
iter=0;
datasave=[];
while (XR - XL >= eps_step || ( abs( f(XL) ) >= eps_abs && abs( f(XR) ) >= eps_abs ) )
iter=iter+1;
datasave=[datasave; iter XL XR f(XL) f(XR) f(XC) XR-XL];
XC = (XL + XR)/2;
if ( f(XC) == 0 )
break;
elseif ( f(XL)*f(XC) < 0 )
XR = XC;
else
XL = XC;
end
end
disp(datasave)
how do I add "c" to the table
have i done anything wrong?

Sign in to comment.

More Answers (2)

Omar Alsaidi
Omar Alsaidi on 18 Jul 2019
fprintf('Celsius equivalent of a Fahrenheit temperature:\t\t Fahrenheit equivalent of a Celsius temperature:\n\n \t Celsius\tFahrenheit \t\t \t\tFahrenheit\tCelsius\n')
c=[0:2:100];
f=[32:2:212];
function [C] = Celsius(f)
C=(f-32) *5/9;
end
function [F]=Fahrenheit(c)
F =9/5*c+32;
end

Abhay
Abhay on 14 Oct 2022
X=11/3 Format long Display (X)

Categories

Find more on Startup and Shutdown 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!