From 8f41daaa4f22c8db4e830a1c8a3e284ec79b68c1 Mon Sep 17 00:00:00 2001 From: gribse Date: Tue, 12 Dec 2023 16:43:46 +0000 Subject: [PATCH] Add j-cook.py --- j-cook.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 j-cook.py diff --git a/j-cook.py b/j-cook.py new file mode 100644 index 0000000..395b7f2 --- /dev/null +++ b/j-cook.py @@ -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()