Question

Write a function called polyplot that has one input array with three elements and no outputs....

Write a function called polyplot that has one input array with three elements and no outputs. The array elements A, B, and C are coefficients of a polynomial equation y=Ax^3+Bx^2+Cx. The function should plot the equation with a red line and blue x's for markers. There should be 20 values of x varying from -5 to 5. The plot title is "Plot of y=Ax^3+Bx^2+Cx" where the characters A, B, and C are replaced with the actual values from the function input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the function as per the requirement, I have explained everything in code. We created a function that takes an array as the argument. I have used matplotlib in python to generate the plot.

here is the output

here is text code:

===============================================================================

import numpy as np
import matplotlib.pyplot as plt

#here is the function, it accepts 3 arguments
def polyplot(arr):
   A=arr[0]
   B=arr[1]
   C=arr[2]
   x = [-5.0]
   y = [ A*x[0]**3 + B*x[0]**2 + C*x[0] ]
   #this loop generates values corresponding to given formula using A,B and C
   for i in range(1,20):
       x.append(x[i-1]+0.5)
       y.append(A*x[i]**3 + B*x[i]**2 + C*x[i])
   #plot red line
   plt.plot(x, y, '-r')
   #plot red x marker
   plt.plot(x, y, 'xb')
   #title
   T = 'plot of '+str(A)+'x^3 + '+str(B)+'x^2 +'+str(C)+'x'
   plt.title(T)
   #show plot
   plt.show()

#dummy values of A, B and C used to text function
arr = [4,3,2]
arr = np.array(arr)
polyplot(arr)


===================================================================================

Add a comment
Know the answer?
Add Answer to:
Write a function called polyplot that has one input array with three elements and no outputs....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT