Add j-cook.py

This commit is contained in:
gribse 2023-12-12 16:43:46 +00:00
commit 8f41daaa4f

54
j-cook.py Normal file
View file

@ -0,0 +1,54 @@
import csv
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import linregress
r2max = 0.2
# Specify the path to your CSV file
csv_file_path = 'test-data/500-0.1.csv'
custom_delimiter = "\t"
# Read the CSV file and convert numbers from strings with commas to floats
with open(csv_file_path, 'r') as file:
# Create a CSV reader object with the specified delimiter
csv_reader = csv.reader(file, delimiter=custom_delimiter)
# Convert numbers from strings with commas to floats
data = np.array([[float(cell.replace(',', '.')) for cell in row] for row in csv_reader])
# Treat stress and strain values to be positive
data[:, 0] = np.abs(data[:, 0])
data[:, 1] = np.abs(data[:, 1])
# Use linregress to perform linear regression
elasticMax = 10
r_value = 0
while r_value < r2max and len(data[:, 0]):
slope, intercept, r_value, p_value, std_err = linregress(data[:elasticMax, 0], data[:elasticMax, 1])
print(r_value)
elasticMax += 10
# Create the fitted line using the slope and intercept
fitted_line = slope * data[:, 0] + intercept
# Plot data
x_values = data[:, 0]
y_values = data[:, 1]
plt.plot(data[:, 0], data[:, 1], label='Orignial data', color='blue')
plt.plot(data[:, 0], fitted_line, label='Fitted Line', color='red')
# Add labels and legend
plt.xlabel('Stress')
plt.ylabel('Strain')
plt.legend()
# Show the plot
plt.show()