From 114994e7de1f2f063798a590b4f620f596c72835 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Fri, 13 Oct 2017 21:50:10 +0530 Subject: [PATCH 1/4] Add script to calculate area of and plot a given graph --- bin/integrate-graph.py | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 bin/integrate-graph.py diff --git a/bin/integrate-graph.py b/bin/integrate-graph.py new file mode 100644 index 0000000..17bd179 --- /dev/null +++ b/bin/integrate-graph.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- + +import numpy as np +import matplotlib.pyplot as plt +from matplotlib import animation + +def f(order, coefficients, x): + s = sum([coefficients[i]*(x**(order-i)) for i in range(order+1)]) + return s + +class Integrate(object): + def __init__(self): + self.N = None + self.I = None + + def graph(self, order, coefficients, low, high, interval, ans): + fig=plt.figure() + plt.axes(xlim=(low-5, high+5)) + lines = [plt.plot([], [], color='r')[0] for i in range(30)] + plt.title('Trapezoid Method\nRequired area is %0.2f units' %(ans)) + plt.xlabel('X-axis') + plt.ylabel('Y-axis') + plt.axhline(y=0, color='k') + plt.axvline(x=0, color='k') + x=np.linspace(low, high, 500) + y=f(order, coefficients, x) + s='' + for i in range(order+1): + if coefficients[i]>=0: + s=s+(' +'+str(coefficients[i])+'x^'+str(order-i)) + else: + s=s+(' '+str(coefficients[i])+'x^'+str(order-i)) + s=s+' = 0' + plt.plot(x, y, 'darkslateblue', label=s) + + def init(): + for line in lines: + line.set_data([], []) + return lines + + def animate(i): + if i<2: + for j in range(30): + lines[j].set_data([], []) + else: + x = np.linspace(low, high, i) + y = f(order, coefficients, x) + lines[29].set_data(x, y) + for j in range(i): + lines[j].set_data([x[j], x[j]], [y[j], 0]) + for j in range(i, 29): + lines[j].set_data([], []) + return lines + + animation.FuncAnimation(fig, animate, init_func=init, frames=30, interval=500, blit=True) + plt.legend(loc='upper right', numpoints=1) + plt.show() + + def Trapezoid(self, order, coefficients, low, high, interval): + self.N = (high - low)/interval + self.I = sum([2*f(order, coefficients, low + (i*interval)) for i in range(1, int(self.N))]) + self.I += (f(order, coefficients, low) + f(order, coefficients, high)) + ans = (self.I*(high - low))/(2*self.N) + self.graph(order, coefficients, low, high, interval, ans) + return ans + + def Simpson(self, order, coefficients, low, high, interval): + self.N = (high - low)/(2*interval) + self.I = sum([4*f(order, coefficients, low + (i*interval)) if i%2==1 else 2*f(order, coefficients, low + (i*interval)) for i in range(1, int(2*self.N))]) + self.I += (f(order, coefficients, low) + f(order, coefficients, high)) + return (self.I*(high - low))/(6*self.N) + + def solve(self, order, coefficients, low, high, method, interval = 1e-3): + if method == 'trapezoid': + return self.Trapezoid(order, coefficients, low, high, interval) + elif method == 'simpson': + return self.Simpson(order, coefficients, low, high, interval) + +igr = Integrate() +for method in ['trapezoid'] : + solution = igr.solve(3, [7, 3, -6, 10], -50, 65, method=method) + print (solution) From 864648f17afb7040f0ad9d599579e9e290ea15fa Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Fri, 13 Oct 2017 21:57:56 +0530 Subject: [PATCH 2/4] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index a687864..e9fece0 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,13 @@ python client.py python server.py ``` +### Intgrate to find area of a graph +The script takes a given graph along with the range within which the area is to be calculated. +It then calculates the area using two methods, the Simpson method and the Trapezoid method and displays the results on a graph. +```bash +python integrate-graph.py +``` + ## Release History From a3f05674ba234d0d50986b97769ef9e4eefe5fe9 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Fri, 13 Oct 2017 22:04:02 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index e9fece0..2aa62b1 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,13 @@ It then calculates the area using two methods, the Simpson method and the Trapez python integrate-graph.py ``` +### Integrate to find area of a graph +The script takes a given graph along with the range within which the area is to be calculated. +It then calculates the area using two methods, the Simpson method and the Trapezoid method and displays the results on a graph. +```bash +python integrate-graph.py +``` + ## Release History @@ -225,6 +232,7 @@ The following people helped in creating the above content. * Akshit Grover * Sharan Pai * Madhav Bahl +* Ishank Arora ### If you like the project give a star Star button From 927c00f2c148a96ab59116cf5ef00930828188ba Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Fri, 13 Oct 2017 22:05:55 +0530 Subject: [PATCH 4/4] Update README.md --- README.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/README.md b/README.md index 2aa62b1..b076744 100644 --- a/README.md +++ b/README.md @@ -180,13 +180,6 @@ python client.py python server.py ``` -### Intgrate to find area of a graph -The script takes a given graph along with the range within which the area is to be calculated. -It then calculates the area using two methods, the Simpson method and the Trapezoid method and displays the results on a graph. -```bash -python integrate-graph.py -``` - ### Integrate to find area of a graph The script takes a given graph along with the range within which the area is to be calculated. It then calculates the area using two methods, the Simpson method and the Trapezoid method and displays the results on a graph.