How to write this better!
이전 댓글 표시
I know enough to know this is written terribly!
Please let me know if a different way to write it. I will only include the part of the code I think is necessary:
if true
%declare array Q
Q = [Q11, Q12, 0; Q12, Q22, 0; 0, 0, Q66];
%New Matrix Qbar - 'a' is the angle set in for loop
a = 0;
q11 = Q11*cosd(a).^4 + Q22*sind(a).^4 + (2*Q12 + 4*Q66)*(sind(a).^2)*(cosd(a).^2);
q22 = Q22*cosd(a).^4 + Q11*sind(a).^4 + (2*Q12 + 4*Q66)*(sind(a).^2)*(cosd(a).^2);
q12 = Q12*(cosd(a).^4 + sind(a).^4) + (Q11 + Q22 - 4*Q66)*(sind(a).^2)*(cosd(a).^2);
q66 = Q66*(cosd(a).^4 + sind(a).^4) + (Q11 + Q22 - 2*Q12 - 2*Q66)*(sind(a).^2)*(cosd(a).^2);
q16 = (Q11 - Q12 - 2*Q66)*(cosd(a).^3)*sind(a) - (Q22 - Q12 - 2*Q66)*(sind(a).^3)*(cosd(a));
q26 = (Q11 - Q12 - 2*Q66)*(sind(a).^3)*cosd(a) - (Q22 - Q12 - 2*Q66)*(cosd(a).^3)*(sind(a));
Q0 = [q11, q12, q16; q12, q22, q26; q16, q26, q66];
%for 30 degrees
b = 30;
q11 = Q11*cosd(b).^4 + Q22*sind(b).^4 + (2*Q12 + 4*Q66)*(sind(b).^2)*(cosd(b).^2);
q22 = Q22*cosd(b).^4 + Q11*sind(b).^4 + (2*Q12 + 4*Q66)*(sind(b).^2)*(cosd(b).^2);
q12 = Q12*(cosd(b).^4 + sind(b).^4) + (Q11 + Q22 - 4*Q66)*(sind(b).^2)*(cosd(b).^2);
q66 = Q66*(cosd(b).^4 + sind(b).^4) + (Q11 + Q22 - 2*Q12 - 2*Q66)*(sind(b).^2)*(cosd(b).^2);
q16 = (Q11 - Q12 - 2*Q66)*(cosd(b).^3)*sind(b) - (Q22 - Q12 - 2*Q66)*(sind(b).^3)*(cosd(b));
q26 = (Q11 - Q12 - 2*Q66)*(sind(b).^3)*cosd(b) - (Q22 - Q12 - 2*Q66)*(cosd(b).^3)*(sind(b));
Q30 = [q11, q12, q16; q12, q22, q26; q16, q26, q66];
%For 45 degrees
c = 45;
q11 = Q11*cosd(c).^4 + Q22*sind(c).^4 + (2*Q12 + 4*Q66)*(sind(c).^2)*(cosd(c).^2);
q22 = Q22*cosd(c).^4 + Q11*sind(c).^4 + (2*Q12 + 4*Q66)*(sind(c).^2)*(cosd(c).^2);
q12 = Q12*(cosd(c).^4 + sind(c).^4) + (Q11 + Q22 - 4*Q66)*(sind(c).^2)*(cosd(c).^2);
q66 = Q66*(cosd(c).^4 + sind(c).^4) + (Q11 + Q22 - 2*Q12 - 2*Q66)*(sind(c).^2)*(cosd(c).^2);
q16 = (Q11 - Q12 - 2*Q66)*(cosd(c).^3)*sind(c) - (Q22 - Q12 - 2*Q66)*(sind(c).^3)*(cosd(c));
q26 = (Q11 - Q12 - 2*Q66)*(sind(c).^3)*cosd(c) - (Q22 - Q12 - 2*Q66)*(cosd(c).^3)*(sind(c));
Q45 = [q11, q12, q16; q12, q22, q26; q16, q26, q66];
end
Any help is appreciated.
Thanks
답변 (1개)
Star Strider
2017년 3월 20일
It is difficult to follow what you are doing.
The one change I would do is to use anonymous functions, since your assignments seem to repeat.
Example:
q1_fcn = @(Q1,Q2,Q3,Q4,a) Q1*cosd(a).^4 + Q2*sind(a).^4 + (2*Q3 + 4*Q4)*(sind(a).^2)*(cosd(a).^2);
q11 = q1_fcn(Q11,Q22,Q12,Q66,a);
and similarly for the others. Create the functions, then call them as necessary.
댓글 수: 2
Tiarnan O'Kelly
2017년 3월 20일
Star Strider
2017년 3월 20일
My pleasure.
If you just want to run the loop, you could use anonymous functions or your original code. The loop using your original code would be:
angl = [0 30 45];
for k1 = 1:length(angl)
a = angl(k1);
q11 = Q11*cosd(a).^4 + Q22*sind(a).^4 + (2*Q12 + 4*Q66)*(sind(a).^2)*(cosd(a).^2);
q22 = Q22*cosd(a).^4 + Q11*sind(a).^4 + (2*Q12 + 4*Q66)*(sind(a).^2)*(cosd(a).^2);
q12 = Q12*(cosd(a).^4 + sind(a).^4) + (Q11 + Q22 - 4*Q66)*(sind(a).^2)*(cosd(a).^2);
q66 = Q66*(cosd(a).^4 + sind(a).^4) + (Q11 + Q22 - 2*Q12 - 2*Q66)*(sind(a).^2)*(cosd(a).^2);
q16 = (Q11 - Q12 - 2*Q66)*(cosd(a).^3)*sind(a) - (Q22 - Q12 - 2*Q66)*(sind(a).^3)*(cosd(a));
q26 = (Q11 - Q12 - 2*Q66)*(sind(a).^3)*cosd(a) - (Q22 - Q12 - 2*Q66)*(cosd(a).^3)*(sind(a));
Q(k1,:) = [q11, q12, q16; q12, q22, q26; q16, q26, q66];
end
This just takes one section of your code, substitutes the angles on each iteration, and saves the results in the ‘Q’ matrix. You can calculate as many angles as you like. All you need to do is to change the ‘a’ vector to include them. The code automatically adapts.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!