My Coding >
Programming language >
Python >
Python libraries and packages >
Python NumPy >
Converting polar field to cartesian coordinates
Converting polar field to cartesian coordinatesIt is a common task to calculate vector fields in cartesian coordinates based on polar equations. For most visualisation and calculation procedures, cartesian coordinates are more natural and more straightforward, but for theoretical studies, polar (or cylindrical) coordinates often can be more helpful. In this example, I will show how to calculate vector fields in a cartesian coordinate system based on polar equations. Equations of functionsFirst of all, let's make an example of polar functions: \(F_r(r, \phi) = r^2 e^{-r^2}\) \(F_\phi(r, \phi) = r^2 e^{-r^2}\sin(10\phi)\) It is important to note, that in polar coordinates value of the function at R=0, should always be equal to 0 for radian and angular components, and can be any for vertical components (in cylindrical, 3D system). Conversion\(F_x = F_r \cdot \cos(\phi) - F_{\phi} \cdot \sin(\phi)\) \(F_y = F_r \cdot \sin(\phi) + F_{\phi} \cdot \cos(\phi)\) CodingIn Python, this task is pretty easy to solve with numpy. Polar functionsFor coding polar functions it is necessary to use numpy functions, rather than math functions, because we will apply them to a full domain in one go.
Cartesian domainThe final result will be stored in the Cartesian domain, so we need to create it. Personally, I prefer to use mgrid function from numpy. You can read what is the difference between mgrid and meshgrid. The domain will be squared from -3 to 3 with a grid size of 0.01
Then we need to calculate values for \( \textbf{r}\) and \( \textbf{\phi}\) for every cell of the domain, and then calculate values of the functions for every given point.
After calculating all values in polar form, it is necessary to convert them to cartesian form according to the equation given. And also I will calculate the overall field value.
DisplayingAfter calculation, we can visualise this field
Full codeThis is a full working code:
|
Last 10 artitles
9 popular artitles
|
|||
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |