Code is working:: However, Matrix obtained not correct (Code is working but the zeros elements place are not correct
Show older comments
Hi all,
I need help in this code,
The Matrix obtained is correct however, the zeros elements are not in the correct place
Should be the following:
[164 54 0;
54 109 0;
0 0 109] ;
I think something is wrong in the indexing, but can not find ??
Below are the code:
clearvars;
clc;
close all;
E11 = 54;
E22 = 54;
v12 = .25;
v23 = .25;
S = zeros(9,9);
S(1,1) = 1/E11;
S(1,2) = -v12/E11;
S(1,3) = S(1,2);
S(2,1) = S(1,2);
S(2,2) = 1/E22;
S(2,3) = -v23/E22;
S(3,1) = S(1,2);
S(3,2) = S(2,3);
S(3,3) = 1/E22;
S(4,4) = (1+v12)/(E11);
S(5,5) = S(4,4);
S(6,6) = S(5,5);
S(7,7) = S(4,4);
S(8,8) = S(5,5);
S(9,9) = S(6,6);
C_n = inv(S);
C(1:3,1:3,1:3,1:3) = zeros(3,3,3,3);
IND =[1 1; 2 2; 3 3; 1 2; 2 3; 3 1; 2 1; 3 2; 1 3]
for m=1:9
for n=1:9
C(IND(m,1),IND(m,2),IND(n,1),IND(n,2)) = C_n(m,n);
end
end
t = 2.54;
delta = [1 1 1]*t;
A = zeros(3,3); % INITALIZATION OF A MATRIX
delAJ = [1 0 0]*(t*t);%permut(j,m,n)*delta(m)*delta(n);
for i = 1:3
for k = 1:3
for j = 1:3
for l = 1:3
A(i,k) = A(i,k) + delAJ(j)/delta(l)*C(i,j,k,l);
end
end
end
end
A
10 Comments
ANKUR KUMAR
on 1 Mar 2021
I am getting the following output, not close to the output which you provided (neglecting the index of elements). If you can provide the desired matrix, someone might help you.
A =
164.5920 54.8640 54.8640
0 54.8640 0
0 0 22.8600
Mark Sc
on 1 Mar 2021
Bob Thompson
on 1 Mar 2021
Are delAJ and delta always going to be the same thing they are in what you included? If so, what is the purpose of making them have three elements?
If delAJ and delta are constant then the first value of j is the only one that matters, so why bother adding the other two? I would recommend removing the loop for j and saving yourself the time to run it. Just index the second dimension to 1.
I could be wrong, because I don't entirely understand why anything is happening in this code, but I think the indexing issue is in your assigment of C_n into C. As best I can trace out, The placement of zeroes in the array of C(:,1,:,:) is what's causing the assignments to appear messed up. So maybe adjust the IND matrix to reorder them?
Mark Sc
on 1 Mar 2021
Bob Thompson
on 1 Mar 2021
Why can't you adjust IND? It is a hard coded matrix, so changing it should be as simple as changing the numbers that make it.
Where does IND come from? Where does C_n come from? How do you know that both are correct, and that your desired output is correct?
Mark Sc
on 1 Mar 2021
Bob Thompson
on 2 Mar 2021
Unfortunately, I personally don't really know how to help much more. It sounds like the math you're doing is not something I really understand, so I don't know how to better direct you. The best I can offer is just to reiterate what I already mentioned, that I believe the issue is coming from your assignment into C (therefore looking at IND and C_n), or the generated values of C_n.
Cris LaPierre
on 3 Mar 2021
Edited: Cris LaPierre
on 3 Mar 2021
I also played around with changing IND, as well as how you index C in your 4 nested for loops. The issue appears to be more than just a simple indexing issue, as a single change affects the entire output, not just a single value.
Without knowing what it is you are trying to implement, it's hard to determine where your code may be incorrect, especially as you noted, it's working. Could you share details on how S should be created, and what algorithm you are following to create C and to compute A?
Please assume that we are unfamiliar with the actual system you are solving. If you make it easy for us to understand what your code is supposed to be doing, we can probably be of more help.
Mark Sc
on 3 Mar 2021
Answers (0)
Categories
Find more on Logical 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!