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

35 vues (au cours des 30 derniers jours)
MD SAJJAD ALAM
MD SAJJAD ALAM le 7 Mai 2021
Modifié(e) : Adam Danz le 8 Mai 2021
% 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 commentaires
MD SAJJAD ALAM
MD SAJJAD ALAM le 7 Mai 2021
I am attaching a photo for better understanding.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 8 Mai 2021
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 commentaires
MD SAJJAD ALAM
MD SAJJAD ALAM le 8 Mai 2021
Its working, Thank you.
Adam Danz
Adam Danz le 8 Mai 2021
Modifié(e) : Adam Danz le 8 Mai 2021
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Adam Danz
Adam Danz le 7 Mai 2021
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 commentaires
MD SAJJAD ALAM
MD SAJJAD ALAM le 8 Mai 2021
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 le 8 Mai 2021
Modifié(e) : Adam Danz le 8 Mai 2021
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numeric 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