Home > Net >  Problem trying to read data from a function imported from an other file
Problem trying to read data from a function imported from an other file

Time:10-19

I am a noob who is trying to progress my python skills. I am working on a task that probably has the intent of teaching how to access variables, procedures, functions, etc from other files as a "soft intro" to object-oriented programing.

The task is to write a function that reads the file and return three arrays consisting of a number corresponding to some elements from the imported file and then plot them in matplotlib.pyplot to see how some round-up measurement develops over the course of some iterations.

The file that is given in the task (that you are supposed to import and read from) looks like this:

# This program is needed in Problem 6.5 Interpret output from a program.

from math import sin, cos, pi

def f(x):
    return sin(x)

def df_approx(f, x, delta_x):
    print(delta_x, f(x delta_x)- f(x))
    return (f(x delta_x)-f(x))/delta_x
    #return (f(x delta_x)-f(x-delta_x))/(2*delta_x)



x = pi/3
for n in range(1, 20):
    delta_x = 10**(-n)
    calculated = df_approx(f, x, delta_x)
    exact = cos(x)
    rel_err = abs(calculated - exact)/abs(exact)
    abs_err = abs(calculated - exact)

    print("delta_x: %e, df_approx: .10e, df_exact: .10e, abs_error: %e, \
           #rel_error: %e, n=%d" % (delta_x, calculated, exact, abs_err, rel_err, n))

And the given file outputs this:

0.1 0.045590188541076104
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
0.01 0.004956615757736871
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-19, df_approx: 0.0000000000e 00, df_exact: 5.0000000000e-01, abs_error: 5.000000e-01,           #rel_error: 1.000000e 00, n=19

The problem is:

I am trying to access the data from the function df_approx in addition to the variables delta_x,abs_error and n from the imported file given the task. Unfortunately, I am only able to access some of the data, not all of it.

For example, if I try to access the values from the variable calculated = df_approx(f, x, delta_x) like this:

import approx_derivative_sine as apx

approx_values = np.array(apx.calculated)
print(approx_values)

It just returns 0.0.

Or if I try to access the variable apx.delta_x like this

delta_x = apx.delta_x
    print(delta_x)

It just returns 1e-19.

If I try to access the data through a for-loop it also crashes or returns nothing like what I want, and so the story continues.

So my questions are

  1. How do I access the variables I need to when I import from other files like this in a proper way? I want all of the data the imported file produces, not just some of it.
  2. How do I access all of the data from the for-loop in the imported file?

This is the code i wrote so far

from approx_derivative_sine import df_approx, f, x, delta_x

def read_from_file():

    approximation = df_approx(f,x,delta_x)
    print(approximation)

read_from_file()

If there is someone kind enough to help me out and provide me with some solutions to this problem I would be extremely grateful.

CodePudding user response:

You can store all the values produced during for loop in list or dict, and import it for your use. A single variable would retain just one value(the most recent one), so you have to use some type of container for storing multiple values.

One approach (An example):

lst = []
for n in range(1, 20):
    delta_x = 10**(-n)
    calculated = df_approx(f, x, delta_x)
    exact = cos(x)
    rel_err = abs(calculated - exact)/abs(exact)
    abs_err = abs(calculated - exact)
    lst.append([delta_x, calculated, rel_arr, abs_err]

And then just use it like:

import approx_derivative_sine as apx

for item in apx.lst:
    print(item[0], item[1], item[2])

Otherwise, you can just import the function definition and call it in your new file with the arguments to generate values. This is the preferred way.

# I am changing function name so that you get it better

from approx_derivative_sine import df_approx as dfa

x = pi/3
for n in range(1, 20):
    delta_x = 10**(-n)
    calculated = dfa(f, x, delta_x)
    exact = cos(x)
    rel_err = abs(calculated - exact)/abs(exact)
    abs_err = abs(calculated - exact)

    print("delta_x: %e, df_approx: .10e, df_exact: .10e, abs_error: %e, \
           #rel_error: %e, n=%d" % (delta_x, calculated, exact, abs_err, rel_err, n))
  • Related