Question & Answer: Using python:…..

Using python:

Use import numpy as np

Print results to a file

Write a computer code to produce a table with 2 columns. The first column contains x, numbers from 0, .1, .2, … , 1.2. The second column contains the cos(x) for those values. Use a “for loop” to generate the table. The cosine should show exactly 4 decimal digits(use %f format for each column). The first line(Header) should say what is in each column like ” x cos(x)”.

Expert Answer

 

#!/usr/bin/python
import math
f = open(‘myfile’, ‘w’)
f.write(“x     cos(x)n”)
for i in range(1,13):
i = i*0.1
f.write(“%.1f %.4fn” % (i,math.cos(i)))
f.close()
=============================================
See Output : If you run the above code you will see myfile. will be created in same directy as your python code

File Contents

Still stressed from student homework?
Get quality assistance from academic writers!