How to drop small terms in symbolic expression?

조회 수: 36 (최근 30일)
JT
JT 2020년 11월 18일
댓글: Walter Roberson 2021년 3월 16일
Hello,
I have a large complicated matrix with symbolic expressions within such as:
(3.7e-33*A*E)/R + (12.0*E*I)/R^3
Attached is a larger subset of the large matrice to give you an idea.
I want to loop through each cell to remove the small terms such as (3.7e-33*A*E)/R or (6.1e-17*A*E)/R - (7.3e-16*E*I)/R^3. If only I could target any numbers less than a certain threshold (like 1e-10) and make them 0. That would effectively accomplish that task. Any ideas how I would do this?
Thanks!

답변 (2개)

Prudhvi Peddagoni
Prudhvi Peddagoni 2020년 11월 25일
Hi,
You can use conditional indexing this
a={1; 2; 3; 4; 5};
a(cell2mat(a)>3)={0}
Hope this helps.
  댓글 수: 1
JT
JT 2020년 11월 27일
Hello,
Unfortunately, the terms I want to drop are within each cell. For example, in a cell that says:
(3.7e-33*A*E)/R + (12.0*E*I)/R^3
I want it to be edited to:
(12.0*E*I)/R^3
And the variables are symbolic which adds complication.
Thanks

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


Walter Roberson
Walter Roberson 2020년 11월 27일
I was sure that I had posted code for this at one point, but I cannot find it.
threshold = 1e-10;
syms A E I R
M = (sym('3.7e-33')*A*E)/R + (sym('12.0')*E*I)/R^3
M = 
mapSymType(M, 'vpareal', @(x) piecewise(abs(x)<=threshold, 0, x))
ans = 
  댓글 수: 2
ali akbar
ali akbar 2021년 3월 16일
How to set this as default?
Walter Roberson
Walter Roberson 2021년 3월 16일
You cannot set this as default.
There are cases, such as working with the bessel functions, where values smaller than 1e-10 (or 1e-16) are important to the calculation. Bessel function often occur in ratios, so although the individual components might be small, the ratio might be subtantial enough to affect the calculation. So in the general case having this as a default facility would lead to results that would be difficult to explain.

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

Community Treasure Hunt

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

Start Hunting!

Translated by