Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: two
Views: 74
Kernel: Python 2 (SageMath)
#num 7 import numpy as np import matplotlib.pyplot as pl
fn = lambda x: x / (3 + x**2)
fn(5)
0
def fun(x): return x / (3 + x**2)
fun(2)
0
fun(np.arange(0.2, 0.7, 0.1))
array([ 0.06578947, 0.09708738, 0.12658228, 0.15384615, 0.17857143])
np.arange(0.2, 0.6, 0.1)
array([ 0.2, 0.3, 0.4, 0.5])
x = np.linspace(-5,5,28) pl.plot(x, fun(x), x, Nutint(), 'r') pl.grid(1)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-9-abe3c2d68723> in <module>() 1 x = np.linspace(-5,5,28) ----> 2 pl.plot(x, fun(x), x, Nutint(), 'r') 3 pl.grid(1) NameError: name 'Nutint' is not defined
def interp(func, n=30, dis=0): x = np.linspace(-5, 5, n) y = [func(x + dis) for x in x] dy = [(y[1:][j] - y[j])*n for j in range(n-1)] ddy = [(y[2:][j] - 2*y[1:][j] + y[j])*(n**2)/2 for j in range(n-2)] return [x, y, dy, ddy] res = interp(fun)
def Nutint(x = np.linspace(-5, 5, 30) , lst=np.linspace(-5, 5, len(res[-1])), res=res): return [y[i] + (lst[i] - x[i])*dy[i] + (lst[i] - x[i])*(lst[i] - x[1:][i])**2*ddy[i] for i in range(len(res[-1]))] Nutint()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-11-d489d6ccbb24> in <module>() 1 def Nutint(x = np.linspace(-5, 5, 30) , lst=np.linspace(-5, 5, len(res[-1])), res=res): 2 return [y[i] + (lst[i] - x[i])*dy[i] + (lst[i] - x[i])*(lst[i] - x[1:][i])**2*ddy[i] for i in range(len(res[-1]))] ----> 3 Nutint() <ipython-input-11-d489d6ccbb24> in Nutint(x, lst, res) 1 def Nutint(x = np.linspace(-5, 5, 30) , lst=np.linspace(-5, 5, len(res[-1])), res=res): ----> 2 return [y[i] + (lst[i] - x[i])*dy[i] + (lst[i] - x[i])*(lst[i] - x[1:][i])**2*ddy[i] for i in range(len(res[-1]))] 3 Nutint() NameError: global name 'y' is not defined
n=30 x = np.linspace(0, 1, n) y = [fun(i) for i in x] dy = [(y[1:][j] - y[j])*n for j in range(n-1)] ddy = [(y[2:][j] - 2*y[1:][j] + y[j])*(n**2)/2 for j in range(n-2)]
len(ddy)
len(np.linspace(0, 1, len(res[-1])))
28
#лагранж
fn = lambda x: x / (3 + x**2) x = np.linspace(-5, 5, 15);x fzn = fn(x);h = 0.71428571428571441
print(x[2] - x[1], x[1] - x[0])
(0.71428571428571441, 0.71428571428571441)
[x[1:][i] - x[i] for i in range(len(x)-1)]
[0.71428571428571441, 0.71428571428571441, 0.71428571428571397, 0.71428571428571441, 0.71428571428571441, 0.71428571428571397, 0.71428571428571441, 0.71428571428571441, 0.71428571428571441, 0.71428571428571441, 0.71428571428571441, 0.71428571428571352, 0.7142857142857153, 0.71428571428571352]

#5

def Lagr(func, arr, x): h = abs(arr[1] - arr[0]) farr = [func(u) for u in arr] Q = [] for i in range(len(x)-1): ind = 1 znc = 0 for k in range(len(farr)): for j in range(len(arr)): if i == j: continue ind *= (x[i] - arr[j])/h znc += farr[k]*ind Q += [znc] return (np.array(Q + [Q[-1]]) )
n = 5 arr = np.linspace(-5,5, n) x = np.linspace(-5, 5, 20) print(fun(x)) (Lagr(fun, arr, x))
[-0.17857143 -0.19439095 -0.21243292 -0.23266767 -0.25438169 -0.27509653 -0.28812825 -0.27810304 -0.21788991 -0.08574007 0.08574007 0.21788991 0.27810304 0.28812825 0.27509653 0.25438169 0.23266767 0.21243292 0.19439095 0.17857143]
array([ 4.86176733e+19, -6.10976013e+15, -3.51541183e+14, -8.64111238e+12, -2.44876509e+10, -3.65485503e+13, -3.15654130e+16, -8.42013680e+16, -2.47316619e+16, -1.81953563e+14, 1.82502941e+14, 2.47595987e+16, 8.42758047e+16, 3.15993704e+16, 3.67007440e+13, -1.87520234e+16, -1.56110019e+18, -8.35180342e+18, -4.53334097e+18, -4.53334097e+18])
len(x)
20
print(len(x)) n = 5 arr = np.linspace(-5,5, n) x = np.linspace(-5, 5, 20).tolist() len(Lagr(fun, arr, x))
20
20
n = 3 arr = np.linspace(-5, 5, n) x = np.linspace(-5, 5, 20) LA = Lagr(fun, arr, x).tolist() pl.plot(x+0.2, fun(x),'*', x, np.array([0] + LA[1:])*5, 'r') pl.grid(1)
Image in a Jupyter notebook
x[0]-x[1]
-0.5263157894736841
import numpy as np import matplotlib.pyplot as plt n = 4 x = np.linspace(-5, 5, n) y = fun(x) def lagranz(x,y,t): z = 0 for j in range(len(y)): p1 = 1 p2 = 1 for i in range(len(x)): if i==j: p1 = p1*1 p2 = p2*1 else: p1 *= (t-x[i]) p2 *= (x[j]-x[i]) z += y[j]*p1/p2 return z
xnew=np.linspace(np.min(x),np.max(x),100) ynew=[lagranz(x,y,i) for i in xnew] plt.plot(x,y,'o',xnew,ynew) plt.grid(True) plt.show()
Image in a Jupyter notebook
plt.plot(x,y,'^')
[<matplotlib.lines.Line2D at 0x7f09c69898d0>]
Image in a Jupyter notebook
#######################
n = 25 x = np.linspace(-5,5,n) y = fn(x) def product( val, n ): mul = 1 for i in range(n): if i: mul *= val - x[i-1] yield mul C=[] for n in range(len(x)): p = product( x[n], n+1 ) C.append( (y[n]-sum(C[k]*next(p) for k in range(n)) )/next(p) ) def f( v ): return sum(C[k]*p for k, p in enumerate(product(v, len(C)) ))
pl.plot(x, fun(x+0.05), '*', x, f(x)) pl.grid(1)
Image in a Jupyter notebook