fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # --------------------------------------
  5. # System of equations:
  6. # 2x + 3y = 5
  7. # 4x - y = 1
  8. # --------------------------------------
  9.  
  10. A = np.array([[2, 3],
  11. [4, -1]])
  12.  
  13. b = np.array([5, 1])
  14.  
  15. # Solve A x = b
  16. x = np.linalg.solve(A, b)
  17. x1, x2 = x
  18.  
  19. print("Solution:")
  20. print(f"x = {x1:.4f}, y = {x2:.4f}")
  21.  
  22. # Extract column vectors
  23. col1 = A[:, 0] # First column
  24. col2 = A[:, 1] # Second column
  25.  
  26. # Scale columns by solution components
  27. v1 = x1 * col1
  28. v2 = x2 * col2
  29.  
  30. # Plotting
  31. plt.figure(figsize=(7,7))
  32.  
  33. origin = np.array([[0, 0], [0, 0]])
  34.  
  35. # Plot x1 * col1 and x2 * col2
  36. plt.quiver(*origin,
  37. [v1[0], v2[0]],
  38. [v1[1], v2[1]],
  39. angles='xy', scale_units='xy', scale=1,
  40. color=['black','black'])
  41.  
  42. # Plot resulting vector b
  43. plt.quiver(0, 0, b[0], b[1],
  44. angles='xy', scale_units='xy', scale=1,
  45. width=0.006, color='blue')
  46.  
  47. # Annotate vectors
  48. plt.text(v1[0], v1[1], " x·col1", fontsize=12)
  49. plt.text(v2[0], v2[1], " y·col2", fontsize=12)
  50. plt.text(b[0], b[1], " b", fontsize=12)
  51.  
  52. # Cosmetic settings
  53. plt.xlim(-2, 8)
  54. plt.ylim(-2, 8)
  55. plt.grid(True)
  56. plt.axhline(0, color='black', linewidth=0.5)
  57. plt.axvline(0, color='black', linewidth=0.5)
  58. plt.title("Column Picture of a Linear System:\n x·col₁ + y·col₂ = b")
  59.  
  60. plt.show()
  61.  
Success #stdin #stdout #stderr 3.45s 69688KB
stdin
Standard input is empty
stdout
Solution:
x = 0.5714,    y = 1.2857
stderr
Fontconfig error: No writable cache directories