Using an if statement to differentiate between odd and even numbers

137 visualizaciones (últimos 30 días)
Christopher Marlar
Christopher Marlar el 26 de Nov. de 2013
Editada: Jon Brenner el 26 de Nov. de 2013
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

Respuestas (1)

Jon Brenner
Jon Brenner el 26 de Nov. de 2013
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 comentarios
Christopher Marlar
Christopher Marlar el 26 de Nov. de 2013
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 el 26 de Nov. de 2013
Editada: Jon Brenner el 26 de Nov. de 2013
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

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by