Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Michael Trumbull - Project 1 #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added projects/.DS_Store
Binary file not shown.
Binary file added projects/one/.DS_Store
Binary file not shown.
Binary file added students/.DS_Store
Binary file not shown.
Binary file added students/one_MichaelTrumbull/.DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions students/one_MichaelTrumbull/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
3 -4.621424
3.25 -5.804482
3.5 -6.70809
3.75 -7.398549
4 -7.922563
4.25 -8.316091
4.5 -8.606262
4.75 -8.813852
5 -8.954821
5.25 -9.042461
5.5 -9.08705
5.75 -9.0977
6 -9.07705
6.25 -9.03505
6.5 -8.9743
6.75 -8.8984
7 -8.8103
7.25 -8.7122
7.5 -8.60665
7.75 -8.4949
8 -8.3785
8.25 -8.2585
8.5 -8.1362
8.75 -8.0118
9 -7.88685
9.25 -7.7615
9.5 -7.63665
9.75 -7.51265
10 -7.3898

81 changes: 81 additions & 0 deletions students/one_MichaelTrumbull/fitDiamond.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import numpy as np
import matplotlib.pyplot as plt

"""
This file loads/plots 2-column-data from /data/data.txt
then finds two polyfits x in [3,10]
"""

def mindata(file):
### Load data ###
data = np.loadtxt(file)
print (data)
x = data[:,0]
y = data[:,1]

### Plot data ###
plt.plot(x,y,"o",label="data")

### Fit data ###
x_fit = np.linspace(x[0],x[-1],1000)

### parabolic fit ###
z = np.polyfit(x,y,2)
fit = np.poly1d(z)
y_fit = fit(x_fit)

### Plot the fit ###
print( 'Figure 1 is fit to all the data' )
plt.plot(x_fit,y_fit,"-r",label="Parabolic fit")

### Find minimum ###
minimum = min(y_fit)
index = np.argmin(y_fit)
plt.plot(x_fit[index],minimum,'x',label='minimum')
print('New minimum located at: (', minimum,',',x_fit[index],')')


plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.figure()

### * Use minimum 3 values ###
x_new = [x[np.argmin(y)-1],x[np.argmin(y)],x[np.argmin(y)+1]]
y_new = [y[np.argmin(y)-1],y[np.argmin(y)],y[np.argmin(y)+1]]

### parabolic fit (New) ###
z = np.polyfit(x_new,y_new,2)
fit = np.poly1d(z)
y_fit = fit(x_fit)

### Plot the fit (New)###

plt.plot(x,y,"o",label="data")

print( 'Figure 2 is fit to the 3 lowest data points' )
plt.plot(x_fit,y_fit,"-r",label="Parabolic fit (of lowest 3)")

### Find minimum (New)###
minimum = min(y_fit)
index = np.argmin(y_fit)

plt.plot(x_fit[index],minimum,'x',label='New min')
print('New minimum located at: (', minimum,',',x_fit[index],')')


plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.show()




if __name__=='__main__':
print( '1 - Fit all data' )
print( '2 - Find minimum' )
print( '3 - Fit 3 data points near lowest point' )
print( '4 - Find better minimum' )
print( 'This is a test using diamond data: Data.txt' )
mindata('Data.txt')