how i can show in matlab tha sqrt(x^2)=x in symbolic toolbox

조회 수: 4 (최근 30일)
george veropoulos
george veropoulos 2024년 9월 22일
댓글: Shubham 2024년 9월 22일
Hi i use the symbolic tool box to simplify an expression I
receive that
((2*A*dhmax + B*h0)^2/h0^2)^(1/2)*(2*A^2*dhmax^2 + 2*A*B*dhmax*h0 + 6*C*A*h0^2 - B^2*h0^2))/(24*A^2*h0^2) - (((2*A*dhmax - B*h0)^2/h0^2)^(1/2)*(2*A^2*dhmax^2 - 2*A*B*dhmax*h0 + 6*C*A*h0^2 - B^2*h0^2))/(24*A^2*h0^2)
how i can say in matlab that ((2*A*dhmax + B*h0)^2/h0^2)^(1/2)= (2*A*dhmax+B*h0)/h0 ?
thank you
George

채택된 답변

Steven Lord
Steven Lord 2024년 9월 22일
That's not true in the general case.
A = 1;
h0 = 1;
dhmax = 2;
B = -5;
lhs = ((2*A*dhmax + B*h0)^2/h0^2)^(1/2)
lhs = 1
rhs = (2*A*dhmax+B*h0)/h0
rhs = -1
If you defined those variables to all be real and positive and used sqrt (so it's the principal square root):
syms A h0 dhmax B real positive
isAlways(sqrt((2*A*dhmax + B*h0)^2/h0^2) == (2*A*dhmax+B*h0)/h0)
ans = logical
1
In that case, asking MATLAB to simplify the expression may allow it to perform the rewriting you want.
  댓글 수: 1
Shubham
Shubham 2024년 9월 22일
If all the defined variables are not positive to begin, then atleast the entire expression should be positive:
syms A h0 dhmax B real;
expr = sqrt((2*A*dhmax + B*h0)^2/h0^2);
simplifiedExpr = simplify(expr);
disp(simplifiedExpr);
isAlways(simplifiedExpr == abs( (2*A*dhmax+B*h0)/h0 ))
ans = logical
1

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

추가 답변 (1개)

John D'Errico
John D'Errico 2024년 9월 22일
Except, that equality does NOT hold! The sqrt "function" has two branches. sqrt(x^2) can as easily be -x, as it is x. So you cannot simply replace sqrt(x^2) with x.
syms A dhmax B h0
expression = ((2*A*dhmax + B*h0)^2/h0^2)^(1/2)
simplify(expression)
As you can see, simplify refuses to do what you think is obvious. However, if you want, you can effectively tell simplify to play a little fast with the rules, to not be quite so picky.
simplify(expression,IgnoreAnalyticConstraints = true)
  댓글 수: 1
John D'Errico
John D'Errico 2024년 9월 22일
편집: John D'Errico 2024년 9월 22일
Sadly, Answers is again bugged, and for some strange reason will not allow me to show the results of those operations. But it does do what you want. SIGH. It does not help if I change browsers. It does not help if I clear all history. It does not help if I empty the browser caches.
The IgnoreAnalyticConstraints flag does what you want, however.

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

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by