Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Matlab symbolic toolkit to get the positive root

Using the Matlab symbolic toolkit, consider a square root formula like the following:

syms c;         assume(c,'real');       assumeAlso(c > 0);      
syms d;         assume(d,'real');       assumeAlso(d > 0);      
N = (d^2 + 8*c)^0.5 - d
isAlways(N > 0)

Given that c and d are both positive, my intuition was that N should always be positive. d^2 + 8*c > d^2, so the root should be larger. But then I agreed with Matlab, because the square roots of 100 are actually both +10 and -10, and so sqrt(100) - 9 could be greater or less than 0 (1 or -19). But I can't figure out how to tell Matlab, using the Matlab symbolic toolbox, that I want the positive root of d^2 + 8*c and not the negative one. How do I do that? What commands should I use to tell the symbolic toolkit that I want the positive root?

To clarify the questions below, I would like a generic solution for taking the square root of a positive symbolic Matlab expression and getting the positive root. So if sqrt(a), where 'a' is a mathematical expression of many terms that I know is positive, I want to return the positive root (abs(sqrt(a))).

like image 424
BKay Avatar asked Nov 23 '25 15:11

BKay


1 Answers

Assumptions can be set on symbolic expressions (see here), so what about assume((d^2 + 8*c)^0.5>0)?

EDIT: as the following command yields "true"

isAlways((d^2 + 8*c)^0.5)

it is safe to assume that the symbolic engine evaluates the square root of the expression (d^2 + 8*c) as positive, therefore considering only the positive root.

like image 53
picchiolu Avatar answered Nov 25 '25 11:11

picchiolu