Matlab

How to Use Pi in MATLAB

Pi is not just a Greek letter, though. It is the circle’s circumference ratio to its diameter, which is approximately 3.14159. But it’s much more in the era of mathematics. It appears in trigonometry, statistics, MATLAB, and many other fields. In this article, we are going to discuss how to use pi in MATLAB using some practical examples.

How to Use Pi in MATLAB?

The pi is a built-in function in MATLAB that returns the circle’s circumference ratio to its diameter which is roughly 3.14159 value equivalent to pi. Simply type pi in the command window or script to utilize pi in MATLAB. Pi can be used in mathematical formulas, such as figuring out the area of the circle. This function has the following syntax in MATLAB.

p = pi

Here,
The expression p = pi provides the IEEE floating-point number with double precision that is closest to the value of π.

Examples

Let’s consider some practical examples that illustrate the use of the pi in MATLAB.

Example 1

The simple MATLAB code that is provided below returns the value having double precision, with fifteen digits after the decimal point.

format long
p = pi

Example 2

This MATLAB code calculates the circle’s area utilizing the pi function in MATLAB.

r = 9;
A = pi*r^2

Example 3

In this example, we use the pi function for calculating the surface area and the volume of the sphere.

r = 9;
S_Area = 4*pi*r^2
S_Volume = 4/3*pi*r^3

Conclusion

The built-in MATLAB function pi returns the circumference-to-diameter ratio of a circle, which is roughly 3.14159 equal to the value of pi. To use pi in MATLAB, just type pi in the command window or script. It is used in trigonometry, statistics, MATLAB, and many other fields. This guide taught us how to use pi in MATLAB using some practical examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.