Esempio n. 1
0
fig_ind = 1

for n in N:
	
	x = linspace(-1,1,1001)
	x_eq = array([-1.0+2.0*(i)/(n-1.0) for i in range(0,n)])
	x_ch = array([cos(((2.0*i+1)/n)*0.5*pi) for i in range(0,n)])
	
	f = zeros(n)
	
	for i in range(0,len(f)):
		f[i] = random.uniform(-1,1)
		
	
		
	c_eq = mi.solve(n,x_eq,f); c_ch = mi.solve(n,x_ch,f)
	
	print c_eq
	
	p_eq = mi.horner(c_eq,x); p_ch = mi.horner(c_ch,x)
	
	plt.figure(fig_ind)
	plt.plot(x,p_eq)
	plt.plot(x_eq,f,'o')
	plt.title('Random equidistant points')
	plt.savefig('Random_eq_N=%d.png' % n)
	fig_ind+=1
	
	plt.figure(fig_ind)
	plt.plot(x,p_ch)
	plt.plot(x_ch,f,'o')
Esempio n. 2
0
	
	x = linspace(-1,1,1001)
	x_eq = array([-1.0+2.0*(i)/(n-1.0) for i in range(0,n)])
	x_ch = array([cos(((2.0*i+1)/n)*0.5*pi) for i in range(0,n)])

	f_eq = f_book(x_eq); f_ch = f_book(x_ch)
	
	f_eq_p = zeros(len(f_eq)); f_ch_p = zeros(len(f_ch))
	
	for i in range(0,len(f_eq)):
		df = rd.uniform(-0.1,0.1)
		f_eq_p[i] = f_eq[i] + df
		f_ch_p[i] = f_ch[i] + df

	#Find coefficients for equidistant points and Chebyshev points
	c_eq   = mi.solve(n,x_eq,f_eq);   c_ch   = mi.solve(n,x_ch,f_ch)
	c_eq_p = mi.solve(n,x,f_eq_p) ;   c_ch_p = mi.solve(n,x_ch,f_ch_p)
	
	

	#Evaluate p_eq and p_ch for a larger set of x-values in [-1,1]
	p_eq = mi.horner(c_eq,x)  ;   p_ch = mi.horner(c_ch,x)
	q_eq = mi.horner(c_eq_p,x);   q_ch = mi.horner(c_ch_p,x)

	#Compute the errors, |p-f|, for equidistant points and Chebyshev points
	error_eq = abs(f_book(x)-p_eq); error_ch = abs(f_book(x)-p_ch)
	
	diff_qp_eq = abs(q_eq - p_eq); diff_qp_ch = abs(q_ch - p_ch) 

	
	plt.figure(fig_ind)