I am working on an assignment for class I can't figure out how to round the values in the table to two decimal places. Can anyone point out what I am missing?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
%clear the workspace and command window
clear, clc;
%program description
disp('This program will calculate and display the displacement of an object in free fall.');
%{
list of variables with a short description:
x_traveled = distance traveled by the object (m)
g = acceleration due to gravity (m/s^2)
t_start = starting time (s)
t_end = end time (s)
t_vector = time vector (s)
v_traveled = velocity of the object (m/s)
a_traveled = acceleration of the object (m/s^2)
free_fall = funtion for displacement
%}
%variable initialization
t_start = input('Enter a value for starting time (s): '); %
t_end = input('Enter a value for end time (s): ');
t_vector = t_start:1:t_end;
function [x_traveled, v_traveled, a_traveled] = free_fall(t_vector)
%acceleration due to gravity
g = 9.8;
%calculating distance traveled
x_traveled = 0.5 * g * t_vector.^2;
%calculating velocity (first derivative of distance)
v_traveled = g * t_vector;
%defining acceleration as a constant
a_traveled = g * ones(size(t_vector));
end
[x_traveled, v_traveled, a_traveled] = free_fall(t_vector);
%code for "free_fall(t_vector)" will be in separate file (free_fall.m)
%display
distance_traveled = round(x_traveled, 2);
velocity_traveled = round(v_traveled, 2);
acceleration_traveled = round(a_traveled, 2);
result_table1 = table(t_vector', distance_traveled', velocity_traveled', acceleration_traveled', ...
'VariableNames', {'Time (s)', 'Distance (m)', 'Velocity (m/s)', 'Acceleration (m/s^2)'});
result_table2 = array2table(round(result_table1{:,:},2));
disp(result_table2)
%{
sample output:
This program will calculate and display the displacement of an object in free fall.
Enter a value for starting time (s): 0
Enter a value for end time (s): 20
Time (s) Distance (m) Velocity (m/s) Acceleration (m/s^2)
________ ____________ ______________ ____________________
0 0 0 9.8
1 4.9 9.8 9.8
2 19.6 19.6 9.8
3 44.1 29.4 9.8
4 78.4 39.2 9.8
5 122.5 49 9.8
6 176.4 58.8 9.8
7 240.1 68.6 9.8
8 313.6 78.4 9.8
9 396.9 88.2 9.8
10 490 98 9.8
11 592.9 107.8 9.8
12 705.6 117.6 9.8
13 828.1 127.4 9.8
14 960.4 137.2 9.8
15 1102.5 147 9.8
16 1254.4 156.8 9.8
17 1416.1 166.6 9.8
18 1587.6 176.4 9.8
19 1768.9 186.2 9.8
20 1960 196 9.8
%}
1 comentario
John D'Errico
el 14 de Feb. de 2025
You want the displayed table to be shown as 2 places after the decimal, instead of the one place it chose to display? You DID round the numbers to 2 places.
Respuestas (2)
Paul
el 14 de Feb. de 2025
It appears that formating numerics in a table is not same as the format used for display in the command window.
Do you have to use a table to show the results?
0 comentarios
Walter Roberson
el 14 de Feb. de 2025
Use
format bank
before displaying the table.
All of the values in the table happen to have only a single decimal place of precision. There simply does not happen to be any digits after (for example) 4.9 .
1 comentario
Paul
el 14 de Feb. de 2025
I tried format bank and thought it didn't work for the table display. But it does work, so I'm not sure what I did.
In any case, do you know why the table displays only one place after the decimal? That is, with the default format the table displays as
t_start = 0; % input('Enter a value for starting time (s): '); %
t_end = 20; % input('Enter a value for end time (s): ');
t_vector = t_start:1:t_end;
function [x_traveled, v_traveled, a_traveled] = free_fall(t_vector)
%acceleration due to gravity
g = 9.8;
%calculating distance traveled
x_traveled = 0.5 * g * t_vector.^2;
%calculating velocity (first derivative of distance)
v_traveled = g * t_vector;
%defining acceleration as a constant
a_traveled = g * ones(size(t_vector));
end
[x_traveled, v_traveled, a_traveled] = free_fall(t_vector);
%code for "free_fall(t_vector)" will be in separate file (free_fall.m)
%display
distance_traveled = round(x_traveled, 2);
velocity_traveled = round(v_traveled, 2);
acceleration_traveled = round(a_traveled, 2);
result_table1 = table(t_vector', distance_traveled', velocity_traveled', acceleration_traveled', ...
'VariableNames', {'Time (s)', 'Distance (m)', 'Velocity (m/s)', 'Acceleration (m/s^2)'});
result_table2 = array2table(round(result_table1{:,:},2))
But displaying only one element shows four places
fmt = format
result_table2.Var4(1:3)
So the table display does something a bit more than just use the format for the command window, at least for short.
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!