cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Why root function does not find the second imaginary solution?

Cornel
19-Tanzanite

Why root function does not find the second imaginary solution?

Hi,

Why is it not found also this solution by the root function?

Cornel_1-1728048982114.png

 

Cornel_0-1728048956282.png

Cornel_2-1728049105967.png

Cornel_3-1728049115953.png

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Cornel)

I was a bit surprised to see that root would even return non-real results with real guesses.

Here are my 2 Cents, using random non-real guesses.

Werner_E_0-1728101568290.png

 

 

View solution in original post

19 REPLIES 19
StuartBruff
23-Emerald III
(To:Cornel)

y runs from -50.  Indices have to start from ORIGIN.

 

2024 10 04 F.png

 

Stuart

Cornel
19-Tanzanite
(To:StuartBruff)

Ok, but still one solution is missing:

The second imaginary solution:

Cornel_1-1728052117461.png

I don't know why this solution it is not catched/found it...

Cornel_0-1728052302577.png

StuartBruff
23-Emerald III
(To:Cornel)

Try polyroots.  root's algorithm (without a search range) doesn't cover all complex results).

 

StuartBruff_0-1728052723748.png

 

Stuart

 

Cornel
19-Tanzanite
(To:StuartBruff)

I understand. Also, I can use solve command and received all 3 solutions.

But I want to understand why in the way as I shown above (with x as a range, and with root function), this way does not find the second imaginary solution as I should be the case.

StuartBruff
23-Emerald III
(To:Cornel)

I enjoy conversations where messages cross each other—so much more fun than a strictly linear thread. 😊 

 

You'd have to dig into the code to understand why Ridder's (or Brent's) algorithm returns one complex root and not the other.  It would help if one could specify a complex range.  Perhaps somebody could write a wrapper function around f(x) that allows root to stray further into the complex plane.  Alternatively, somebody could write a new root function.  I've got vague memories of rolling my own Levenberg–Marquardt solver to better understand what it did.

 

Stuart

Cornel
19-Tanzanite
(To:StuartBruff)

Cornel_0-1728053911152.png

 

I found a way:

 

Cornel_1-1728053926696.png

Cornel
19-Tanzanite
(To:Cornel)

But thanks for this hint. In this way we can use Unique function also when range variable is negative.

Cornel_1-1728054225996.png

Cornel_0-1728054145202.png

 

StuartBruff
23-Emerald III
(To:Cornel)

Yes, Mathcad doesn't care what the minimum value of the range variable you used was, as long as you ensure that all indices are equal to or greater than (current) ORIGIN.     

 

Stuart

StuartBruff
23-Emerald III
(To:Cornel)

I like it.  If it doesn't work first time, hit it with a bigger hammer!  🙂

 

However, when I changed x to match your new values in my download of your worksheet, Unique gave a different result.

 

2024 10 04 H.png

 

I used one of my versions of the function set (which does the same sort of thing as Unique).  That gave a better result ... stepping x by 10 gives a lot of (default) zeros.  I wrote out UniqueP from Tetsuro's original post, which gave the same result as yours.  I wonder what's going on?

 

Stuart

Cornel
19-Tanzanite
(To:StuartBruff)

Be aware that in the first post when I uploaded my mathcad prime sheet I had 0 for the second element. I changed to 1, to skip the first 0. Only that I modified (0 -> 1) from the uploaded file.

Cornel_0-1728056641919.png

 

StuartBruff
23-Emerald III
(To:Cornel)

OK.

StuartBruff
23-Emerald III
(To:StuartBruff)

The mystery is solved.  The -1s in Unique's result aren't all -1s.  Showing a few more decimal places shows that beating root into submission with a heavy integer hammer gives more than just the three expected roots.

 

2024 10 04 N.png

 

So Unique and my set0 are looking for strict numerical equality, whereas my set2 only looks for approximate equality as determined by the system variable TOL.

 

Stuart

StuartBruff
23-Emerald III
(To:Cornel)

Just some random thoughts, particularly concerning the skipped elements in A that are set to 0 by default.

 

There are other methods of dealing with this issue, but I like to use functions to convert between data types, so ...

 

2024 10 04 I.png

 

2024 10 04 J.png

 

Stuart

 

Mathcad Prime 10 file attached.

StuartBruff
23-Emerald III
(To:Cornel)

As to why, ...

 

Mathcad Help:

 

The root of a function is the value at which the function equals zero.
 
polyroots(v)—Returns a vector containing the roots of the polynomial whose coefficients are in v.
 
By default, polyroots uses the LaGuerre method which is iterative and searches for solutions in the complex plane. If you want to use the Companion Matrix method instead, right-click on the polyroots function and select the method from the menu. The Companion Matrix method converts the equations into an eigenvalue problem. Learn more about choosing a solving algorithm for the polyroots function here.
 
root(f(var1, var2, ...), var1, [a, b])—Returns the value of var1 to make the function f equal to zero. If a and b are specified, root finds var1 on the interval [a, b]. Otherwise, var1 must be defined with a guess value before root is called. When a guess value is used, root uses the secant or Mueller’s method; in the case where root bracketing is used, root uses Ridder’s or Brent’s method.
 
Stuart
 
I have a strange feeling of deja vu, as if this subject has been discussed in the not too distant past.
Werner_E
25-Diamond I
(To:Cornel)

I was a bit surprised to see that root would even return non-real results with real guesses.

Here are my 2 Cents, using random non-real guesses.

Werner_E_0-1728101568290.png

 

 

StuartBruff
23-Emerald III
(To:Werner_E)

Now that's interesting.  When I tried that, Mathcad squawked like a parrot on a sugar high.  "No, take that nasty complexity out right now! Honestly, some people. Pieces of Eight. Who's a pretty CAS. The horror, The horror ...".  

 

The local definition I used above arose from such an attempt. However, with freshly typed expressions this time, the parrot was rather quiet.

 

2024 10 05 A.png

 

Reducing the range over which root searched gave better (for values of "better") results.

 

2024 10 05 B.png

 

2024 10 05 C.png

 

I blame any typos, logical errors and programming mistakes on it being Oh Dark O'Clock and sleep deprivation taking its toll.

 

Stuart

Cornel
19-Tanzanite
(To:Werner_E)

Cornel_0-1728287005557.png

 

Cornel_1-1728287012113.png

 

 

StuartBruff
23-Emerald III
(To:Cornel)


@Cornel wrote:

Cornel_0-1728287005557.png


 

Some button jabbing sessions weren't converging at all with small nz.  At first, I thought I broken something because I wasn't seeing any results or was only getting one result.  So, I modified my smart monkey program to trap convergence errors and increased nz.  

 

2024 10 07 D.png

 

Stuart

StuartBruff
23-Emerald III
(To:StuartBruff)

I felt deeply unsatisfied looking at that image and realized it needed a good sorting. Unfortunately, Mathcad's standard sort algorithm doesn't sort complex numbers in a satisfactory order - it sorts on the Re element only; the Im order is somewhat arbitrary.

 

So,  I dug up one of my old Mathcad 15 mergesort worksheets and reworked it to sort on Re then Im.  Csort(A,clist) sorts array A using the column order specified in clist.  zsort(A) sorts the elements of array A in ascending order, retaining A's shape.

 

Sorting the symbolic solver's roots and the numerical solver's roots allows for an easier comparison of the different sets of solutions.

 

2024 10 08 I.png

 

Stuart

 

zmrunif = z m r unif = complex matrix random uniform

zmrunif(m,n,a,b) : return matrix of size m rows by n columns with random numbers spanning the range a to b, where a and b are possibly complex.

Announcements

Top Tags