File size: 1,679 Bytes
4f1509e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import sympy as sp 

# Test the corrected numerical examples separately
def test_corrected_examples():
    x, y = sp.symbols('x y')
    
    print("Testing Corrected Numerical Examples:")
    print("=" * 50)
    
    # Case 1 Example: F(x,y) = [2, 3y²]
    print("Case 1: F(x,y) = [2, 3y²]")
    Vx = 2
    Vy = 3 * y**2
    
    # Manual calculation: φ = ∫2 dx + ∫3y² dy = 2x + y³
    phi_manual = 2*x + y**3
    
    # Verify
    print(f"∂φ/∂x = {sp.diff(phi_manual, x)} = Vx? {sp.diff(phi_manual, x) == Vx}")
    print(f"∂φ/∂y = {sp.diff(phi_manual, y)} = Vy? {sp.diff(phi_manual, y) == Vy}")
    print()
    
    # Case 2 Example: F(x,y) = [2x + 3y + 1, 3x + 4y + 2]
    print("Case 2: F(x,y) = [2x + 3y + 1, 3x + 4y + 2]")
    Vx2 = 2*x + 3*y + 1
    Vy2 = 3*x + 4*y + 2
    
    # Check gradient condition: ∂P/∂y = 3, ∂Q/∂x = 3 → equal, so gradient field exists
    print(f"Gradient condition: ∂P/∂y = {sp.diff(Vx2, y)}, ∂Q/∂x = {sp.diff(Vy2, x)}")
    print(f"Field is gradient? {sp.diff(Vx2, y) == sp.diff(Vy2, x)}")
    
    # Find potential using our method
    phi_x = sp.integrate(Vx2, x)  # ∫(2x + 3y + 1)dx = x² + 3xy + x
    remaining = Vy2 - sp.diff(phi_x, y)  # (3x + 4y + 2) - 3x = 4y + 2
    phi_y = sp.integrate(remaining, y)  # ∫(4y + 2)dy = 2y² + 2y
    phi_calculated = phi_x + phi_y  # x² + 3xy + x + 2y² + 2y
    
    print(f"Calculated potential: φ = {phi_calculated}")
    print(f"∂φ/∂x = {sp.diff(phi_calculated, x)} = Vx? {sp.diff(phi_calculated, x) == Vx2}")
    print(f"∂φ/∂y = {sp.diff(phi_calculated, y)} = Vy? {sp.diff(phi_calculated, y) == Vy2}")

# Run the test
test_corrected_examples()