-1

I am solving Newton ralphson problem in which I am getting complex values of the first derivative, I have to plot the values of the first derivative vs the number of iterations, How do I plot the graph?

I am new to python so I am a little confused.

Edit: I want to plot the real part on the X-axis, the Imaginary on the Y, and the number of iterations on z.

1
  • I think @xFranko answered what I was asking, I just wanted to plot a 3D graph in python that had Real values of a number on the X-axis, Imaginary on the Y, and the iterations of newton ralphson on z.
    – Ansh
    Commented Feb 2, 2023 at 6:38

1 Answer 1

0

Updated per your comment:

import matplotlib.pyplot as plt

# Define the first_derivative and iterations data
first_derivative = [1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j, 5 + 5j]
iterations = [0, 1, 2, 3, 4]

# Split the first_derivative into real and imaginary parts
x = [val.real for val in first_derivative]
y = [val.imag for val in first_derivative]

# Create a figure and axis for the 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the data as a scatter plot
ax.scatter(x, y, iterations, c='r', marker='o')

# Set the x, y, and z labels
ax.set_xlabel('Real Part')
ax.set_ylabel('Imaginary Part')
ax.set_zlabel('Number of Iterations')

# Show the plot
plt.show()

enter image description here


Old Code

Using the matplotlib library:

import matplotlib.pyplot as plt

# Example values of first derivative and the number of iterations
first_derivative = [1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j, 5 + 5j]
iterations = [0, 1, 2, 3, 4]

# Plot the values of first derivative vs number of iterations
plt.plot(iterations, first_derivative, 'ro')
plt.xlabel('Number of iterations')
plt.ylabel('First derivative')
plt.title('First derivative vs Number of iterations')
plt.show()

the ro argument passed to the plot function is a format string that specifies the format of the data points to be plotted. The format string consists of two characters:

r: specifies the color of the data points as red.

o: specifies the shape of the data points as circles.

You can use different format strings to specify different colors and shapes for the data points. For example, bx would specify blue crosses, g^ would specify green triangles, and so on.

enter image description here

2
  • I think I miswrote the question, I wanted to plot a 3d graph i.e. x-axis for the real part, y for the imaginary, and z has the number of iterations
    – Ansh
    Commented Feb 1, 2023 at 2:06
  • Can it be done ?
    – Ansh
    Commented Feb 1, 2023 at 2:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.