Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- import struct
- import numpy as np
- import math
- from scipy import stats
- # read the data from the program Bisection
- def read_data(filename, datasize):
- with open(filename, "rb") as f:
- lines = f.read()
- print(f"File {filename} Read ", len(lines), "bytes")
- data = struct.unpack('d'*datasize*2 + "i"*datasize, lines);
- # first bite packet are the tolear
- tolerance_values = data[:datasize]
- result_values = data[datasize: datasize*2]
- iteration_values = data[datasize*2:]
- return tolerance_values, result_values, iteration_values
- # calculate the error in log form
- def calc_error(teo_y, y):
- erry = []
- for v in y:
- print("value:", v, end=" ")
- err = (teo_y - v) / teo_y
- if err != 0:
- err = np.log10(abs(err))
- erry.append(err)
- print("log10(error)", err)
- else:
- print("Max precision reached")
- erry.append(min(erry))
- return erry
- #plot and evaluate the data points
- def plot_eval(filename, datasize, teo_y):
- # read the data
- _, y, i = read_data(filename, datasize)
- # calculate the errro
- erry = calc_error(teo_y, y)
- plt.plot(i, erry)
- slope, intercept, r_value, p_value, std_err = stats.linregress(i,erry)
- print("y = " + str(slope) + "x + ", intercept)
- print("angular coeff.:", math.tan(slope))
- # fixed parameters
- datasize = 10
- teo_y = math.sqrt(2)
- # newton
- filename = "./Bisezione/newton_data.dat"
- plot_eval(filename, datasize, teo_y)
- filename = "./Bisezione/bisezione_data.dat"
- plot_eval(filename, datasize, teo_y)
- plt.title('Error in function of iterations')
- plt.xlabel('Number of iterations')
- plt.ylabel('log(error)')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment