Saving values from a for loop into an existing matrix

1 回表示 (過去 30 日間)
Allen Love
Allen Love 2020 年 2 月 13 日
コメント済み: Allen Love 2020 年 2 月 17 日
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.
Allen Love
Allen Love 2020 年 2 月 17 日
Any ideas beyond the for loop syntax?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructural Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by