Saving values from a for loop into an existing matrix
2 views (last 30 days)
Show older comments
Hello,
I am trying to define the boundary conditions in a FEM truss problem. I am having trouble saving what I did to the matrix. For example if BC(1) = 0 I want to make the first row equal to 0 then make the value in that row along the diagnol equal to 1 and save it to the original matrix.
here is my code:
%number of nodes / number of degrees of freedom
num_nd = 4;
num_dof = 2;
% Boundary Conditions
n_u = ones(num_nd, num_dof);
n_u(1,1) = 0; % Boundary conditions for node 1 x
n_u(1,2) = 0; % Boundary conditions for node 1 y
n_u(3,1) = 0; % Boundary condition for node 3 y
% Global matrix
kg = [1.3536 .3536 -1 0 0 0 -.3536 -.3536;
.3536 1.3536 0 0 0 -1 -.3536 -.3536;
-1 0 1 0 0 0 0 0; 0 0 0 1 0 0 0 -1;
0 0 0 0 1 0 -1 0; 0 -1 0 0 0 1 0 0;
-.3536 -.3536 0 0 -1 0 1.3536 .3536;
-.3536 -.3536 0 -1 0 0 .3536 1.3536];
%Apply boundary conditions
for i = 1:num_nd
if n_u(i) == 0
kg(i,:) = 0;
if kg(i,:) == 0
kg(i,i) = 1;
end
end
end
% What I want kg to look like
% kg = [1 0 0 0 0 0 0 0;
% 0 1 0 0 0 0 0 0;
% -1 0 1 0 0 0 0 0;
% 0 0 0 1 0 0 0 -1;
% 0 0 0 0 1 0 0 0;
% 0 -1 0 0 0 1 0 0;
% -.3536 -.3536 0 0 -1 0 1.3536 .3536;
% -.3536 -.3536 0 -1 0 0 .3536 1.3536];
0 Comments
Answers (1)
Spencer Chen
on 13 Feb 2020
This might be your problem:
for i = num_nd ...
This code does not give you a proper for-loop.
I'll leave you pondering on the problem yourself.
Blessings,
Spencer
See Also
Categories
Find more on Structural Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!