I'm trying to make 3 columns in my .txt file to make it easier to read. This is the code:
file_out = open("Assignment1_Part1.txt", "w")
file_out.write(str(x) + str(fx(x, a)), + str(fx1(x, a)))
file_out.close()
Where x,fx, and fx1 each contain a ton of different values. I want the first value of x to line up with the first value of fx and fx1, however in the .txt file whats happening is all of the x values are typed out first, followed by all the fx, followed by all fx1, like so:
(x)(x)1(x)2 (fx)(fx)1(fx)2 (fx1)(fx1)1(fx1)2
whereas I want it to look like this:
(x) (fx) (fx1)
(x)1 (fx)1 (fx1)1
(x)2 (fx)2 (fx1)2
And so on. Thanks in advance!
edit: whole code
import numpy as np
import matplotlib.pyplot as plt
# Defining Functions ----
def fx(x, a):
return np.sin(x**a - x**(1 / a) + a * x)
def gx(x, a):
return a*(x)**(a-1) - (1/a)*x**((1/a) - 1) + a
def fx1(x, a):
return gx(x, a) * (np.cos(x**a - x**(1 / a) + a * x))
# NR Method ----
def NR(x, function, deriv):
return x - function/deriv
# Creating x values and defining a ----
x = np.arange(0.00, 5, 0.01)
a = 0.4 + (0223.0 / 25000.0)
# np.column_stack joins two arrays to make a table
ar1 = np.column_stack((x, fx(x, a)))
print(ar1)
plt.plot(x, fx(x, a))
plt.xlabel('x')
plt.ylabel('f(x)')
#plt.savefig("part1")
plt.savefig('part1')
# ar2 = np.column_stack((ar1,fx1(x,a)))
# print(ar2)
file_out = open("Assignment1_Part1.txt", "w")
file_out.write(str(x)+str(fx(x, a))+str(fx1(x, a)))
file_out.close()
Edit 2: Sorry for not making this clear, but the txt file is huge, like 240 lines, the assignment is the plot of a graph so it contains every x value and y value on the graph. This would make adding a newline after each value impossible