CoCalc Shared Files6.1.sagews
Author: Georgy Lazarev
Views : 5
x = vector(RR, [-2.75,-2,-1,0.5,1])
y = vector(RR, [-0.2,-1.1,-2.3,0.1,1.1])
x[0]
#x
#y
n=4

def mnk(x,y,n,m):
G=matrix(RR,m+1)
b=vector(RR,m+1)
for j in range(m+1):
j
for i in range(n+1):
b[j]+=y[i]*(x[i])^j
b
for k in range(m+1):
for i in range(n+1):
G[j,k]+=x[i]^(k+j)
a=G.solve_right(b)
return a
mnk(x,y,4,3)
def value(m,v):
P=mnk(x,y,4,m)
vaL=0
for i in range(len(P)):
vaL+=P[i]*(v^i)
return vaL

value(3, 2)

def sigma(m):
q=0
for k in range(n+1):
q+=(value(m,x[k])-y[k])^2
return sqrt(q/(n-m))
sigmaarr = []
for i in range(4):
sigmaarr.append(sigma(i))

n

bar_chart(sigmaarr, width=1)

pointsarr = []
for i in range(len(x)):
pointsarr.append((x[i], y[i]))

vec3 = mnk(x,y,4,2)
vec2 = mnk(x,y,4,1)
vec1 = mnk(x,y,4,0)
t = var('t')
P = points(pointsarr, color='fuchsia', pointsize=50) + plot(vec3[0] * t^0 + vec3[1] * t^1 + vec3[2] * t^2, (t,-3,1), color='greenyellow') + plot(vec2[0] * t^0 + vec2[1] * t^1, (t,-3,1), color='mediumblue') + plot(vec1[0] * t^0, (t,-3,1), color='red')

show(P, aspect_ratio=1)

-2.75000000000000 (-1.16398854586130, 1.59108402684564, 0.791508903803130, 0.0261812975391491) 5.39366550335569 4