Created
December 13, 2018 12:42
-
-
Save leomaurodesenv/a48820e1e4068dd4f9cef5a964153c70 to your computer and use it in GitHub Desktop.
A simple example showing the average function g(x) of two functions f(x) and f'(x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example in C#: https://stackoverflow.com/questions/5042712/calculate-average-function-of-several-functions | |
# This code is a simple example showing the average function g(x) of two functions f(x) and f'(x). | |
# Note, this average work with multiple functions. | |
import numpy as np | |
import matplotlib.pyplot as plt | |
fig = plt.figure(figsize=(5, 3)) | |
f1 = lambda x: 2*x + 5 # f(x): 2x + 5 | |
f2 = lambda x: 6*x + 7 # f'(x): 6x + 7 | |
f3 = lambda x: 4*x + 6 # average g(x): mean(a, a')*x + mean(b, b') | |
x = np.arange(-50, 100, 1); | |
l1, = plt.plot(x, list(map(f1, x)), color='blue') | |
l2, = plt.plot(x, list(map(f2, x)), color='red') | |
l3, = plt.plot(x, list(map(f3, x)), color='black') | |
plt.legend([l1, l2, l3], [r'$f(x)$', r'$f^\prime(x)$', r'$g(x)$']) | |
# display | |
fig.savefig('graph.png') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Online python IDE to see the plot repl-it.