Commit cca2b177 authored by Cyril L'Orphelin's avatar Cyril L'Orphelin
Browse files

Merge branch 'JulienPeloton/tp3' into 'master'

Ajout du materiel pour TP3

See merge request !24
parents 7a6e22ac 2760fdde
x,y
0.0,1.7562656254169835
0.5,3.3478933570678633
1.0,4.6624545488463776
1.5,5.125083079978194
2.0,4.769628703005167
2.5,3.815725656824885
3.0,2.205250223212375
3.5,1.0152716559103214
4.0,-0.27440408274510014
4.5,-1.3166591796810758
5.0,-0.9482172345959369
5.5,-0.27143471990585943
6.0,1.2668984010815398
6.5,2.5331431932836015
7.0,3.8707778121598366
7.5,4.986432853548176
8.0,4.564295893398583
8.5,4.046935739152962
9.0,3.267502080592287
9.5,2.1900301490557426
10.0,-0.0184758061439797
10.5,-0.43140929023368724
11.0,-1.1009941045071576
11.5,-0.767555722690707
12.0,0.3265505097367498
12.5,1.553778850694469
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sys
def load_data(fn):
data = pd.read_csv(fn, sep=',')
return data
def model_function(x, a, b):
return a * np.sin(x) + b
def fit_data(x, y):
params, cov = curve_fit(model_function, x, y)
err = np.sqrt(np.diag(cov))
return params, err
def plot_fit(x, y, params, errors):
plt.plot(
x, y, ls='', marker='o',
label='data'
)
plt.plot(
x, model_function(x, *params), '--',
label='fit'.format(*params)
)
plt.title(
r'$f(x) = a \times sin(x) + b$, with a = {:.2f} $\pm$ {:.2f}, b = {:.2f} $\pm$ {:.2f}'.format(
*np.transpose((params, errors)).flatten()
)
)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
def main():
""" TBD """
fn = sys.argv[1]
data = load_data(fn)
params, errors = fit_data(data['x'], data['y'])
plot_fit(data['x'], data['y'], params, errors)
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment