Skip to content

Instantly share code, notes, and snippets.

@leomaurodesenv
Created December 13, 2018 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leomaurodesenv/a48820e1e4068dd4f9cef5a964153c70 to your computer and use it in GitHub Desktop.
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)
# 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()
@leomaurodesenv
Copy link
Author

Online python IDE to see the plot repl-it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment