mesh_function.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from pysketcher import *
  2. u = SketchyFunc2('$u(t)$', name_pos='end')
  3. n = 7
  4. t_mesh = [i*2.25/(n-1) for i in range(n)]
  5. u = SketchyFunc2('$u(t)$', name_pos='end')
  6. t_mesh = [0, 2, 4, 6, 8]
  7. u = SketchyFunc3()
  8. t_mesh = linspace(0, 6, 8) # bra
  9. t_mesh = linspace(0, 6, 6)
  10. t_min1 = t_mesh[0] - 0.1*(t_mesh[-1] - t_mesh[0])
  11. t_max1 = t_mesh[-1] + 0.2*(t_mesh[-1] - t_mesh[0])
  12. t_min2 = t_mesh[0] - 0.2*(t_mesh[-1] - t_mesh[0])
  13. t_max2 = t_mesh[-1] + 0.3*(t_mesh[-1] - t_mesh[0])
  14. u_max = 1.3*max([u(t) for t in t_mesh])
  15. u_min = -0.2*u_max
  16. drawing_tool.set_coordinate_system(t_min2, t_max2, u_min, u_max, axis=False)
  17. drawing_tool.set_linecolor('black')
  18. r = 0.005*(t_max2-t_min2) # radius of circles placed at mesh points
  19. discrete_u = Composition({i: Composition(dict(
  20. circle=Circle(point(t, u(t)), r).set_filled_curves('black'),
  21. u_point=Text('$u_%d$' % i,
  22. point(t, u(t)) + (point(0,5*r)
  23. if i > 0 else point(-5*r,0))),
  24. )) for i, t in enumerate(t_mesh)})
  25. interpolant = Composition({
  26. i: Line(point(t_mesh[i-1], u(t_mesh[i-1])),
  27. point(t_mesh[i], u(t_mesh[i]))).set_linewidth(1)
  28. for i in range(1, len(t_mesh))})
  29. axes = Composition(dict(x=Axis(point(0,0), t_max1, '$t$',
  30. label_spacing=(1/45.,-1/30.)),
  31. y=Axis(point(0,0), 0.8*u_max, '$u$',
  32. rotation_angle=90)))
  33. h = 0.03*u_max # tickmarks height
  34. nodes = Composition({i: Composition(dict(
  35. node=Line(point(t,h), point(t,-h)),
  36. name=Text('$t_%d$' % i, point(t,-3.5*h))))
  37. for i, t in enumerate(t_mesh)})
  38. illustration = Composition(dict(u=discrete_u,
  39. mesh=nodes,
  40. axes=axes)).set_name('fdm_u')
  41. drawing_tool.erase()
  42. illustration.draw()
  43. drawing_tool.display()
  44. drawing_tool.savefig(illustration.get_name())
  45. exact = u.set_linestyle('dashed').set_linewidth(1)
  46. exact.draw()
  47. drawing_tool.display()
  48. drawing_tool.savefig('%s_ue' % illustration.get_name())
  49. interpolant.draw()
  50. drawing_tool.display()
  51. drawing_tool.savefig('%s_uei' % illustration.get_name())
  52. raw_input()