Skip to content

Commit

Permalink
added stuff to make it easy
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Jan 1, 2023
1 parent 7f1ba67 commit e1888f2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions fourier_series.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from sympy import *
import numpy as np
import matplotlib.pyplot as plt
import io


x, n = symbols("x n")


def fourier_series_evaluator(func, start, end):
T = end - start
cos_term = cos((2 * n * pi * x) / T)
Expand All @@ -18,6 +20,22 @@ def fourier_series_evaluator(func, start, end):
return (a0, an, bn)


def fourier_series_latex(func, start, end):
a0, an, bn = fourier_series_evaluator(func, start, end)
a0, an, bn = (
nsimplify(a0, tolerance=0.01, full=True),
nsimplify(an, tolerance=0.01, full=True),
nsimplify(bn, tolerance=0.01, full=True),
)

latex_string = latex(a0) + " + "
latex_string += "\\sum_{n=1} ^{\\infty} "
latex_string += latex(an)
latex_string += latex(bn)

return latex_string


def fourier_graph_compute(func, start, end, terms=6, repeat=1, dir=1):
a0, an, bn = fourier_series_evaluator(func, start, end)
T = end - start
Expand Down Expand Up @@ -58,6 +76,21 @@ def fourier_visualizer(func, start, end, terms=6, repeat=1, dir=1):
plt.show()


def fourier_visualizer_svg(func, start, end, terms=6, repeat=1, dir=1):
x_axis, y_axis = fourier_graph_compute(func, start, end, terms, repeat, dir)

for i in y_axis:
plt.plot(x_axis, i)

buf = io.BytesIO()
plt.savefig(buf, format="svg")
buf.seek(0)

plt.close()

return buf


if __name__ == "__main__":
func = eval(input("Enter Function to be Plot\n\tf(x) = "))
start = float(input("Enter start of period: "))
Expand Down
14 changes: 14 additions & 0 deletions graph_2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import matplotlib.pyplot as plt
import numpy as np
from sympy import *
import io


x = symbols("x")
Expand All @@ -20,6 +21,19 @@ def graph(func, start=-3.14, end=3.14):
plt.show()


def graph_svg(func, start=-3.14, end=3.14):
x_axis, y_axis = graph_compute_points(func, start, end)
plt.plot(x_axis, y_axis)

buf = io.BytesIO()
plt.savefig(buf, format="svg")
buf.seek(0)

plt.close()

return buf


if __name__ == "__main__":
func = input("f(x): ")
graph(eval(func))

0 comments on commit e1888f2

Please sign in to comment.