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

조회 수: 31 (최근 30일)
MD SAJJAD ALAM
MD SAJJAD ALAM 2021년 5월 7일
편집: Adam Danz 2021년 5월 8일
% 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
MD SAJJAD ALAM
MD SAJJAD ALAM 2021년 5월 7일
I am attaching a photo for better understanding.

댓글을 달려면 로그인하십시오.

채택된 답변

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
MD SAJJAD ALAM
MD SAJJAD ALAM 2021년 5월 8일
Its working, Thank you.
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