mesh_function.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from pysketcher import *
  2. """
  3. u = SketchyFunc2('$u(t)$', name_pos='end')
  4. n = 7
  5. t_mesh = [i*2.25/(n-1) for i in range(n)]
  6. u = SketchyFunc2('$u(t)$', name_pos='end')
  7. t_mesh = [0, 2, 4, 6, 8]
  8. """
  9. u = SketchyFunc3()
  10. Nt = 5
  11. t_mesh = linspace(0, 6, Nt+1)
  12. # Add 20% space to the left and 30% to the right of the coordinate system
  13. t_axis_extent = t_mesh[-1] - t_mesh[0]
  14. t_min = t_mesh[0] - 0.2*t_axis_extent
  15. t_max = t_mesh[-1] + 0.3*t_axis_extent
  16. u_max = 1.3*max([u(t) for t in t_mesh])
  17. u_min = -0.2*u_max
  18. drawing_tool.set_coordinate_system(t_min, t_max, u_min, u_max, axis=False)
  19. drawing_tool.set_linecolor('black')
  20. r = 0.005*(t_max-t_min) # radius of circles placed at mesh points
  21. #import random; random.seed(12)
  22. perturbations = [0, 0.1, 0.1, 0.2, -0.4, -0.1]
  23. u_points = {}
  24. u_values = []
  25. for i, t in enumerate(t_mesh):
  26. u_value = u(t) + perturbations[i]
  27. u_values.append(u_value)
  28. u_points[i] = Composition(dict(
  29. circle=Circle(point(t, u_value), r).set_filled_curves('black'),
  30. u_point=Text('$u^%d$' % i,
  31. point(t, u_value) + (point(0,3*r)
  32. if i > 0 else point(-3*r,0)))))
  33. u_discrete = Composition(u_points)
  34. interpolant = Composition({
  35. i: Line(point(t_mesh[i-1], u_values[i-1]),
  36. point(t_mesh[i], u_values[i])).set_linewidth(1)
  37. for i in range(1, len(t_mesh))})
  38. axes = Composition(dict(
  39. x=Axis(point(0,0), t_mesh[-1] + 0.2*t_axis_extent, '$t$',
  40. label_spacing=(1/45.,-1/30.)),
  41. y=Axis(point(0,0), 0.8*u_max, '$u$',
  42. rotation_angle=90)))
  43. h = 0.03*u_max # tickmarks height
  44. nodes = Composition({i: Composition(dict(
  45. node=Line(point(t,h), point(t,-h)),
  46. name=Text('$t_%d$' % i, point(t,-3.5*h))))
  47. for i, t in enumerate(t_mesh)})
  48. illustration = Composition(dict(u=u_discrete,
  49. mesh=nodes,
  50. axes=axes)).set_name('fdm_u')
  51. drawing_tool.erase()
  52. # Draw t_mesh with discrete u points
  53. illustration.draw()
  54. drawing_tool.display()
  55. drawing_tool.savefig(illustration.get_name())
  56. # Add exact u line (u is a Spline Shape that applies 500 intervals by default
  57. # for drawing the curve)
  58. exact = u.set_linestyle('dashed').set_linewidth(1)
  59. exact.draw()
  60. drawing_tool.display()
  61. drawing_tool.savefig('%s_ue' % illustration.get_name())
  62. # Add linear interpolant
  63. interpolant.draw()
  64. drawing_tool.display()
  65. drawing_tool.savefig('%s_uei' % illustration.get_name())
  66. # Linear interpolant without exact, smooth line
  67. drawing_tool.erase()
  68. illustration.draw()
  69. interpolant.draw()
  70. drawing_tool.display()
  71. drawing_tool.savefig('%s_ui' % illustration.get_name())
  72. input()