193k views
5 votes
Write a python program to find and plot n-th root of the complex numbers in the polar coordinates, where the value of n is to be computed as the sum of your register number until it reduces to a single digit, where the register number is a user input given by you. The plot must have appropriate customizations.

For example, if register number is 1848803, you need to take it as a user input and find the sum of its digits 1+8+4+8+8+0+3=32, again iterate 3+2 = 5 using python and take the value 5 as n and find the fifth roots of unity and plot them.

User Baligena
by
8.6k points

2 Answers

0 votes

Answer:

import cmath

import matplotlib.pyplot as plt

# Take register number as input

register_number = input("Enter your register number: ")

# Compute the sum of digits of the register number

n = sum(int(digit) for digit in register_number)

while n >= 10:

n = sum(int(digit) for digit in str(n))

# Find the nth roots of unity

roots = [cmath.exp(2j * cmath.pi * k / n) for k in range(n)]

# Convert roots to polar coordinates

r = [cmath.polar(root)[0] for root in roots]

theta = [cmath.polar(root)[1] for root in roots]

# Plot the roots in the polar coordinates

fig = plt.figure()

ax = fig.add_subplot(111, projection='polar')

ax.plot(theta, r, 'o', markersize=10)

ax.set_rticks([0.5, 1, 1.5, 2]) # Customize the radial ticks

ax.set_rlabel_position(-22.5) # Offset radial labels from plotted line

ax.grid(True)

plt.title(f"{n}-th root of complex numbers in polar coordinates")

plt.show()

Sample Input & Output:

Enter your register number: 1848803

Output:
"Attached Below"

Step-by-step explanation:

Here is how the program works:

The user is prompted to enter their register number.

The program computes the sum of digits of the register number and reduces it to a single digit by iterating until the sum is less than 10.

The program finds the nth roots of unity using the cmath.exp function and a for loop.

The program converts the roots to polar coordinates using the cmath.polar function.

The program plots the roots in the polar coordinates using the matplotlib.pyplot module and its plot and subplot functions. It also customizes the radial ticks, radial label position, and adds a grid to the plot.

Write a python program to find and plot n-th root of the complex numbers in the polar-example-1
User Minnie
by
8.0k points
7 votes

import cmath

import matplotlib.pyplot as plt

# Get user's register number as input

register_number = input("Enter your register number: ")

# Compute the sum of digits until a single digit is obtained

while len(register_number) > 1:

register_number = str(sum(int(digit) for digit in register_number))

# Use the single digit as the value of n

n = int(register_number)

# Compute the n-th roots of unity in polar coordinates

roots = [cmath.rect(1, 2 * k * cmath.pi / n) for k in range(n)]

# Extract the real and imaginary parts of the roots

real_parts = [z.real for z in roots]

imag_parts = [z.imag for z in roots]

# Create a scatter plot of the roots in the complex plane

plt.scatter(real_parts, imag_parts)

plt.title(f"{n}-th Roots of Unity")

plt.xlabel("Real Part")

plt.ylabel("Imaginary Part")

plt.grid()

plt.show()

Write a python program to find and plot n-th root of the complex numbers in the polar-example-1
Write a python program to find and plot n-th root of the complex numbers in the polar-example-2
User Ramzan Mahmood
by
7.8k points

No related questions found