How to Convert the negative Zero into Positive Zero in my code?

35 次查看(过去 30 天)
% Find the A B and D matrix of the given elastic constant and the given
% sequence
clc,clear all
% Input the data
E1=input(' The elastic modulus in the fiber direction in GPa : ');
E2=input(' The elastic modulus in the transverse direction in GPa : ');
G12=input(' The rigidity of the material in GPa : ');
nu12=input(' The poisson ratio of the materal : ');
n=input(' The number of the orientation have : ');
% Calculating the transformation coeffficient
nu21=(E2/E1)*nu12;
x=1-(nu12*nu21);
Q11=E1/x;
Q22=E2/x;
Q12=(nu12*E2)/x;
Q66=G12;
% Representing the transformed coefficient in the matrix form
Q=[Q11,Q12,0;Q12,Q22,0;0,0,Q66];
% Calculating the transformed reduce matrix for different orientation
for i=1:n
theta=input(' Enter the angle in degrees : ');
% forming the transformation matrix
m=cos(theta*0.0174533);
n=sin(theta*0.0174533);
T1=[m^2,n^2,2*m*n;n^2,m^2,-2*m*n;-m*n,m*n,m^2-n^2];
T=inv(T1)
T2=[m^2,n^2,m*n;n^2,m^2,-m*n;-2*m*n,2*m*n,m^2-n^2];
% Calculating the tranformed reduces matrix
disp(' The reduced transformation matrix for this orientation : ')
Qbar=T*Q*T2;
disp(Qbar)
end
  3 个评论

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-5-8
Try getting a map of where T is really small, by your definition, like less than 0.001 or whatever. Then use that to set them to zero
smallValues = T < 0.001;
T(smallValues) = 0; % Force small values to be exactly 0 without touching other, larger values.
  2 个评论
Adam Danz
Adam Danz 2021-5-8
编辑:Adam Danz 2021-5-8
On second thought,@MD, what's your reasoning for wanting to get rid of those negative values near 0? If they're supposed to be zeros then wouldn't you also want to round the positive numbers that are close to 0 too? Some of your values are -1 and those will be changed to zero with this approach.
This small modification with convert all near-zeros to zero instead of converting all values less than .001 to zero.
smallValues = abs(T) < 0.001;
T(smallValues) = 0;
But again, there is a difference between the values you're seeing on the screen and the actual value stored in T which I tried to show you by changing the display format.

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2021-5-7
Any time you see a negative 0 you can bet that the number contains a very small decimal value that is negative.
For example, set your display format to "long" and you'll see more decimal places.
This is your T matrix with short and long display formats.
format short
T =
0.0000 1.0000 0.0000
1.0000 0.0000 -0.0000
-0.0000 0.0000 -1.0000
format long
T =
0.000000000000453 0.999999999999547 0.000001346410207
0.999999999999547 0.000000000000453 -0.000001346410207
-0.000000673205104 0.000000673205104 -0.999999999999094
If you want to round to +/-1 and 0,
format long
round(T)
ans =
0 1 0
1 0 0
0 0 -1
  2 个评论
MD SAJJAD ALAM
MD SAJJAD ALAM 2021-5-8
By rounding off, the negative zero is eliminated, but it also round off the others element like 140.222 to 140, which create a rounding off error in my computation, i want to eleminate that negative zero without altering of other element.
Adam Danz
Adam Danz 2021-5-8
编辑:Adam Danz 2021-5-8
Good point. But I don't understand what your motivation is. Are you expecting to only receive 1, -1 and 0? Did you understand the difference between the display formats that I shared with you?
To be clear, in that matrix of 1s -1s 0s and -0, there's not a single number that is equal to any of those values. That's just what you are seeing in the simplified display of the variable.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by