Reputation: 115
How can I solve a system of linear equations with some Boundary conditions, using Numpy?
Ax=B
Where x is a column vector with, let's say x1=0.
For different iterations BCs are going to be different, so different variables of vector x going to be zero.
[A] and [B] are known.
Here is an example from my FEM course:
{F} Is the column vector of known values
[k] is the stiffness matrix with the known values
{U} is the displacement column vector where U1 and U3 are known to be zero, but U2 and U4 need to be found.
This would result in these values:
Naturally this would reduce to the 2X2 matrix equation, but I because for different elements the BC would be different, I'm looking for some numpy matrix equation solver where I can let it know that some of the unknowns must be this certain value and nothing else.
Is there something similar to np.linalg.solve() with conditions to it?
Thank you.
Upvotes: 2
Views: 1890
Reputation: 46921
the matrix k
in your example is invertible. that means there is one and only one solution; you can not choose any of the U
s. this is the solution:
import numpy as np
k = np.array(((1000, 0, -1000, 0),
(0, 3000, 0, -3000),
(-100, 0, 3000, -2000),
(0, -3000, -2000, 5000)))
F = np.array((0, 0, 0, 5000))
U = np.linalg.solve(k, F)
print(U)
# # or:
# k_inv = np.linalg.inv(k)
# U = k_inv.dot(F)
# [ 5.55555556 8.05555556 5.55555556 8.05555556]
the same in sage:
k = matrix(((1000, 0, -1000, 0),
(0, 3000, 0, -3000),
(-100, 0, 3000, -2000),
(0, -3000, -2000, 5000)))
F = vector((0, 0, 0, 5000))
U = k.inverse() * F
# (50/9, 145/18, 50/9, 145/18)
Upvotes: 1