i'm having issues using format short in my code

40 vues (au cours des 30 derniers jours)
Trevor Pixley
Trevor Pixley le 18 Nov 2021
Commenté : Trevor Pixley le 18 Nov 2021
I want to limit the number of decimal points my outputs have for an entire code section. I tried using "format short" at the top of my section, but find that my outputs at the end of calculations have 30+ digits attached. The values are correct, i just want to pare them down without having to use the vpa command on every one.
  2 commentaires
Adam Danz
Adam Danz le 18 Nov 2021
Could you provide a minimal working example?
Trevor Pixley
Trevor Pixley le 18 Nov 2021
Yeah for sure
here's what i'm running:
clear
clc
format short
syms t
E = 9000 %MPa
E = 9000
v = 0.36;
t = 152e-5;
I = eye(3);
F = t*[.1 -.171 0;
.063 .062 0;
0 -.045 -.149];
Ft = transpose(F);
strain = vpa(.5*(F+Ft),4);
rotation = vpa(.5*(F-Ft),4);
epskk = sum(diag(strain));
delta = 0;
for i = 1:length(strain)
for j = 1:length(strain)
if i==j
delta = 1;
else
delta = 0;
end
sig(i,j) = (E/(1+v))*(strain(i,j) + (v/(1-2*v))*epskk*delta);
end
disp(vpa(sig,4))
end
sigt = transpose(sig);
n = [0.8 -0.6 0];
nt = transpose(n);
T = vpa(sigt*nt,4);
signn = vpa(dot(T,n),4); %this is in MPa
T_normal = signn*nt
T_normal = 
T_shear = T-T_normal
T_shear = 
and the outputs are:
T_normal =
1.2462824873952785892947148942708
-0.93471186554645894197103617070313
0
T_shear =
0.018830117646986670192218298321815
0.025106823529315560256291064429087
0.13579411764699626557451077079207
as you can see, I've applied the vpa function to a lot of the calculations thru out the section already. I just want to know for the future if there's a different way to do this. I figure the issue is probably because I am doing this work with symbollic values before applying a number value for t, but can't confirm that this is overriding "format short".

Connectez-vous pour commenter.

Réponses (1)

Cris LaPierre
Cris LaPierre le 18 Nov 2021
Because you are using symbolic variables. For symbolic, try using the digits function instead.
syms t
digits(4)
E = 9000; %MPa
v = 0.36;
t = 152e-5;
I = eye(3);
F = t*[.1 -.171 0;
.063 .062 0;
0 -.045 -.149];
Ft = transpose(F);
strain = .5*(F+Ft);
rotation = .5*(F-Ft);
epskk = sum(diag(strain));
delta = 0;
for i = 1:length(strain)
for j = 1:length(strain)
if i==j
delta = 1;
else
delta = 0;
end
sig(i,j) = (E/(1+v))*(strain(i,j) + (v/(1-2*v))*epskk*delta);
end
disp(sig)
end
1.1740 -0.5432 0 1.1740 -0.5432 0 -0.5432 0.7918 -0.2263 1.1740 -0.5432 0 -0.5432 0.7918 -0.2263 0 -0.2263 -1.3306
sigt = transpose(sig);
n = [0.8 -0.6 0];
nt = transpose(n);
T = sigt*nt;
signn = dot(T,n); %this is in MPa
T_normal = signn*nt
T_normal = 3×1
1.2463 -0.9347 0
T_shear = T-T_normal
T_shear = 3×1
0.0188 0.0251 0.1358

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by