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];

回答(1 个)

Spencer Chen
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
  2 个评论
Allen Love
Allen Love 2020-2-14
even for i = 1:num_nd I still get the original matrix. I understand for loops in the basic sense. My question is how to save values to an existing matrix.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structural Analysis 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by