Error message: ValueError: math domain error

Question:

I have a Python script to find an X coordinate of the circle center. If the r1 is small the xA1 and xA2 are complex numbers, so I sometimes get the error message: ValueError: math domain error.

How can I use a while-loop to keep trying? Must I copy the entire code below the while statement? Is it possible to make a link to the first equation xs2=r3*math.sin(beta2r)?

import math

r1=0;
r2=110.5;              
r3=212.5;              
beta2d=35;             
beta1dpoz=45;          

beta2r=beta2d*math.pi/180

xs2=r3*math.sin(beta2r)     
ys2=r1-r3*math.cos(beta2r) 

kruh1=math.pow(xs2,2)+math.pow(ys2,2)                         
kruh2=math.pow(r1,2)+math.pow(r3,2)-2*r1*r3*math.cos(beta2r)

xA1=1.0/2*((xs2*(1+((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2)))))+(ys2*(math.sqrt(((2*(math.pow(r1,2)+math.pow(r2,2)))/(math.pow(xs2,2)+math.pow(ys2,2)))-(math.pow(((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2))),2))-1))))
xA2=1.0/2*((xs2*(1+((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2)))))-(ys2*(math.sqrt(((2*(math.pow(r1,2)+math.pow(r2,2)))/(math.pow(xs2,2)+math.pow(ys2,2)))-(math.pow(((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2))),2))-1))))

xAmat=[xA1,xA2]

xA=max(xAmat) 

while ('ValueError: math domain error'):   
    r1=r1+0.1
.
.
.
xA=
Asked By: user2925613

||

Answers:

This will increment r1 until xA1 and xA2 are valid.

import math

r1=0;
r2=110.5;              #vstupny polomer
r3=212.5;              #vystupny polomer
beta2d=35;             #vystupny uhel
beta1dpoz=45;          #vstupny uhel

xA1, xA2 = None, None
#Keep looping till xA1 and xA2 are valid
while xA1 == None and xA2 == None:

    beta2r=beta2d*math.pi/180

    xs2=r3*math.sin(beta2r)     
    ys2=r1-r3*math.cos(beta2r) 

    kruh1=math.pow(xs2,2)+math.pow(ys2,2)                         
    kruh2=math.pow(r1,2)+math.pow(r3,2)-2*r1*r3*math.cos(beta2r)
    
    try:
        xA1=1.0/2*((xs2*(1+((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2)))))+(ys2*(math.sqrt(((2*(math.pow(r1,2)+math.pow(r2,2)))/(math.pow(xs2,2)+math.pow(ys2,2)))-(math.pow(((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2))),2))-1))))
        xA2=1.0/2*((xs2*(1+((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2)))))-(ys2*(math.sqrt(((2*(math.pow(r1,2)+math.pow(r2,2)))/(math.pow(xs2,2)+math.pow(ys2,2)))-(math.pow(((math.pow(r1,2)-math.pow(r2,2))/(math.pow(xs2,2)+math.pow(ys2,2))),2))-1))))
    except ValueError:
        r1 += 0.1

xAmat=[xA1,xA2]
xA=max(xAmat) 

If it is possible for any of the other calculations (beta2r,s2, xs2, kruh1, kruh1) to produce a ValueError then move the try statement up to encompass those as well.

You may want to read the documentation for the try statement.

Answered By: wwii
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.