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
Star Strider 2017년 3월 20일

0 개 추천

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
Tiarnan O'Kelly 2017년 3월 20일
Thanks for the reply. That is of some help.
What I was trying to do was create a more efficient way where I didn't have to rewrite a code 3 times.
I wanted to put the code into say a for loop where are the start of that loop I could define a value for 'a' and then after the loop was complete one cycle it would put out a unique array for that cycle of the loop containing the values calculated at the value of 'a'. The next loop cycle would have a different value for 'a' and so on.
Thanks again.
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에 대해 자세히 알아보기

질문:

2017년 3월 20일

댓글:

2017년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by