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

5 次查看(过去 30 天)
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