## Question 4.  

<b style='color:red;'>The structure of the code is correct. You were asked to find the $\infty$- norm.</b>

#### Let find the $\infty$-norm condition of teh coefficients of the system of the normal equation $$X^{T}X\begin{pmatrix}
	m\\
	b
\end{pmatrix}=X^{T}Y$$
We have $\forall i\in\{1,2,\dots,7\},$
\begin{align*}
	 mx_{i}+b=y_{i}&\implies \begin{cases}
		mx_{1}+b=y_{1}\\
		mx_{2}+b=y_{2}\\
		\qquad	\vdots\\
		mx_{7}+b=y_{7}
	\end{cases}\\
&\implies\begin{pmatrix}
	x_{1}&1\\
	x_{2}&1\\
	\vdots&\vdots\\
	x_{7}&1
\end{pmatrix}\begin{pmatrix}
m\\
b
\end{pmatrix}=\begin{pmatrix}
y_{1}\\
\vdots\\
y_{2}\\
y_{7}\\
\end{pmatrix} \end{align*} 
We deduce $X=\begin{pmatrix}
	x_{1}&1\\
	x_{2}&1\\
	\vdots&\vdots\\
	x_{7}&1
\end{pmatrix}$ and $Y=\begin{pmatrix}
y_{1}\\
y_{2}\\
\vdots\\
y_{7}
\end{pmatrix}$
\begin{align*}
	X^{T}X\begin{pmatrix}
		m\\
		b
	\end{pmatrix}=X^{T}Y&\implies\begin{pmatrix}
	x_{1}&x_{2}&\dots&x_{7}\\
	1&1&\dots& 1\\
	\end{pmatrix}\begin{pmatrix}
x_{1}&1\\
x_{2}&1\\
\vdots&\vdots\\
x_{7}&1
\end{pmatrix}\begin{pmatrix}
m\\
b
\end{pmatrix}=\begin{pmatrix}
x_{1}&x_{2}&\dots&x_{7}\\
1&1&\dots& 1\\
\end{pmatrix}\begin{pmatrix}
y_{1}\\
y_{2}\\
\vdots\\
y_{7}
\end{pmatrix}\\
&\implies \begin{pmatrix}
	x_{1}^{2}+\dots+x_{7}^{2}&	x_{1}^{}+\dots+x_{7}^{}\\
	x_{1}^{}+\dots+x_{7}^{}&1+\dots+1
\end{pmatrix}\begin{pmatrix}
m\\
b
\end{pmatrix}=\begin{pmatrix}
x_{1}y_{1}+\dots+x_{7}y_{7}\\
y_{1}+\dots+y_{7}
\end{pmatrix}
\end{align*}
Let $A=\begin{pmatrix}
	x_{1}^{2}+\dots+x_{7}^{2}&	x_{1}^{}+\dots+x_{7}^{}\\
	x_{1}^{}+\dots+x_{7}^{}&1+\dots+1
\end{pmatrix}$ the $\infty$-norm condition is the $\infty$-norm condition of $A$ 



```python
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import *
```


```python
Xi=np.array([1.001,1.002,1.003,1.004,1.005,1.006,1.007])
yi=np.array([3.665,3.789,3.891,4.022,4.233,5.200,5.329])
I=np.ones(7)
```


```python
A=np.zeros((2,2))
A[0,0],A[0,1],A[1,0],A[1,1]=sum(Xi**2),sum(Xi),sum(Xi),sum(I)

#A=np.array([[sum(Xi**2)],[sum(Xi)],[sum(Xi)],[sum(I)]])
A
```




    array([[7.05614, 7.028  ],
           [7.028  , 7.     ]])



### Conditionnal number


```python
print('The conditionnal number of the system is',np.linalg.cond(A))
```

    The conditionnal number of the system is 1008034.0799851977


### interpretation: 
The conditionnal number of the system is very big mean that the system is ill-conditionned

## Question 5

### Use python to determine the orbital parameter

We have for all $i\in\{1,2,\dots,10\}$  $ay_{i}^{2}+bx_{i}y_{i}+cx_{i}+dy_{i}+e=x_{i}^{2}$ ie :
\begin{align}
	\begin{pmatrix}
		y_{1}^{2}&x_{1}y_{1}&x_{1}&y_{1}&1\\
		y_{2}^{2}&x_{2}y_{2}&x_{2}&y_{2}&1\\
		\vdots&\vdots&\vdots&\vdots&\vdots\\
		y_{10}^{2}&x_{10}y_{10}&x_{10}&y_{10}&1
	\end{pmatrix}\begin{pmatrix}
	a\\
	b\\
	c\\
	d\\
	e
\end{pmatrix}=\begin{pmatrix}
x_{1}^{2}\\
x_{2}^{2}\\
\vdots\\
x_{10}^{2}
\end{pmatrix}
\end{align}
Let $A=\begin{pmatrix}
	y_{1}^{2}&x_{1}y_{1}&x_{1}&y_{1}&1\\
	y_{2}^{2}&x_{2}y_{2}&x_{2}&y_{2}&1\\
	\vdots&\vdots&\vdots&\vdots&\vdots\\
	y_{10}^{2}&x_{10}y_{10}&x_{10}&y_{10}&1
\end{pmatrix}$ Since $A$ is not a square matrix, we can multiply equation (3) by the transpose of $A$ and take the inverse of $A^{T}A$ to have the parameters ie 
\begin{align*}
	A^{T}A\begin{pmatrix}
		a\\
		b\\
		c\\
		d\\
		e
	\end{pmatrix}=\begin{pmatrix}
	x_{1}^{2}\\
	x_{2}^{2}\\
	\vdots\\
	x_{10}^{2}
\end{pmatrix}\implies \begin{pmatrix}
a\\
b\\
c\\
d\\
e
\end{pmatrix}=(A^{T}A)^{-1}A^{T}\begin{pmatrix}
x_{1}^{2}\\
x_{2}^{2}\\
\vdots\\
x_{10}^{2}
\end{pmatrix}
\end{align*}

### printing the coefficient


<b style='color:red;'>The structure of the code and output of the orbital parameters are correct.</b>


```python
xi=np.array([1.02,0.95,0.87,0.77,0.67,0.56,0.44,0.30,0.16,0.01])
yi=np.array([0.39, 0.32 ,0.27, 0.22, 0.18, 0.15 ,0.13, 0.12, 0.13 ,0.15])
```


```python
A=np.array([yi**2,xi*yi,xi,yi,np.ones(10)]).T
A
```




    array([[0.1521, 0.3978, 1.02  , 0.39  , 1.    ],
           [0.1024, 0.304 , 0.95  , 0.32  , 1.    ],
           [0.0729, 0.2349, 0.87  , 0.27  , 1.    ],
           [0.0484, 0.1694, 0.77  , 0.22  , 1.    ],
           [0.0324, 0.1206, 0.67  , 0.18  , 1.    ],
           [0.0225, 0.084 , 0.56  , 0.15  , 1.    ],
           [0.0169, 0.0572, 0.44  , 0.13  , 1.    ],
           [0.0144, 0.036 , 0.3   , 0.12  , 1.    ],
           [0.0169, 0.0208, 0.16  , 0.13  , 1.    ],
           [0.0225, 0.0015, 0.01  , 0.15  , 1.    ]])




```python
coef=np.linalg.inv(A.T@A)@A.T@(xi**2)
print(coef)
```

    [-2.63562548  0.14364618  0.55144696  3.22294034 -0.43289427]


### ploting of the resulting and the given data


```python
def F(y,x,p):
    a,b,c,d,e=p
    return a*y**2+b*x*y+c*x+d*y-x**2+e
t=np.linspace(0,1,40)
q=coef.T
z=[fsolve(F,0,args=(i,q)) for i in t]

```


```python
plt.plot(t,z,color='b',lw='6')
plt.plot(xi,yi, 'r--',lw='5')
plt.legend(['given data','elliptical orbit'])
plt.show()
```

### 2. This least squares problem is nearly rank- deficient. To see what ef-fect this has on the solution, perturb the input by adding to each data point a random number uniformly distributed on the interval [−0.005,0.005] and solve the least squares problem with perturbed data. What effect does this difference has on the plot of the orbits ? Ca you explain this behavior.




<b style='color:red;'>The pertubation added to the input data is correct. But the plot is not as expected, check again the strategy used.</b>


```python
import random
```


```python
x=np.zeros(len(xi))
for i in range(len(xi)):
    x[i]=xi[i]+random.uniform(-0.005,0.005) 
    
```


```python
q=coef.T
T=list(x)
z1=[fsolve(F,0,args=(i,q)) for i in T]
```


```python
z1
```




    [array([0.39003906]),
     array([0.32677984]),
     array([0.27072941]),
     array([0.21664325]),
     array([0.1789049]),
     array([0.14983222]),
     array([0.13036778]),
     array([0.1213942]),
     array([0.12733398]),
     array([0.15043457])]




```python
t=np.linspace(0,1,40)
plt.plot(z1,x)
plt.plot(t,z)
```

 ### we can see that a small pertubation in the input gives a big change in the output. This behavior come from the fact that the system is not well conditioned because we have:


```python
print('The conditional number of the system is',np.linalg.cond(A.T@A))
```

    The conditional number of the system is 473935.10331647756


Who is very big ie the system is ill conditioned


```python

```

7628b90e1507f8c96819a9e11750e37a9459511d
ipynb
Jupyter Notebook
louismozart_teyou_NLA2.ipynb
Louis-Mozart/Louis-Mozart.github.io
ffc11437fb47fa4007b47ef0bf82388de132cca1
MIT
louismozart_teyou_NLA2.ipynb
Louis-Mozart/Louis-Mozart.github.io
ffc11437fb47fa4007b47ef0bf82388de132cca1
MIT
louismozart_teyou_NLA2.ipynb
Louis-Mozart/Louis-Mozart.github.io
ffc11437fb47fa4007b47ef0bf82388de132cca1
MIT
Qwen/Qwen-72B
1. YES
2. YES