How to create a single table that shows every iteration

4 views (last 30 days)
This is my code that uses the bisection method to find the maximum bending moment on a beam. Right now the output shows 16 different iterations on 16 different tables all equal to T. How can I write my code so all the tables are together in a single table? I feel like this ought to be rather simple but I can't figure it out.
f=
@(x) 33.33-5/3*x^2; %function of shear force
x1=0; %location of support A
x2=6; %location of maximum linear distributed force
eps=abs(x2-x1);
conv=.0001;
iter=0;
while eps>=conv
xc=(x2+x1)/2;
if f(xc)*f(x2)<0
x1=xc;
elseif f(xc)*f(x1)<0
x2=xc;
else
break;
end
eps=abs(x2-x1);
iter=iter+1;
T=table(iter,x1,x2,xc,f(x1),f(x2),f(xc),eps);
T.Properties.VariableNames={'iter' 'x1' 'x2' 'xc' 'fx1' 'fx2' 'fxc' 'eps'}
end

Answers (1)

Walter Roberson
Walter Roberson on 12 Sep 2019
Edited: Walter Roberson on 13 Sep 2019
f=
@(x) 33.33-5/3*x^2; %function of shear force
x1=0; %location of support A
x2=6; %location of maximum linear distributed force
eps=abs(x2-x1);
conv=.0001;
iter=0;
while eps>=conv
xc=(x2+x1)/2;
if f(xc)*f(x2)<0
x1=xc;
elseif f(xc)*f(x1)<0
x2=xc;
else
break;
end
eps=abs(x2-x1);
iter=iter+1;
iters(iter) = iter;
x1s(iter) = x1;
x2s(iter) = x2;
xcs(iter) = xc;
fx1s(iter) = f(x1);
fx2s(iter) = f(x2);
fxcs(iter) = f(xc);
epss(iter) = eps;
end
T = table(iters(:), x1s(:), x2s(:), xcs(:), fx1s(:), fx2s(:), fxcs(:), epss(:));
T.Properties.VariableNames = {'iter' 'x1' 'x2' 'xc' 'fx1' 'fx2' 'fxc' 'eps'}

Categories

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