<jupyter_start><jupyter_text>Function with one variable (line) $$\fbox{$f(x) = a + b x$}$$Boundary conditions:$$\begin{array}{rcl}f(-2) &=& 0 \\f(2)  &=& 1\end{array}$$Rewrite into system of linear equations:$$\begin{array}{rcl}a + b(-2) &=& 0 \\a + b(2)  &=& 1\end{array}$$or in matrix form:$$\left[\begin{array}{cc}1 & -2 \\1 & 2\end{array}\right]\left[\begin{array}{c}a \\ b\end{array}\right]= \left[\begin{array}{c}0 \\ 1\end{array}\right]$$<jupyter_code>def f1D(x,a,b):
    y = a + b*x
    return y
A = np.array([[1,-2], [1,2]])
b = np.array([0,1])
ab = np.linalg.solve(A, b)
print(ab)
points = [[-2,2],[0,1]]
x = np.linspace(-2,2,11)
plt.xlim([-2.1,2.1])
plt.ylim([-1,2])
plt.plot(points[0],points[1],linewidth=0,marker='o',color='red',markersize=10)
plt.plot(x,f1D(x,a=ab[0],b=ab[1]))<jupyter_output><empty_output><jupyter_text>Function with two variables (plane)$$\fbox{$f(x,y) = a + b x + c y$}$$Boundary conditions:$$\begin{array}{rcl}f(-2,2) &=& 0.1 \\f(2,2)  &=& 0.5 \\f(2,-2) &=& 1\end{array}$$Rewrite into system of linear equations:$$\begin{array}{rcl}a + b(-2) + c(2) &=& 0.1 \\a + b(2) + c(2)  &=& 0.5 \\a + b(2) + c(-2) &=& 1  \end{array}$$or in matrix form:$$\left[\begin{array}{ccc}1 & -2 &  2 \\1 & 2 & 2 \\1 & 2 & -2\end{array}\right]\left[\begin{array}{c}a \\ b \\c\end{array}\right]= \left[\begin{array}{c}0.1 \\ 0.5 \\ 1\end{array}\right]$$<jupyter_code>def f2D(x,y,a,b,c):
    f2D = a + b*x + c*y
    return f2D
A = np.array([[1,-2,2], [1,2,2], [1,2,-2]])
b = np.array([0.1,0.5,1])
abc = np.linalg.solve(A, b)
print(abc)
print(f2D(-2,-2,abc[0],abc[1],abc[2]))
points = np.array([[-2,2,2],[2,2,-2],[0.1,0.5,1]])
step=0.25
X = np.arange(-2, 2+step, step)
Y = np.arange(-2, 2+step, step)
X, Y = np.meshgrid(X, Y)
Z = f2D(X,Y,abc[0],abc[1],abc[2])

fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')
ax.view_init(elev=30., azim=290.)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_xlim([-2.1,2.1])
ax.set_ylim([-2.1,2.1])
ax.set_zlim([-0.0,1.5])
#ax.set_aspect("equal")

for i in range(points.shape[0]):
    ax.plot([points[0,i],points[0,i]],[points[1,i],points[1,i]],[0,points[2,i]],color='black')
ax.scatter(points[0], points[1], points[2], c=points[2],cmap=cm.gist_rainbow,marker='o',s=200)
cbar=ax.plot_surface(X, Y, Z, cmap=cm.gist_rainbow,linewidth=0,alpha=0.5,antialiased=False)
cbar_label=fig.colorbar(cbar, shrink=0.5, aspect=10)
cbar_label.set_label('z')<jupyter_output><empty_output><jupyter_text>----  Function with two variables (curved)$$\fbox{$f(x,y) = a + b x + c y + d x y + e x^2 + f y^2$}$$<jupyter_code>def f2Dcurved(x,y,a,b,c,d,e,f):
    f2D = a + b*x + c*y + d*x*y + e*x**2 + f*y**2
    return f2D
points = np.array([[-2,2,2],[2,2,-2],[0.1,0.5,1]])
step=0.25
X = np.arange(-2, 2+step, step)
Y = np.arange(-2, 2+step, step)
X, Y = np.meshgrid(X, Y)
Z = f2Dcurved(X,Y,abc[0],abc[1],abc[2],0.0,0.1,0.)

fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')
ax.view_init(elev=20., azim=290.)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_xlim([-2.1,2.1])
ax.set_ylim([-2.1,2.1])
ax.set_zlim([-0.0,2.1])
#ax.set_aspect("equal")

for i in range(points.shape[0]):
    ax.plot([points[0,i],points[0,i]],[points[1,i],points[1,i]],[0,points[2,i]],color='black')
ax.scatter(points[0], points[1], points[2], c=points[2],cmap=cm.gist_rainbow,marker='o',s=200)
surf = ax.plot_surface(X, Y, Z, cmap=cm.gist_rainbow,linewidth=0,alpha=0.5,antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2814456459874787, 'fraction_numerical': 0.06943653955606147, 'mean_word_length': 5.310592459605027, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '452714', 'license': 'MIT', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1728, 'n_words': 242, 'path': '.ipynb_checkpoints/Fitting_solveLinearSystem-checkpoint.ipynb', 'repo_name': 'georgkaufmann/lecture_agII', 'size': '3514'}