I'm trying to use the table function, How do I show all the outputs for each iteration?

1 vue (au cours des 30 derniers jours)
Lina
Lina le 5 Mai 2023
Commenté : Jon le 8 Mai 2023
clear all, format long
xl=1.5;xu=3;error=abs(xu-xl);k=0;
number_of_significant_digits=3;pre=0.5*10^(2-number_of_significant_digits);
f=@(x)x^2-3*x+2;
xr=xu;
while abs(error)>pre
xrn=(xl+xu)/2;
if f(xrn)*f(xl)>0
xl=xrn;
else
xu=xrn;
end
k=k+1;
error=(xrn-xr)*100/xrn;
xr=xrn;
end
k=k';
xr=xr';
error=error';
table(k,xr,error,f(xrn))

Réponses (1)

Jon
Jon le 5 Mai 2023
You need to assign your values in the loop to arrays so that they are saved. Could do something like this:
xl=1.5;
xu=3;
error=abs(xu-xl);
k=0;
number_of_significant_digits=3;
pre=0.5*10^(2-number_of_significant_digits);
f=@(x)x^2-3*x+2;
xr=xu;
% preallocate (we don't know size so just estimate)
numVals = 1000;
kvals = zeros(numVals,1);
errorVals = zeros(numVals,1);
xrVals = zeros(numVals,1);
fvals = zeros(numVals,1);
while abs(error)>pre
xrn=(xl+xu)/2;
if f(xrn)*f(xl)>0
xl=xrn;
else
xu=xrn;
end
k=k+1;
error = (xrn-xr)*100/xrn;
xr = xrn;
% save results in arrays
kvals(k) = k;
errorVals(k)=error;
xrVals(k)=xr;
fvals(k) = f(xrn);
end
% put in table, just use values that were actually assigned
table(kvals(1:k),xrVals(1:k),errorVals(1:k),fvals(1:k),'VariableNames',{'k','xr','error','f'})
ans = 11×4 table
k xr error f __ ______ ________ ___________ 1 2.25 -33.333 0.3125 2 1.875 -20 -0.10938 3 2.0625 9.0909 0.066406 4 1.9688 -4.7619 -0.030273 5 2.0156 2.3256 0.015869 6 1.9922 -1.1765 -0.0077515 7 2.0039 0.5848 0.0039215 8 1.998 -0.29326 -0.0019493 9 2.001 0.14641 0.00097752 10 1.9995 -0.07326 -0.00048804 11 2.0002 0.036617 0.0002442
  2 commentaires
Jon
Jon le 5 Mai 2023
Alternatively, you could assign new rows to the table on each iteration like this which may be a little simpler, not sure about efficiency, but probably doesn't matter for this small size problem:
xl=1.5;
xu=3;
error=abs(xu-xl);
k=0;
number_of_significant_digits=3;
pre=0.5*10^(2-number_of_significant_digits);
f=@(x)x^2-3*x+2;
xr=xu;
% initialize table
% this first row will get overwritten anyhow just initialize with zeros
results = table(0,0,0,0,'VariableNames',{'k','xr','error','f'});
while abs(error)>pre
xrn=(xl+xu)/2;
if f(xrn)*f(xl)>0
xl=xrn;
else
xu=xrn;
end
k=k+1;
error = (xrn-xr)*100/xrn;
xr = xrn;
% save results in table
results(k,:) = {k,xr,error,f(xrn)};
end
results
results = 11×4 table
k xr error f __ ______ ________ ___________ 1 2.25 -33.333 0.3125 2 1.875 -20 -0.10938 3 2.0625 9.0909 0.066406 4 1.9688 -4.7619 -0.030273 5 2.0156 2.3256 0.015869 6 1.9922 -1.1765 -0.0077515 7 2.0039 0.5848 0.0039215 8 1.998 -0.29326 -0.0019493 9 2.001 0.14641 0.00097752 10 1.9995 -0.07326 -0.00048804 11 2.0002 0.036617 0.0002442
Jon
Jon le 8 Mai 2023
Was this what you were looking for? Did this answer your question?

Connectez-vous pour commenter.

Catégories

En savoir plus sur General Physics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by