Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from numpy import *
  3. import matplotlib.pyplot as plt
  4. import math
  5. import pylab
  6. from matplotlib import mlab
  7.  
  8. print("Строитель графиков")
  9.  
  10. f = input ('f(x)=')
  11. code = """
  12. def func (x):
  13. return %s
  14. """ % f
  15. exec(code)
  16.  
  17. xmin = -20.0
  18. xmax = 20
  19. dx = 0.01
  20. xlist = mlab.frange (xmin, xmax, dx)
  21. ylist = [func (x) for x in xlist]
  22. plt.axis([-20, 20, -20, 20])
  23. plt.xlabel('x')
  24. plt.ylabel('y')
  25. plt.title('График функции(x)')
  26. plt.grid(True)
  27. pylab.plot (xlist, ylist)
  28. pylab.show()
  29.  
  30. code = """
  31. def func (x):
  32. return %s
  33. """ % f
  34.  
  35. # -*- coding: utf-8 -*-
  36. import numexpr as ne
  37. import matplotlib.pyplot as plt
  38. from matplotlib import mlab
  39.  
  40. plt.style.use('ggplot')
  41.  
  42. xmin = -20.0
  43. xmax = 20
  44. dx = 0.01
  45. x = mlab.frange(xmin, xmax, dx)
  46.  
  47. funcs = ["x**2 * sin(x)", "x**2", "15", "12 + 34"]
  48.  
  49. # преобразовуем функции, в которых отсутствует переменная `x` к виду: `x/x * (5)`
  50. new_funcs = [f if 'x' in f else 'x/x * ({})'.format(f) for f in funcs]
  51.  
  52. [plt.plot(x, ne.evaluate(f), linewidth=1.5) for f in new_funcs]
  53. plt.title('Графики функций')
  54. plt.xlabel('x')
  55. plt.ylabel('y')
  56. plt.legend(funcs)
  57. plt.show()
  58.  
  59. In [25]: new_funcs
  60. Out[25]: ['x**2 * sin(x)', 'x**2', 'x/x * (15)', 'x/x * (12 + 34)']
  61.  
  62. # -*- coding: utf-8 -*-
  63. import numexpr as ne
  64. import matplotlib.pyplot as plt
  65. from matplotlib import mlab
  66.  
  67. plt.style.use('ggplot')
  68.  
  69. xmin = -20.0
  70. xmax = 20
  71. dx = 0.01
  72. x = mlab.frange(xmin, xmax, dx)
  73.  
  74. # f = input ('f(x)=')
  75. f = "x**2 * sin(x)"
  76. plt.plot(x, ne.evaluate(f), linewidth=1.5)
  77. plt.xlabel('x')
  78. plt.ylabel('y')
  79. plt.legend(['График функции: f(x) = {}'.format(f)])
  80. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement