Skip to content

Instantly share code, notes, and snippets.

@b00033811
Last active May 28, 2018 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b00033811/48235d7fdf0f74dc6c5d2f656ee43bb6 to your computer and use it in GitHub Desktop.
Save b00033811/48235d7fdf0f74dc6c5d2f656ee43bb6 to your computer and use it in GitHub Desktop.
"""
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 p(x)
x=np.linspace(0,5,100)
p,_=evaluate_poly(x,A)
# Fix the random seed and add gauss. noise to the data.
np.random.seed(seed=1337)
e=np.random.normal(0,.7,len(x))
y=p+e
#Plotting
plt.scatter(x,y,label='y(x)=x+x^2-0.2x^3+e(x)',marker='.')
plt.plot(x,p,label='p(x)=x+x^2-0.2x^3',color='red')
plt.xlabel('x')
plt.ylabel('y(x)')
plt.legend()
plt.grid()
#np.savetxt('data.csv', (x,y), delimiter=',')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment