I’m currently majoring in maths and minoring in physics and hence need to do some homework assignments in python.

I never coded in python(miniscule experience in java) and never attended the python classes assigned by uni(ik) and currently I try to apprroximate pi by measuring the area under the graph of a circle in python.

My Problem is that, although no errors appear in the console and I’m calling the function, no output is displayed(when I put return above the print statement)

When I put return after the print statement however I get a math domain error;

I previously couldnt use x^2 when defining y in the halfCircle function and tried to solve it by using pow(x,2) instead.

Currently I only implemented the end result to be pi/2.

And ik that it’s a bad approximation, since I only let the area pieces be under the graph.

If this code works I’ll implement another function which does the same for area pieces which go over the graph and then calculate their average.

Then I’ll multiply it by 2.

I’m currently confused about where to place the return statement and what’s up with the math domain error.

This is my code:

`

```
import math
def halfCircle(x):
y = abs(math.sqrt(1-pow(x,2)))
return y
#N is the Number of pieces we try to approximate pi with, the higher N, the closer G will be to pi.
def areaUnderGraph(N):
#x1 is initialized to be -1 since our range starts at x0 = -1
x1 = float(-1)
#x2 will be set to 0 at the moment.
x2 = float(0)
#We use G as the final area under the graph and set it to 0 to begin with.
G = 0
#Now we need to know how thick our areas will be, [-1,1] is 2 long on the x axis, so we divide 2 by N:
d = 2/N
#Now we come to calculating our areas and adding them up until we hit 1 as the end of the given range.
while x2 <= 1:
#x2 will be x1 + d since we're adding from left to right.
x2 = x1 + d
#Since we need the smaller area pieces we calculate the corresponding y values for x1 and x2 so as to discern the smaller area value.
y2 = halfCircle(x2)
y1 = halfCircle(x1)
#The area is smaller, if y is smaller;
a1 = (x2 - x1) * y1
a2 = (x2 - x1) * y2
if a1 > a2:
a = a2
else:
#if a2 and a1 are equal it doesn't matter wich value a takes (a1 or a2)
a = a1
#for the next step we let x1 become x2 since that will be the leftmost point of the next area piece.
x1 = x2
#We define G to be the area made up of all the previous areas and the newly calculated one.
G = G + a
print('The Area under the graph is ' + G)
return G
areaUnderGraph(100)
```

`

### >Solution :

`math.sqrt()`

does not take a negative number. When I was trying to use a negative value as a parameter, it is showing the math domain error.

See this example:

```
import math
math.sqrt(-4)
Traceback (most recent call last):
File "/usr/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
ValueError: math domain error
```

So, I changed the function to use `abs`

before calling the `sqrt`

method.

Updated code:

```
import math
def halfCircle(x):
y = math.sqrt(abs(1 - pow(x, 2)))
return y
# N is the Number of pieces we try to approximate pi with, the higher N, the closer G will be to pi.
def areaUnderGraph(N):
# x1 is initialized to be -1 since our range starts at x0 = -1
x1 = float(-1)
# x2 will be set to 0 at the moment.
x2 = float(0)
# We use G as the final area under the graph and set it to 0 to begin with.
G = 0
# Now we need to know how thick our areas will be, [-1,1] is 2 long on the x axis, so we divide 2 by N:
d = 2 / N
# Now we come to calculating our areas and adding them up until we hit 1 as the end of the given range.
while x2 <= 1:
# x2 will be x1 + d since we're adding from left to right.
x2 = x1 + d
# Since we need the smaller area pieces we calculate the corresponding y values for x1 and x2 so as to discern the smaller area value.
y2 = halfCircle(x2)
y1 = halfCircle(x1)
# The area is smaller, if y is smaller;
a1 = (x2 - x1) * y1
a2 = (x2 - x1) * y2
if a1 > a2:
a = a2
else:
# if a2 and a1 are equal it doesn't matter wich value a takes (a1 or a2)
a = a1
# for the next step we let x1 become x2 since that will be the leftmost point of the next area piece.
x1 = x2
# We define G to be the area made up of all the previous areas and the newly calculated one.
G = G + a
return G
print(f'The Area under the graph is {areaUnderGraph(100)}')
```

Output:

```
The Area under the graph is 1.5491342564916826
```