Write a Python program which takes any data set (1, 1), (2, 92),... (N, YN) and computes the best fit polynomial P(x) = a + a₁ + a₂x² +...+akak, for any polynomial order K. I have provided codes that do this for K = 1 (linear best fit), K = 2 (quadratic best fit), and K = 4 (quartic best fit).

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Please use python and show codes
Write a Python program which takes any data set
(1, 1), (2, y2),... (*N, YN)
and computes the best fit polynomial
P(x)= a + a₁ + a₂x² + ....
+akak,
for any polynomial order K. I have provided codes that do this for K = 1
(linear best fit), K = 2 (quadratic best fit), and K = 4 (quartic best fit).
You need to generalize this code to work for any K.
Transcribed Image Text:Write a Python program which takes any data set (1, 1), (2, y2),... (*N, YN) and computes the best fit polynomial P(x)= a + a₁ + a₂x² + .... +akak, for any polynomial order K. I have provided codes that do this for K = 1 (linear best fit), K = 2 (quadratic best fit), and K = 4 (quartic best fit). You need to generalize this code to work for any K.
import time
import math
import matplotlib.pyplot as plt
import numpy as np
#def f(x):
#
return 1.76 *x + 2.5
def f(x):
return 2*x**4 - 0.5*x**3 + 0.5*x**2 - x + 0.25
N = 7
a = -2
b = 2
step = (b-a)/N
X = np.linspace(a, b, N)
Y = np.zeros (N)
the_random_noise = np.random.random (N)
for n in range (0,N):
X[n]X[n] +
((-1) **n) *0.1*step*the_random_noise[n]
for n in range (0,N):
Y[n] = f(X[n]) +
((-1) **n) *0.5*the_random_noise[n]
#Try quartic best fit: K = 4 (5 equations in 5
unknowns a0, al, a2, a3, a4):
# N = 7 Data points
b0= 0
O O O O O
b1 = 0
b2 =
b3
b4
0
A1 = 0
A2 = 0
A3 = 0
A4 = 0
0
= 0
=
A5 = 0
A6 = 0
A7 = 0
A8 = 0
for i in range(0, N):
b0b0 + Y[i]
Transcribed Image Text:import time import math import matplotlib.pyplot as plt import numpy as np #def f(x): # return 1.76 *x + 2.5 def f(x): return 2*x**4 - 0.5*x**3 + 0.5*x**2 - x + 0.25 N = 7 a = -2 b = 2 step = (b-a)/N X = np.linspace(a, b, N) Y = np.zeros (N) the_random_noise = np.random.random (N) for n in range (0,N): X[n]X[n] + ((-1) **n) *0.1*step*the_random_noise[n] for n in range (0,N): Y[n] = f(X[n]) + ((-1) **n) *0.5*the_random_noise[n] #Try quartic best fit: K = 4 (5 equations in 5 unknowns a0, al, a2, a3, a4): # N = 7 Data points b0= 0 O O O O O b1 = 0 b2 = b3 b4 0 A1 = 0 A2 = 0 A3 = 0 A4 = 0 0 = 0 = A5 = 0 A6 = 0 A7 = 0 A8 = 0 for i in range(0, N): b0b0 + Y[i]
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY