Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help creating a Python 3.6 program that will produce a data table for an

ID: 3818464 • Letter: I

Question

I need help creating a Python 3.6 program that will produce a data table for an arbitrary function of two variables (x,y), using the argparse module to parse user command line input allowing the user to specify the following optional arguments (in addition to the string representing the function to tabulate):

• -x is the name of independent variable 1

• -y is the name of independent variable 2

• -xmin is the minimum value of x

• -xmax is the maximum value of x

• -ymin is the minimum value of y

• -ymax is the maximum value of y

• -n is the number of points between xmin and xmax.

So basically, I would like to have the first module/ defined function: accept and parse the user command line input example: (-x "x" -y "y" -n 5 "exp(-(x**2+y**2))”), and a main function to produce the output into a data table with 'z_value' -for the arbitrary equation result with 2 variables used.

Explanation / Answer

Following is a python program for given requirement. It has two methods:

Comments are added in the program which explain the various steps performed.

Sample execution output from program is also given below for your reference.

File: data_table.py

import argparse

def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-x", help="Name of independent variable 1")
parser.add_argument("-y", help="Name of independent variable 2")
parser.add_argument("-xmin", help="Minimum value of x", default=0)
parser.add_argument("-xmax", help="Maximum value of x", default=1)
parser.add_argument("-ymin", help="Minimum value of y", default=0)
parser.add_argument("-ymax", help="Maximum value of y", default=1)
parser.add_argument("-n", help="Number of points between xmin and xmax", type=int, default=10)
parser.add_argument("function", help="Function to evaluate", type=str)

args = parser.parse_args()

return args

if __name__ == "__main__":

# Parse command line arguments
args = parse_arguments()

# Read optional arguments
x = args.x
y = args.y
x_min = args.xmin
x_max = args.xmax
y_min = args.ymin
y_max = args.ymax
n = args.n

# Read required expression argument
function = args.function

# Remove 'exp' from function string so that it can be evaluated using python
function = function.replace("exp", "")

# Print data table header
print(x, " ", y, " ", "z")

# Evaluate 'z_value' for each 'x_value' and 'y_value'
x_value = x_min # Initial value of variable 1
y_value = y_min # Initial value of variable 2
x_incr = (x_max - x_min) / n # Increment value for variable 1
y_incr = (y_max - y_min) / n # Increment value for variable 2

for step in range(0, n + 1):
exec("{0} = {1}".format(args.x, x_value)) # Set variable 1 value to variable 1 name given as argument
exec("{0} = {1}".format(args.y, y_value)) # Set variable 2 value to variable 2 name given as argument

z_value = eval(function) # Evaluate 'z_value'

x_value = x_value + x_incr
y_value = y_value + y_incr

print("%.2f" % x, " ", "%.2f" % y, " ", "%.2f" % z_value) # Print a data table row

Sample Execution Output 1:

$ python3 data_table.py -h
usage: data_table.py [-h] [-x X] [-y Y] [-xmin XMIN] [-xmax XMAX] [-ymin YMIN]
[-ymax YMAX] [-n N]
function

positional arguments:
function Function to evaluate

optional arguments:
-h, --help show this help message and exit
-x X Name of independent variable 1
-y Y Name of independent variable 2
-xmin XMIN Minimum value of x
-xmax XMAX Maximum value of x
-ymin YMIN Minimum value of y
-ymax YMAX Maximum value of y
-n N Number of points between xmin and xmax

Sample Execution Output 2:

$ python3 data_table.py -x "x" -y "y" -n 5 "exp(-(x**2+y**2))"
x y z
0.00 0.00 0.00
0.20 0.20 -0.08
0.40 0.40 -0.32
0.60 0.60 -0.72
0.80 0.80 -1.28
1.00 1.00 -2.00

Sample Execution Output 3:

$ python3 data_table.py -x "x" -y "y" -n 10 "exp(x+y)"
x y z
0.00 0.00 0.00
0.10 0.10 0.20
0.20 0.20 0.40
0.30 0.30 0.60
0.40 0.40 0.80
0.50 0.50 1.00
0.60 0.60 1.20
0.70 0.70 1.40
0.80 0.80 1.60
0.90 0.90 1.80
1.00 1.00 2.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote