Question: The following python code is meant to take any truss, inputting information about its nodes, members, and external loads and the code is meant to

The following python code is meant to take any truss, inputting information about its nodes, members, and external loads and the code is meant to output all internal member forces and reactions of the truss. There are a couple errors in the code that are preventing it from running properly. All the current code can output values are zero, which is wrong.
Task: Fix any errors in this code. Once you finish, submit the finished python code and ensure it works. I will check the code to see if it is right. If everything is good, I will give you a like immediately.
If the submitted code cant run, I will give you a thumbs down, until it is fixed.
Python Code >
import numpy as np
class Truss:
def __init__(self, nodes, members, loads):
self.nodes = nodes
self.members = members
self.loads = loads
self.num_nodes = len(nodes)
self.num_members = len(members)
self.num_unknowns = self.num_members *2 # Each member has two unknown forces
def solve_method_of_joints(self):
# Initialize the equations matrix and the forces vector
num_equations = self.num_nodes -1 # Number of equations
A = np.zeros((num_equations, self.num_unknowns))
forces = np.zeros((num_equations, 1))
# Fill the equations matrix and forces vector
eq_index =0 # Equation index
for node in self.nodes:
if node != 'fixed': # Skip the fixed node
# Gather member indices connected to the current node
member_indices =[i for i, member in enumerate(self.members) if node in member['nodes']]
# Sum forces in x and y directions for each connected member
for member_index in member_indices:
dx = self.nodes[self.members[member_index]['nodes'][1]]['x']- self.nodes[self.members[member_index]['nodes'][0]]['x']
dy = self.nodes[self.members[member_index]['nodes'][1]]['y']- self.nodes[self.members[member_index]['nodes'][0]]['y']
length = np.sqrt(dx**2+ dy**2)
cos_theta = dx / length
sin_theta = dy / length
# A[eq_index, 2*member_index]= cos_theta
# A[eq_index, 2*member_index +1]= sin_theta
# Add external forces to the forces vector
for load in self.loads:
if load['joint']== node:
forces[eq_index]= load['force']
eq_index +=1
try:
# Solve for the unknown forces
unknown_forces = np.linalg.lstsq(A, forces, rcond=None)[0]
except np.linalg.LinAlgError:
print("Error: Incompatible dimensions in the linear equations.")
return None
# Store the results in a dictionary
results ={}
for i, member in enumerate(self.members):
results[member['name']]={
'force_x': unknown_forces[2*i,0],
'force_y': unknown_forces[2*i +1,0]
}
return results
# Example usage:
nodes1={
'A': {'x': 0,'y': 0},
'B': {'x': 0,'y': 3},
'C': {'x': 4,'y': 0}
}
members1=[
{'name': 'AB', 'nodes': ['A','B']},
{'name': 'BC', 'nodes': ['B','C']}
]
loads1=[
{'force': 50, 'delta_x': 0, 'delta_y': 3, 'joint': 'B'}
]
truss = Truss(nodes1, members1, loads1)
# Solve for unknown forces using Method of Joints
results = truss.solve_method_of_joints()
if results is not None:
print("Unknown forces:", results)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!