Program to Calculate Volume of Ellipsoid

Python Program to Calculate Volume of Ellipsoid

Given the three radius values of an ellipsoid and the task is to calculate the volume of a given ellipsoid in python.

Ellipsoid :

Ellipsoid is a closed surface whose plane cross-sections are all ellipses or circles. An ellipsoid has three mutually perpendicular axes that intersect at the center. It is a three-dimensional, closed geometric shape with all of its planar sections being ellipses or circles.

An ellipsoid has three independent axes and is usually defined by the lengths of the three semi-axes, a, b, and c. If an ellipsoid is formed by rotating an ellipse about one of its axes, the ellipsoid’s two axes are the same, and it is known as an ellipsoid of revolution or spheroid. It is a sphere if the lengths of all three of its axes are the same.

Formula:

Volume = (4/3) * pi * r1 * r2 * r3

where pi = 3.1415…

r1 = first radius

r2 = second radius

r3 =  third radius

Examples:

Example1:

Input:

Given first radius = 1
Given second radius = 3
Given third radius = 6.5

Output:

The Volume of given ellipsoid with given radii { 1 3 6.5 } =  81.4772054708513

Example2:

Input:

Given first radius = 2
Given second radius = 4.5
Given third radius = 8

Output:

The Volume of given ellipsoid with given radii { 2 4.5 8 } =  300.8389125077586

Program to Calculate Volume of Ellipsoid in Python

Below are the ways to calculate the volume of a given ellipsoid in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the first radius as static input and store it in a variable.
  • Give the second radius as static input and store it in another variable.
  • Give the third radius as static input and store it in another variable.
  • Create a function to say Ellipsoid_volume() which takes the given three radii as the arguments and returns the volume of a given ellipsoid.
  • Inside the function, calculate the volume of the ellipsoid using the above given mathematical formula, math. pi and store it in a variable.
  • Return the above result.
  • Pass the given three radii as the arguments to the Ellipsoid_volume() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say Ellipsoid_volume() which takes the given three radii as
# the arguments and returns the volume of a given ellipsoid.


def Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss):
    # Inside the function, calculate the volume of the ellipsoid using the above given
    # mathematical formula, math.pi and store it in a variable.
    rslt_vol = 1.33 * math.pi * fst_radiuss * scnd_radiuss * thrd_radiuss
    # Return the above result.
    return rslt_vol


# Give the first radius as static input and store it in a variable.
fst_radiuss = 1
# Give the second radius as static input and store it in another variable.
scnd_radiuss = 3
# Give the third radius as static input and store it in another variable.
thrd_radiuss = 6.5
# Pass the given three radii as the arguments to the Ellipsoid_volume() function
# and print it.
print("The Volume of given ellipsoid with given radii {", fst_radiuss, scnd_radiuss, thrd_radiuss, "} = ",
      Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss))
#include <iostream>
#include <cmath>

using namespace std;

int main() {
  double fst_radiuss = 1;
   double scnd_radiuss = 3;
   double thrd_radiuss = 6.5;
   double rslt_vol = 1.33 * M_PI * fst_radiuss * scnd_radiuss * thrd_radiuss;
 cout << "The Volume of given ellipsoid with given radii {" << fst_radiuss << ", " << scnd_radiuss << ", " << thrd_radiuss << "} = " << rslt_vol << rslt_vol;

}

Output:

The Volume of given ellipsoid with given radii { 1 3 6.5 } =  81.4772054708513

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the first radius as user input using the float(input()) function and store it in a variable.
  • Give the second radius as user input using the float(input()) function and store it in another variable.
  • Give the third radius as user input using the float(input()) function and store it in another variable.
  • Create a function to say Ellipsoid_volume() which takes the given three radii as the arguments and returns the volume of a given ellipsoid.
  • Inside the function, calculate the volume of the ellipsoid using the above given mathematical formula, math. pi and store it in a variable.
  • Return the above result.
  • Pass the given three radii as the arguments to the Ellipsoid_volume() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say Ellipsoid_volume() which takes the given three radii as
# the arguments and returns the volume of a given ellipsoid.


def Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss):
    # Inside the function, calculate the volume of the ellipsoid using the above given
    # mathematical formula, math.pi and store it in a variable.
    rslt_vol = 1.33 * math.pi * fst_radiuss * scnd_radiuss * thrd_radiuss
    # Return the above result.
    return rslt_vol


# Give the first radius as user input using the float(input()) function and 
# store it in a variable.
fst_radiuss = float(input("Enter some random number = "))
# Give the second radius as user input using the float(input()) function and 
# store it in another variable.
scnd_radiuss = float(input("Enter some random number = "))
# Give the third radius as user input using the float(input()) function and 
# store it in another variable.
thrd_radiuss = float(input("Enter some random number = "))
# Pass the given three radii as the arguments to the Ellipsoid_volume() function
# and print it.
print("The Volume of given ellipsoid with given radii {", fst_radiuss, scnd_radiuss, thrd_radiuss, "} = ",
      Ellipsoid_volume(fst_radiuss, scnd_radiuss, thrd_radiuss))

Output:

Enter some random number = 2
Enter some random number = 4.5
Enter some random number = 8
The Volume of given ellipsoid with given radii { 2.0 4.5 8.0 } = 300.8389125077586