Using an if statement to differentiate between odd and even numbers
226 次查看(过去 30 天)
显示 更早的评论
Given two Qbar matricies and the number of ply layers in a composite, I am trying to calculate the A,B, and D stress matricies.
My first error is calculating h. Given an even number of ply, h is calculated one way, and given an odd number of ply, h is calculated another way. I have tried coding that below but all my attempts have been unsuccessful.
My second problem is figuring out how to tell matlab that the odd numbered layers draw their values from one Qbar matrix and the even numbered layers draw their values from a different Qbar matrix and all the values are summed.
function[A B D] = ABD(Qbar1,Qbar2,n)
x = 0:1:n;
if n == 2*x+1 % odd number of ply
h = -(n/2):1:(n/2);
elseif n == 2*x % even number of ply
h = -n:1:n;
end
disp(h)
for k = 1:n; i = 1:3; j = 1:3;
for k = [1 3 5]
Qbar(i,j,k) = Qbar1;
end
for k = [2 4]
Qbar(i,j,k) = Qbar2;
end
A(i,j) = Qbar(i,j,k)*(h(k+1)-h(k));
B(i,j) = (1/2)*Qbar(i,j,k)*((h(k+1)^2)-(h(k)^2));
D(i,j) = (1/3)*Qbar(i,j,k)*((h(k+1)^3)-(h(k)^3));
end
0 个评论
回答(1 个)
Jon Brenner
2013-11-26
In order to determine if a number is odd or even in MATLAB, use the mod function.
if mod(x, 2) == 0
% x is even
else
% x is odd
end
2 个评论
Jon Brenner
2013-11-26
编辑:Jon Brenner
2013-11-26
If you replace x with n and copy what you were trying to do into my if statement, you get this:
if mod(n, 2) == 0
% n is even
h = -n:1:n;
else
% n is odd
h = -(n/2):1:(n/2);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!