Last active
May 28, 2018 00:42
-
-
Save b00033811/6fbd2c36866f6bfa65c85292ac6b9326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Polynomials | |
@author: Abdullah Alnuaimi | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def fit_poly(a,k): | |
'''returns a function of the dot product (A=V.a) ''' | |
A=lambda x,a=a,k=k:[[a*n**k for a,k in zip(a,k)] for n in x] | |
return A | |
def evaluate_poly(x,A): | |
''' evaluates A=V.a,stores it in matrix form, and | |
returns a list y(x)=[A0,..An]''' | |
y=[sum(i) for i in A(x)] | |
return y,A(x) | |
############################## Main ######################################### | |
# y(x)=x+x^2-0.2x^3 | |
coefficients=[0,1,1,-.2] # polynomial | |
degree=[0,1,2,3] | |
A=fit_poly(coefficients,degree) # returns A(x0)...A(Xn) | |
# Evaluate the functions and returns y(x) | |
x=np.linspace(0,5,20) | |
y,_=evaluate_poly(x,A) | |
#Plotting | |
plt.plot(x,y,label='p(x)=x+x^2-0.2x^3') | |
plt.xlabel('x') | |
plt.ylabel('y(x)') | |
plt.legend() | |
plt.grid() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment