How to Put Pi in MATLAB

In this article, we will delve into the process of working with pi within the MATLAB programming environment. We will discuss how pi is represented in MATLAB, different ways to retrieve its value, and …

Updated November 8, 2023

Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

In this article, we will delve into the process of working with pi within the MATLAB programming environment. We will discuss how pi is represented in MATLAB, different ways to retrieve its value, and also provide a brief overview of pi’s significance in mathematics and science. Furthermore, the article will include sample code snippets to help you understand better the concepts being presented.


Pi (γ), commonly denoted by the Greek letter π, is an irrational number that has been a central concept in the fields of mathematics, physics, and science since ancient times. It can be found in numerous mathematical formulas involving circles, spheres, and other similar shapes. MATLAB, being a powerful computing environment, provides various tools for manipulating pi within its system. In this tutorial, we will explore different methods to put pi into MATLAB and how it is utilized.

Representing Pi in MATLAB

In MATLAB, pi can be represented using the built-in function pi(). This function returns an approximation of the mathematical constant π, accurate up to machine precision. The value obtained will depend on your system’s floating-point settings and may vary slightly across different platforms. To display the value, you simply need to type ‘pi()’ into MATLAB’s command line. You can also store this value in a variable for later use by assigning it to a variable such as MyPi = pi();.

To verify that the stored value of pi is indeed a close approximation of the mathematical constant, you can compare it to another well-known representation of pi, which is 22/7. You can use MATLAB’s rounding function, round(), to ensure that both values match up to three decimal places: round(pi/22/7) and round(MyPi/22/7). The results should be very close, as MATLAB is designed to produce accurate numerical values.

Additional Ways to Obtain Pi in MATLAB

Apart from the built-in pi() function, you can also obtain the value of pi by utilizing MATLAB’s double precision arithmetic with a simple calculation involving square roots: pi = sqrt(2*sqrt(2)). This alternative method is derived from one of the many known approximations for pi and works out to approximately 3.14159265358979, which is close enough for most practical purposes.

Using Pi in MATLAB

Now that we have successfully imported pi into our MATLAB environment, we can begin using it for various mathematical operations and calculations involving circles or spheres. Some common examples include computing the circumference of a circle or determining the surface area of a sphere. We will explore these concepts through sample code snippets to illustrate their usage in practical scenarios.

  1. Calculating Circumference: The formula for calculating the circumference of a circle involves pi and the radius (r) of the circle, as shown below:

    circumference = 2*pi*radius

We can implement this formula using MATLAB’s arithmetic operators. Consider a circle with a radius value stored in the variable ‘Radius’. We can write the following code to compute its circumference:

Circumference = 2 * pi * Radius;

In this example, we used the stored value of pi from our earlier calculations (MyPi).

  1. Calculating Surface Area of a Sphere: To calculate the surface area of a sphere, we use the following formula where r represents the radius of the sphere:

    surfaceArea = 4*pi*r^2

We can use MATLAB to compute this value as well. Let’s store the sphere’s radius in ‘RadiusSphere’ and use it within the same arithmetic expression we utilized earlier with our circumference calculation:

SurfaceArea = 4 * pi * RadiusSphere^2;

These examples demonstrate how MATLAB can be employed effectively for various mathematical operations involving pi, providing accurate results that align closely with known approximations of the mathematical constant.


In this extensive tutorial on “how to put pi in MATLAB”, we have discussed the different methods available for obtaining and working with pi within this programming environment. We learned how to use built-in functions, alternative calculations, and mathematical formulas that involve pi to solve real-world problems like computing the circumference of a circle or surface area of a sphere. We also provided sample code snippets to demonstrate these concepts in action. By understanding the importance of pi and utilizing MATLAB’s powerful capabilities, you can further enhance your programming skills and apply them across diverse fields of study.