Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

A challenge for Mathcad Prime 5.0

Status
Not open for further replies.

PNachtwey

Electrical
Oct 9, 2004
776
I am still waiting for PTC to get their act together. I have 25 years of Mathcad files.

I stopped upgrading at Mathcad 13. I use the symbolic solver a lot and PTC ruined it when they made Mathcad 14. Now I use Mathematica that is expensive. I also use Python with sympy to do symbolic processing.
So here is the challenge. Given 3 points solve for the center of the circle and radius to the 3 points.

This problem is simple enough to be entered in a few minutes.
Code:
from sympy import symbols, init_printing, sqrt 
from sympy.solvers import solve

""" find the center and radius of a circle defined by three points """
init_printing()                         # enable pretty printing.
x0, y0, x1, y1, x2, y2, xc, yc, r = symbols('x0, y0, x1, y1, x2, y2, xc, yc, r')
eq0 = (x0-xc)**2 + (y0-yc)**2 - r**2
eq1 = (x1-xc)**2 + (y1-yc)**2 - r**2
eq2 = (x2-xc)**2 + (y2-yc)**2 - r**2
s = solve([eq0, eq1, eq2], (xc, yc, r))
# there are two solutions, r > 0 and r < 0
xc0, yc0, r0 = s[0]
print("\nxc0 =",xc0)
print("\nyc0 =",yc0)
print("\nr0  =",r0)
xc1, yc1, r1 = s[1]
print("\nxc1 =",xc1)
print("\nyc1 =",yc1)
print("\nr1  =",r1)

Here is the output. There are two solutions because r can be positive or negative and still be valid as far as the problem goes but only the positive solution works.
Code:
xc0 = (x0**2*y1 - x0**2*y2 - x1**2*y0 + x1**2*y2 + x2**2*y0 - x2**2*y1 + y0**2*y1 - y0**2*y2 - y0*y1**2 + y0*y2**2 + y1**2*y2 - y1*y2**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))

yc0 = (-x0**2*x1 + x0**2*x2 + x0*x1**2 - x0*x2**2 + x0*y1**2 - x0*y2**2 - x1**2*x2 + x1*x2**2 - x1*y0**2 + x1*y2**2 + x2*y0**2 - x2*y1**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))

r0  = sqrt((x0**2 - 2*x0*x1 + x1**2 + y0**2 - 2*y0*y1 + y1**2)*(x0**2 - 2*x0*x2 + x2**2 + y0**2 - 2*y0*y2 + y2**2)*(x1**2 - 2*x1*x2 + x2**2 + y1**2 - 2*y1*y2 + y2**2))/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))

xc1 = (x0**2*y1 - x0**2*y2 - x1**2*y0 + x1**2*y2 + x2**2*y0 - x2**2*y1 + y0**2*y1 - y0**2*y2 - y0*y1**2 + y0*y2**2 + y1**2*y2 - y1*y2**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))

yc1 = (-x0**2*x1 + x0**2*x2 + x0*x1**2 - x0*x2**2 + x0*y1**2 - x0*y2**2 - x1**2*x2 + x1*x2**2 - x1*y0**2 + x1*y2**2 + x2*y0**2 - x2*y1**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))

r1  = -sqrt((x0**2 - 2*x0*x1 + x1**2 + y0**2 - 2*y0*y1 + y1**2)*(x0**2 - 2*x0*x2 + x2**2 + y0**2 - 2*y0*y2 + y2**2)*(x1**2 - 2*x1*x2 + x2**2 + y1**2 - 2*y1*y2 + y2**2))/(2*x0*y1 - 2*x0*y2 - 2*x1*y0 + 2*x1*y2 + 2*x2*y0 - 2*x2*y1)
The generated code is much more efficient that Mathcad 13. Notice I can use text which is very convenient for educational purpose or solving math for a webpage with a java script interface. Python will also generate LaTex for pretty printing on web pages.

What can Mathcad Prime 5.0 do?





Peter Nachtwey
Delta Computer Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor