```python
from sympy import *
import numpy as np
import matplotlib.pyplot as plt
from PlottingSpectrum import generate_SED

def weighted_fitting(x_s, y_s, errs):
    list_Y = []
    list_A = []
    list_C = []
    
    for i in range(len(x_s)):
        list_Y.append([y_s[i]])
        list_A.append([1, x_s[i]])
        
        C_row = []
        
        counter = 0
        while counter < len(x_s):
            if counter == i:
                C_row.append(errs[i])
            else:
                C_row.append(0)
                
            counter += 1
        
        list_C.append(C_row)
            
        
    A = Matrix(list_A)
    Y = Matrix(list_Y)
    C = Matrix(list_C)
    
    X = ((A.T*C.inv()*A).inv())*(A.T*C.inv()*Y)
    
    return X[0, 0], X[1, 0]
```


```python
def calc_chi(x_s, y_s, errs, b, m):
    chi = 0
    for i in range(len(x_s)):
        chi += ((y_s[i] - (b + m*x_s[i]))**2)/(errs[i]**2)
    
    return chi                
```


```python
def read_file(name):
    lst_1 = []
    lst_2 = []
    lst_3 = []
    
    with open(name) as reader:
        for line in reader:
            list_str = line.split()
            if len(list_str) == 3:
                lst_1.append(float(list_str[0]))
                lst_2.append(float(list_str[1]))
                lst_3.append(float(list_str[2]))
    return lst_1, lst_2, lst_3
```


```python
x_s, y_s, errs = read_file('fitting_simple_data.txt')
            
b, m = weighted_fitting(x_s, y_s, errs)
#b = -10
#m = 6
print(b, m)
x_line = np.linspace(0, 12)
y_line = b + m*x_line       

plt.figure(figsize=(15, 9))
plt.scatter(x_s, y_s)

plt.plot(x_line, y_line)
plt.show()

print(calc_chi(x_s, y_s, errs, b, m))
```


```python
def linear_interpolation(x_s, y_s, x_val):
    index = np.abs(x_s - x_val).argmin() #Index of x in x_s closest to x_val
    
    if x_s[index] > x_val:
        l_index = index - 1
        r_index = index
    else:
        l_index = index
        r_index = index + 1
        
        
    m = (y_s[r_index] - y_s[l_index])/(x_s[r_index] - x_s[l_index])
    b = y_s[r_index] - m*x_s[r_index]
    
    return b + m*x_val

def spectrum_likelihood(wave_obs, spec_obs, wave_theo, spec_theo):
    epsilon = 0
    
    for i in range(len(wave_obs)):
        interpol_flux = linear_interpolation(wave_theo, spec_theo, wave_obs[i])
        
        epsilon += ((spec_obs[i] - interpol_flux)**2)/(errs[i]**2)
        
    return epsilon
```


```python
wave_obs, spec_obs, errs = read_file('fitting_spectrum_1.txt')

plt.figure(figsize=(15, 9))
plt.loglog(wave_obs, spec_obs)
x_label = r"$\lambda$ ($\AA$)"
plt.xlabel(x_label, fontsize=14)
plt.xlim(10**3, 199600.0)
y_label = r"f$_v$  $(\mu Jy)$"
plt.ylabel(y_label, fontsize=14)
plt.ylim(10**-14, 10**-8)
plt.show()
```


```python
generate_SED(10, 1)
```


```python

```

e9d6d8c4e51dd8b112a40bbb3e81388bf023a291
ipynb
Jupyter Notebook
Final Project/.ipynb_checkpoints/PhysicalProperties-checkpoint.ipynb
CalebLammers/CTA200
2b8e442f10479b8f82a9b8c4558a45aa9e791118
MIT
Final Project/.ipynb_checkpoints/PhysicalProperties-checkpoint.ipynb
CalebLammers/CTA200
2b8e442f10479b8f82a9b8c4558a45aa9e791118
MIT
Final Project/.ipynb_checkpoints/PhysicalProperties-checkpoint.ipynb
CalebLammers/CTA200
2b8e442f10479b8f82a9b8c4558a45aa9e791118
MIT
Qwen/Qwen-72B
1. YES
2. YES