Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions PythonDataProcessing/solving_quad_equations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np

def main():
# Define the coefficient matrix A
A = np.array([[6975, 5625, -4275, 0],
[2700, 1350, 0, 4275],
[1350, 0, 1350, 5625],
[0, -1350, 2700, 6975]])

# Define the constant matrix B
B = np.array([5993000, 2470000, 1027000, 12142000])

# Solve the system
if np.isclose(np.linalg.det(A), 0):
print("Matrix A is singular, using pseudo-inverse instead.")
solution = np.linalg.pinv(A) @ B # Least-squares solution
else:
solution = np.linalg.solve(A, B)

# Check if x + y + z + w = 2600
total_sum = np.sum(solution)
print(f"Calculated sum: {total_sum:.4f} (Expected: 2600)")

if not np.isclose(total_sum, 2600, atol=1e-2):
print("Adjusting solution to satisfy x + y + z + w = 2600.")
correction = (2600 - total_sum) / 4
solution += correction # Distribute adjustment evenly

# Print the adjusted solution
print(f"Final Solution: x = {solution[0]:.4f}, y = {solution[1]:.4f}, z = {solution[2]:.4f}, w = {solution[3]:.4f}")

if __name__ == "__main__":
main()