Using an if statement to differentiate between odd and even numbers

조회 수: 242 (최근 30일)
Christopher Marlar
Christopher Marlar 2013년 11월 26일
편집: Jon Brenner 2013년 11월 26일
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

답변 (1개)

Jon Brenner
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
Christopher Marlar
Christopher Marlar 2013년 11월 26일
I'm not trying to determine if x is odd of even. I'm trying to tell matlab that if n is odd, h = something and if n is even, h is something else.
Jon Brenner
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 CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by