How to Add Headings to a list of data

5 visualizaciones (últimos 30 días)
Emily
Emily el 14 de Sept. de 2020
Comentada: Jon el 14 de Sept. de 2020
Here is the code I have.
function[root,ea,iter]=secant(func,delta,xr,es,maxit,varargin)
if nargin<3,error('atleast 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter=0;
T=[]
while (1)
xrold=xr;
xr=xr-((delta*xr*func(xr))/(func(xr+(delta*xr))-func(xr)));
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
if ea<=es|iter>=maxit,break,end
funcxr=func(xr);
T=[T;[iter,xr,func(xr),ea]];
end
T
root=xr;
Here is the output.
>> secant(@(x) 6*x^2-13*x-5,10^-6,1.2)
T =
[]
T =
1.0000 9.7428 437.8779 87.6832
2.0000 5.5290 106.5398 76.2143
3.0000 3.5319 23.9303 56.5448
4.0000 2.7174 3.9799 29.9710
5.0000 2.5145 0.2472 8.0718
6.0000 2.5001 0.0012 0.5757
7.0000 2.5000 0.0000 0.0029
ans =
2.5000
I need to have headings on the top of the colmuns and have no clue how to add them. Please help

Respuesta aceptada

Star Strider
Star Strider el 14 de Sept. de 2020
If you have R2013b or later, use a table. The documentation explains how to specify and add the variable names. Note that if you have R2019b or later, the variable names do not need to be valid MATLAB identifiers.
  2 comentarios
Emily
Emily el 14 de Sept. de 2020
I have attempted to use table and I do not undestand how to get the information from all iterations into a single table.
Star Strider
Star Strider el 14 de Sept. de 2020
With your current code and arguments, this array2table call:
Tout = array2table(T, 'VariableNames',{'iter','xr','func(xr)','ea'})
produces this reault:
Tout =
7×4 table
iter xr func(xr) ea
____ ______ __________ _________
1 9.7428 437.88 87.683
2 5.529 106.54 76.214
3 3.5319 23.93 56.545
4 2.7174 3.9799 29.971
5 2.5145 0.24716 8.0718
6 2.5001 0.001243 0.57567
7 2.5 3.3171e-08 0.0029246
There are a number of different ways to create tables.
Make appropriate changes to get the result you want.

Iniciar sesión para comentar.

Más respuestas (1)

Jon
Jon el 14 de Sept. de 2020
Editada: Jon el 14 de Sept. de 2020
In general, MATLAB doesn't provide for column or row labels for matrices.
You could put the array into a MATLAB table and label the columns (variable names) there.
Here is a simple example where I turn a 5 by 3 array into a table and label the columns
A = rand(5,3)
Atbl = array2table(A,'VariableNames',{'cat','dog','tortoise'})
You should be able to adapt this to your situation.
  3 comentarios
Jon
Jon el 14 de Sept. de 2020
First get it all into an array (matrix) as you are doing already. Then when you have all of the data in the array (after your while loop) use the array2table command as I earlier suggested.
Jon
Jon el 14 de Sept. de 2020
p.s. It looks like StarStrider and I collided on providing you with similar answers at around the same time. I hadn't seen his posted yet when I sent mine.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by