Good day! I'm trying to write a python script that can model the temperature of a space. I've chosen an 11x11 matrix to represent this space, the edges are 20 degrees, and the centre is 3000 degrees. I then have a matrix with all the other values being variables x1,x2,x3...,x81
Now to calculate the temperatures at each x1, I take the mean of the neighbouring points, e.g x1 = (20 + x10 + 20 + x2) / 4 That is x1 is the sum of the above point, below point, left, right / 4 Now my goal is to form a matrix with these equations. I've made the equations in the form I said above
def generate_equations():
equations = []
for i in range(1, rows - 1): # Iterates over the indices i, j but excludes the edges
for j in range(1, cols - 1):
if isinstance(matrix[i,j], str) and matrix[i,j].startswith('x'): # Firsts checks if the element i,j is a string (identifies variables)
neighbours = get_neighbours(i,j) # Secondly checks if the string starts with an x, ensuring it is one of the unknowns
# Otherwise we get equation: 3000 = ( x32 + x50 + x40 + x42 ) / 4
# Include unknown variables in the equation
equation = f"{matrix[i,j]} = ({' + '.join(map(str, neighbours))}) / 4 " # ' + '.join(map(str, neighbours))': Joins the values of the neighbours, converting them to strings
equations.append(equation) # And the entire expression is formatted to an equation.
return equations
but I now either need to amend this or add more code to create/simplify the equations in a desired form, this form being using x1 again, 4x1 -x2 -x10 = 40. so I can then construct an augmented matrix to solve. I feel like a main problem I have is that all my equations are stored in a list as strings.
I've tried to separate the variables from the constants and the sum them up on either side of the equation, but it usually just leads to something like this: 4x1 - x2 - x10 + 20 + 20 = 40 + x10 + x2, instead of 4x1 -x2 -x10 = 40.