Saving values from a for loop into an existing matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Spencer Chen
2020-2-13
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structural Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!