0

I have this program that calculates an approximation to the derivative of sin(pi/3). The print gives this output into a txt file:

delta_x: 1.000000e-01, df_approx: 4.5590188541e-01, df_exact: 5.0000000000e-01, abs_error: 4.409811e-02,           rel_error: 8.819623e-02, n=1
delta_x: 1.000000e-02, df_approx: 4.9566157577e-01, df_exact: 5.0000000000e-01, abs_error: 4.338424e-03,           rel_error: 8.676848e-03, n=2
delta_x: 1.000000e-03, df_approx: 4.9956690400e-01, df_exact: 5.0000000000e-01, abs_error: 4.330960e-04,           rel_error: 8.661920e-04, n=3
delta_x: 1.000000e-04, df_approx: 4.9995669790e-01, df_exact: 5.0000000000e-01, abs_error: 4.330210e-05,           rel_error: 8.660421e-05, n=4
delta_x: 1.000000e-05, df_approx: 4.9999566987e-01, df_exact: 5.0000000000e-01, abs_error: 4.330133e-06,           rel_error: 8.660266e-06, n=5
delta_x: 1.000000e-06, df_approx: 4.9999956697e-01, df_exact: 5.0000000000e-01, abs_error: 4.330281e-07,           rel_error: 8.660562e-07, n=6
delta_x: 1.000000e-07, df_approx: 4.9999995699e-01, df_exact: 5.0000000000e-01, abs_error: 4.300676e-08,           rel_error: 8.601353e-08, n=7
delta_x: 1.000000e-08, df_approx: 4.9999999696e-01, df_exact: 5.0000000000e-01, abs_error: 3.038736e-09,           rel_error: 6.077471e-09, n=8
delta_x: 1.000000e-09, df_approx: 5.0000004137e-01, df_exact: 5.0000000000e-01, abs_error: 4.137019e-08,           rel_error: 8.274037e-08, n=9
delta_x: 1.000000e-10, df_approx: 5.0000004137e-01, df_exact: 5.0000000000e-01, abs_error: 4.137019e-08,           rel_error: 8.274037e-08, n=10
delta_x: 1.000000e-11, df_approx: 5.0000004137e-01, df_exact: 5.0000000000e-01, abs_error: 4.137019e-08,           rel_error: 8.274037e-08, n=11
delta_x: 1.000000e-12, df_approx: 5.0004445029e-01, df_exact: 5.0000000000e-01, abs_error: 4.445029e-05,           rel_error: 8.890058e-05, n=12
delta_x: 1.000000e-13, df_approx: 4.9960036108e-01, df_exact: 5.0000000000e-01, abs_error: 3.996389e-04,           rel_error: 7.992778e-04, n=13
delta_x: 1.000000e-14, df_approx: 4.9960036108e-01, df_exact: 5.0000000000e-01, abs_error: 3.996389e-04,           rel_error: 7.992778e-04, n=14
delta_x: 1.000000e-15, df_approx: 5.5511151231e-01, df_exact: 5.0000000000e-01, abs_error: 5.511151e-02,           rel_error: 1.102230e-01, n=15
delta_x: 1.000000e-16, df_approx: 0.0000000000e+00, df_exact: 5.0000000000e-01, abs_error: 5.000000e-01,           rel_error: 1.000000e+00, n=16
delta_x: 1.000000e-17, df_approx: 0.0000000000e+00, df_exact: 5.0000000000e-01, abs_error: 5.000000e-01,           rel_error: 1.000000e+00, n=17
delta_x: 1.000000e-18, df_approx: 0.0000000000e+00, df_exact: 5.0000000000e-01, abs_error: 5.000000e-01,           rel_error: 1.000000e+00, n=18
delta_x: 1.000000e-19, df_approx: 0.0000000000e+00, df_exact: 5.0000000000e-01, abs_error: 5.000000e-01,           rel_error: 1.000000e+00, n=19

Now I want to write a function that reads the file and returns three arrays consisting of numbers corresponding to delta_x, abs_error and n. How would I do that? Because I have to plot these arrays into a graph as well later, but I can't wrap my head around on how to actually extract the wanted values with a function that returns arrays for each of the values that I calculated? I have tried with this code, but I get ridiculous print:

import numpy as np
import matplotlib.pyplot as plt

k=19 #number of calculations
abs_error=np.zeros(k)
n=np.zeros(k)
delta_x=np.zeros(k)

def extract_data(filename):
    infile = open('approx_derivative_sine.txt', 'r')

    for line in infile:
        words=line.split()
        #words[0]: delta_x, words[1]: value for delta_x
        np.append(delta_x, words[1])
        np.append(abs_error, words[7])
        np.append(n, words[10])
        
    return delta_x,abs_error,n

print(extract_data(0))

the print is:

(array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0.]), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0.]), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0.]))

Which is completely wrong, because each array is supposed to have float values from delta_x, abs_error and n that is in the txt file. When I do it as a list it works, but I have to use arrays. Anyone got an idea how to fix it? Can anyone write a code that works???

4

0 回答 0