Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Cyril L'Orphelin
template-tp
Commits
2760fdde
Commit
2760fdde
authored
Oct 08, 2021
by
Julien
Browse files
Ajout du materiel pour TP3
parent
7a6e22ac
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
0 deletions
+81
-0
TP3/tp3-data.csv
TP3/tp3-data.csv
+27
-0
TP3/tp3.py
TP3/tp3.py
+54
-0
No files found.
TP3/tp3-data.csv
0 → 100644
View file @
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
TP3/tp3.py
0 → 100644
View file @
2760fdde
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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment