Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the radius of earth by latitude in Python - replicating a formula

I am trying to replicate a formula I found on this website, it relates to calculating the radius of the earth at a given latitude.

https://rechneronline.de/earth-radius/ or https://planetcalc.com/7721/

I then use the calculator on the website to determine whether I have replicated the formula correctly.

I have written the following code, but I can't replicate the answer given on the website (except when the latitude is equal to zero). Since the equation is quite complicated, I've even split each part into a seperate variable. However, my results are still not correct.

Example code

import math

def radius (B):
  a = 6378.137  #Radius at sea level at equator
  b = 6356.752  #Radius at poles

  c = (a**2*math.cos(B))**2
  d = (b**2*math.sin(B))**2
  e = (a*math.cos(B))**2
  f = (b*math.sin(B))**2

  R = math.sqrt((c+d)/(e+f))


  return R

For example, using a latitude of 2 (the variable B), the website calculates the radius of the earth as 6378.111km. My answer is 6360.481km.

Any help will be appreciated. Thank you in advance

like image 549
Kah Avatar asked Aug 31 '25 17:08

Kah


1 Answers

python math library takes radians as input for trignometric functions,

so make sure you convert the value of B into radians

it can be done by B=math.radians(B)

final code:

import math
def radius (B):
    B=math.radians(B) #converting into radians
    a = 6378.137  #Radius at sea level at equator
    b = 6356.752  #Radius at poles
    c = (a**2*math.cos(B))**2
    d = (b**2*math.sin(B))**2
    e = (a*math.cos(B))**2
    f = (b*math.sin(B))**2
    R = math.sqrt((c+d)/(e+f))
    return R
like image 154
nikhildr22 Avatar answered Sep 02 '25 08:09

nikhildr22