The surface area won't round to one decimal place and I don't know how to get rid of the 'e's.

2 vues (au cours des 30 derniers jours)
%%radii
r1=sqrt(0.75*4000/pi);
r2=sqrt(0.75*3500/pi);
r3=sqrt(0.75*3000/pi);
r4=sqrt(0.75*2500/pi);
r5=sqrt(0.75*2000/pi);
r6=sqrt(0.75*1500/pi);
r7=sqrt(0.75*1000/pi);
r1=round(r1,1)
r1 = 30.9000
r2=round(r2,1)
r2 = 28.9000
r3=round(r3,1)
r3 = 26.8000
r4=round(r4,1)
r4 = 24.4000
r5=round(r5,1)
r5 = 21.9000
r6=round(r6,1)
r6 = 18.9000
r7=round(r7,1)
r7 = 15.5000
%%surface area
S1=4*pi*(r1^2);
S2=4*pi*(r2^2);
S3=4*pi*(r3^2);
S4=4*pi*(r4^2);
S5=4*pi*(r5^2);
S6=4*pi*(r6^2);
S7=4*pi*(r7^2);
s1=round(S1,1)
s1 = 1.1998e+04
s2=round(S2,1)
s2 = 1.0496e+04
s3=round(S3,1)
s3 = 9.0257e+03
s4=round(S4,1)
s4 = 7.4815e+03
s5=round(S5,1)
s5 = 6027
s6=round(S6,1)
s6 = 4.4888e+03
s7=round(S7,1)
s7 = 3.0191e+03

Réponses (4)

Stephen23
Stephen23 le 17 Fév 2025
Déplacé(e) : Matt J le 17 Fév 2025
Do not mix up the data stored in memory with how those data are displayed.

Torsten
Torsten le 17 Fév 2025
Déplacé(e) : Matt J le 17 Fév 2025

Umar
Umar le 17 Fév 2025

Hi @Michael ,

To address your requirements, I will provide a complete MATLAB code that calculates the radii and surface areas of spheres based on specified volumes. Additionally, ensuring that the surface area results are formatted correctly, avoiding scientific notation and rounding to one decimal place.

% Define the volumes of the spheres
volumes = [4000, 3500, 3000, 2500, 2000, 1500, 1000];
% Preallocate arrays for radii and surface areas
radii = zeros(1, length(volumes));
surface_areas = zeros(1, length(volumes));
% Calculate the radii based on the volumes
for i = 1:length(volumes)
  radii(i) = sqrt(0.75 * volumes(i) / pi);
  radii(i) = round(radii(i), 1); % Round to one decimal place
end
% Calculate the surface areas based on the radii
for i = 1:length(radii)
  surface_areas(i) = 4 * pi * (radii(i)^2);
  surface_areas(i) = round(surface_areas(i), 1); % Round to one decimal place
end
% Display the results without scientific notation
fprintf('Radii (rounded to 1 decimal place):\n');
for i = 1:length(radii)
  fprintf('r%d = %.1f\n', i, radii(i));
end
fprintf('\nSurface Areas (rounded to 1 decimal place):\n');
for i = 1:length(surface_areas)
  fprintf('S%d = %.1f\n', i, surface_areas(i));
end

Please see attached.

The code above shows that the volumes of the spheres are defined in an array for easy iteration. Arrays for radii and surface_areas are preallocated to improve performance. A loop iterates through the volumes, calculating the radius for each sphere using the formula ( r = sqrt{fraction {0.75 times V}{\pi}} ). Each radius is rounded to one decimal place. Another loop calculates the surface area for each radius using the formula ( S = 4\pi r^2 ), and the results are also rounded to one decimal place. The fprintf function is used to display the radii and surface areas in a formatted manner, ensuring that the output is clear and avoids scientific notation.

By using fprintf, it ensures that the output is user-friendly and meets the requirement of rounding to one decimal place without scientific notation.

If you have any further questions or need additional modifications, please feel free to ask.


Image Analyst
Image Analyst le 17 Fév 2025
To get some other format displayed other than the default one, you can try to use format. If none of those built-in formats are what you want, then you can use fprintf to display it exactly as you want.
format short g
%%radii
r1=sqrt(0.75*4000/pi);
r2=sqrt(0.75*3500/pi);
r3=sqrt(0.75*3000/pi);
r4=sqrt(0.75*2500/pi);
r5=sqrt(0.75*2000/pi);
r6=sqrt(0.75*1500/pi);
r7=sqrt(0.75*1000/pi);
r1=round(r1,1)
r1 =
30.9
fprintf('r1 = %.1f\n', r1)
r1 = 30.9
r2=round(r2,1);
fprintf('r2 = %.1f\n', r2)
r2 = 28.9
r3=round(r3,1);
fprintf('r3 = %.1f\n', r3)
r3 = 26.8
r4=round(r4,1);
fprintf('r4 = %.1f\n', r4)
r4 = 24.4
r5=round(r5,1);
fprintf('r5 = %.1f\n', r5)
r5 = 21.9
r6=round(r6,1);
fprintf('r6 = %.1f\n', r6)
r6 = 18.9
r7=round(r7,1);
fprintf('r7 = %.1f\n', r7)
r7 = 15.5
%%surface area
S1=4*pi*(r1^2);
S2=4*pi*(r2^2);
S3=4*pi*(r3^2);
S4=4*pi*(r4^2);
S5=4*pi*(r5^2);
S6=4*pi*(r6^2);
S7=4*pi*(r7^2);
s1=round(S1,1);
s2=round(S2,1);
s3=round(S3,1);
s4=round(S4,1);
s5=round(S5,1);
s6=round(S6,1);
s7=round(S7,1);
fprintf('s1 = %.1f\n', s1)
s1 = 11998.5
fprintf('s2 = %.1f\n', s2)
s2 = 10495.6
fprintf('s3 = %.1f\n', s3)
s3 = 9025.7
fprintf('s4 = %.1f\n', s4)
s4 = 7481.5
fprintf('s5 = %.1f\n', s5)
s5 = 6027.0
fprintf('s6 = %.1f\n', s6)
s6 = 4488.8
fprintf('s7 = %.1f\n', s7)
s7 = 3019.1

Catégories

En savoir plus sur Data Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by