diff --git a/__pycache__/app.cpython-37.pyc b/__pycache__/app.cpython-37.pyc index 2dd5627..396a0e9 100644 Binary files a/__pycache__/app.cpython-37.pyc and b/__pycache__/app.cpython-37.pyc differ diff --git a/app.py b/app.py index 9eddf47..bf9fd10 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,8 @@ from flask import Flask, render_template, request, redirect, Response from sympy import * from helpers import apology import numpy as np +import matplotlib +matplotlib.use('Agg') from matplotlib import pyplot as plt app = Flask(__name__) @@ -128,7 +130,9 @@ def maxmin(): # Get Derivative, solve for real solutions, update candidates list fprime = f.diff(x) solutions = list() - solutions.append(round(f.subs(x,0), 3)) + solution = round(f.subs(x,0), 3) + if lb <= solution <= ub: + solutions.append(solution) candidates = list() for solution in solutions: candidates.append(solution) @@ -147,17 +151,17 @@ def maxmin(): # Create Graph lam_f = lambdify(x, f, 'numpy') - X = np.linspace(lb-100, ub+100, (ub-lb)*10) + X = np.linspace(lb, ub, (ub-lb)*10) y = lam_f(X) - # plt.figure(figsize=(3,4)) + plt.style.use('seaborn-whitegrid') plt.scatter(X, y, c='g', marker='.', ) max_idx = np.argmax(values) min_idx = np.argmin(values) plt.plot(candidates[max_idx], values[max_idx], c='r', marker='o', ms=10, label='max') plt.plot(candidates[min_idx], values[min_idx], c='b', marker='o', ms=10, label='min') plt.legend() - plt.savefig('static/img/new_plot.png') + plt.savefig('static/img/maxmin_plot.png') plt.close() # Turn all into latex @@ -167,7 +171,7 @@ def maxmin(): solutions[i] = latex(solution) return render_template("optimized.html", value=value, fprime=fprime, solutions=solutions, lb=lb, ub=ub, candidates=candidates, newvar=newvar, values=values, maximum=maximum, - url='static/img/new_plot.png') + url='static/img/maxmin_plot.png') else: return render_template("maxmin.html") @@ -199,19 +203,42 @@ def aprox(): a = round(a, 3) h = round(h, 3) + # Run through Linearization algorithm fprime = f.diff(x) fa = round(f.subs(x, a), 3) fprimea = round(fprime.subs(x, a), 3) lh = round(fa + fprimea*(float(h)-float(a)), 3) + # Create and Save Plot + dist = abs(a - h) + X = np.linspace(a-dist-100, a + dist+100, (dist)*1000) + + lam_f = lambdify(x, f, 'numpy') + lam_fprime = lambdify(x, fprime, 'numpy') + X = np.linspace(a-dist-15, a + dist+15, (dist)*1000) + y = lam_f(X) + + tan_line = fprimea * (X - a) + fa + lam_tan_line = lambdify(x, tan_line, "numpy") + + plt.style.use('seaborn-whitegrid') + plt.plot(X, lam_f(X), label='Original f(x)') + plt.plot(X, lam_tan_line(X), label='Tangent Line') + plt.plot(a, fa, c='r', marker='o', label='Easy Point a') + plt.plot(h, lh, c='b', marker='o', label='Approximation of f(h)') + plt.legend() + plt.savefig('static/img/aprox_plot.png') + plt.close() + # Convert to latex for MathJax reading value = latex(f) fprime = latex(fprime) fa = latex(fa) lh = latex(lh) - return render_template("aproxd.html", value=value, fprime=fprime, a=a, h=h, fa=fa, fprimea=fprimea, lh=lh) + return render_template("aproxd.html", value=value, fprime=fprime, a=a, h=h, fa=fa, fprimea=fprimea, + lh=lh, url='static/img/aprox_plot.png') else: return render_template("aprox.html") diff --git a/static/img/aprox_plot.png b/static/img/aprox_plot.png new file mode 100644 index 0000000..ea5d5d8 Binary files /dev/null and b/static/img/aprox_plot.png differ diff --git a/static/img/maxmin_plot.png b/static/img/maxmin_plot.png new file mode 100644 index 0000000..21ffadf Binary files /dev/null and b/static/img/maxmin_plot.png differ diff --git a/static/img/new_plot.png b/static/img/new_plot.png deleted file mode 100644 index 6cedd64..0000000 Binary files a/static/img/new_plot.png and /dev/null differ diff --git a/templates/aproxd.html b/templates/aproxd.html index 1f1b92e..429d130 100644 --- a/templates/aproxd.html +++ b/templates/aproxd.html @@ -63,4 +63,6 @@ -{% endblock %} \ No newline at end of file + + Chart +{% endblock %}