Borrar filtros
Borrar filtros

How can I determine if the absolute value of a coefficient of x in each element of a matrix was less than 0.01 consider it zero?

3 visualizaciones (últimos 30 días)
For example I have the below matrix 2*2
a=[ e-8(X^2)+12X-e-6(X)+14, -10(X^2)+0.001X-e-6(X^3); X, 4]
I want to write the code so that it give me:
a=[ 12X+14, -10(X^2); X 4]
  3 comentarios
Walter Roberson
Walter Roberson el 6 de Jul. de 2023
could you confirm that this is a question symbolic expressions? If so I posted code for this about 4 years ago but it might be hard to locate. Might be easier to rewrite.
Misi
Misi el 6 de Jul. de 2023
Editada: Misi el 6 de Jul. de 2023
Actullay, the below is my code, I want to calculate the inverse of matrix V but the matlab cannot do that so I wanted to consider the small coefficient of px as a zero, maybe it help me to determine the inverse of V.
the line 59 in the following code cannot be calculated.
syms px
c11=158e9;
c22=15.51e9;
c33=15.51e9;
c44=3.2e9;
c66=4.4e9;
c12=5.64e9;
c13=5.64e9;
c23=7.21e9;
c55=4.4e9;
h=0.01;
m=1;
n=1;
a=1;
b=1;
c1=-c13/c33;
c2=c11-c13^2/c33;
c3=c12-c13*c23/c33;
c4=c22-c23^2/c33;
c5=-c23/c33;
c6=c66;
c10=1/c33;
a11=1/c55;
a22=1/c44;
z=m*pi/a;
e=n*pi/b;
t=px*z^2;
Buckmatrix=[0 0 0 a11 0 -z;
0 0 0 0 a22 -e;
0 0 0 z e -t;
-t+c2*z^2+c6*t^2 (c3+c6)*z*e c1*z 0 0 0;
(c3+c6)*z*e -t+c6*z+c4*e^2, c5*e 0 0 0;
-c1*z, -c5*e, c10 0 0 0];
Buckmatrix=vpa(Buckmatrix,2);
[V,D] = eig(Buckmatrix);
Tf=V*exp(D*h)*inv(V);
FinalM=[Tf(3,1) Tf(3,2) Tf(3,6);Tf(4,1) Tf(4,2) Tf(4,6);Tf(5,1) Tf(5,2) Tf(5,6)];
Ans=det(FinalM);
Ans=vpa(Ans,8)

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 6 de Jul. de 2023
Then just write it as a=[ 12X+14, -10(X^2); X 4] with whatever value you have for X.
Why can't you? Do you not know what the coefficients are? Like they're not hard coded in like you have but they're somehow contained in another matrix b so that
b = [ e-8, 12, -e-6,+14, -10,+0.001, -e-6; 1, 4]
a = [ b(1) * (X^2)+ b(2) * X + b(3) * X+b(4), b(5) * (X^2), b(6) * X + b(7) * (X^3); ...
b(8) * X, b(10)]
so you can just use that a but zero out elements of b before using b, like
b = [ e-8, 12, -e-6,+14, -10,+0.001, -e-6; 1, 4] % Original list of coefficients.
b(b < 0.01) = 0; % Get rid of small coefficients.
X = 42; % Whatever.....
% Now compute a
a = [ b(1) * (X^2)+ b(2) * X + b(3) * X+b(4), b(5) * (X^2), b(6) * X + b(7) * (X^3); ...
b(8) * X, b(10)]
  2 comentarios
Misi
Misi el 6 de Jul. de 2023
Editada: Misi el 6 de Jul. de 2023
Hi many thank for response, but it cannot be use for my code
actually the below is my code and the matlab cannot calulate the inverse of V matrix in line 59
syms px
c11=158e9;
c22=15.51e9;
c33=15.51e9;
c44=3.2e9;
c66=4.4e9;
c12=5.64e9;
c13=5.64e9;
c23=7.21e9;
c55=4.4e9;
h=0.01;
m=1;
n=1;
a=1;
b=1;
c1=-c13/c33;
c2=c11-c13^2/c33;
c3=c12-c13*c23/c33;
c4=c22-c23^2/c33;
c5=-c23/c33;
c6=c66;
c10=1/c33;
a11=1/c55;
a22=1/c44;
z=m*pi/a;
e=n*pi/b;
t=px*z^2;
Buckmatrix=[0 0 0 a11 0 -z;
0 0 0 0 a22 -e;
0 0 0 z e -t;
-t+c2*z^2+c6*t^2 (c3+c6)*z*e c1*z 0 0 0;
(c3+c6)*z*e -t+c6*z+c4*e^2, c5*e 0 0 0;
-c1*z, -c5*e, c10 0 0 0];
Buckmatrix=vpa(Buckmatrix,2);
[V,D] = eig(Buckmatrix);
Tf=V*exp(D*h)*inv(V);
FinalM=[Tf(3,1) Tf(3,2) Tf(3,6);Tf(4,1) Tf(4,2) Tf(4,6);Tf(5,1) Tf(5,2) Tf(5,6)];
Ans=det(FinalM);
Ans=vpa(Ans,8)
Sam Chak
Sam Chak el 6 de Jul. de 2023
Can you introduce the if-else statement such that you have more control freedom to define the rules when a particular input is given?

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 6 de Jul. de 2023
sympref('floating', false);
tolerance = 0.01;
syms X
a=[ 1e-8*(X^2)+12*X-1e-6*(X)+14, -10*(X^2)+0.001*X-1e-6*(X^3); X, 4]
a = 
a_new = mapSymType(a, 'real', @(value) piecewise(value < -tolerance | value > tolerance, value, 0))
a_new = 
Note that this does not give you exactly what you were hoping for. Your notation was unclear so I had to guess that 12X-e-6(X) represents 12*X-1e-6*(X) . But MATLAB would never hold those two coefficients of X separate: it would always combine them, and the only question is whether they are going to get combined as the rational 12 - 1/10^6 or as floating point 12.0 - 0.000001 .
The situation would be different if one of the coefficients had an additional variable; for example 12*X-1e-6*rho*(X) would not automatically combine.
If somehow you had an input of 12*X-1e-6*(X) and you wanted to do the coefficient reduction before combining the coefficients, then you would have to wrap each entry in a function or else you would need to do text manipulation on text expressions before converting the text to symbolic.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by