• September 29th 2008, 11:42 AM
jnwebb
Hello everyone, I am currently making the switch over from MathCad to Mathematica...wow the learning curve is rather steep! (Doh)

Question:

I am trying to set up a calc to determine different dof's in a simple beam.

If I have in mathematica:

is=range[0,nsupports-1] - this is my iterator

I want to set up a loop:

for x in is:
if x=0, return 0
if x=nsupports-1, return ndof-2
otherwise, return x*nels*2

I want the output to go to a column vector which will give me where each different degree of freedom is located.

Thanks
Jared
• September 29th 2008, 01:12 PM
shawsend
Here's two ways. First way is what you'd expect. I used just 0,1,2 for the results. You can change them to the variables you want. The second example is more sophisticated using procedural programming (with the &,#,@) bit). The switch code says, "switch according to the value found in the array created by Range (the # placeholder), if it's 0, return 0. If it's nsupport-1, return 1, if it's anything else (the _), return 2". The & /@ means this is a pure function (with variable #) operating on each element of the array created by Range.

Code:

```In[14]:= nsupport = 10; is = Range[0, nsupport - 1] Table[If[is[[n]] == 0, 0,   If[is[[n]] == nsupport - 1, 1, 2]], {n, 1, 10}] Switch[#, 0, 0, nsupport - 1, 1, _, 2] & /@ Range[0, 9] Out[15]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Out[16]= {0, 2, 2, 2, 2, 2, 2, 2, 2, 1} Out[17]= {0, 2, 2, 2, 2, 2, 2, 2, 2, 1}```
• September 30th 2008, 06:27 AM
jnwebb
Another ?
nels=2
tels=4
ke=Range[0,tels-1]
ndof=10
ratios={10,10}
f(i,j)=0

k(L_)=4x4 matrix previously defined

I would like to set up a loop to transform the local matrices k(L_) for each element (tels=total number of elements) to global matrix.

for all ke do:

Define local variables:
span=IntegerPart(ke/nels)
klocal=k(ratios[[span]]*(t/nels))
kglobal=matrix(ndof,ndof,f)
startdof=ke*2

for i from 0..3
for j from 0..3
kglobal[[startdof+i, startdof+j]]=klocal[[i,j]] (not sure if this needs to be local or not)

This will take each element in the four by four matrix and transform it into a 10x10 matrix
• September 30th 2008, 08:41 AM
shawsend
I think you need to change that to work with tables/matrices that begin with index 1 as I'm pretty sure Mathematica requires that. Also, you have a $t$ in there that is not defined.
• September 30th 2008, 11:46 AM
jnwebb

OK, I am trying...growing pains!!!
I start w/ the following:

k[L_] = {{12, 6 L, -12, 6 L},
{6 L, 4 L^2, -6 L, 2 L^2},
{-12, -6 L, 12, -6 L},
{6 L, 2 L^2, -6 L, 4 L^2}}

t=12
nspans=2
nels=2
ratios={10,10}
ndof=10
tels=4
ke=Range[0,tels-1]

I want to do the following:

Ksubs[ke]: for all in range ke

span=IntegerPart[(ke)/nels
klocal=k[[ratios[[span[[ke]]+1]]*t/nels]]
kglobal=ndof x ndof matrix
startdof=ke*2

for i=1,i=4,i++,
for j=1,j=4,j++
kglobal[[startdof+i,startdof+j]]=klocal[[i,j]]
(This part will place the local matrix for each
element into the global matrix)

That should then give me 4 different Ksubs[1,2,3,4] matrices.

I would then like to combine them:

Ks=summation from i=1,i=4 of Ksubs[i]

Should end up with the following matrix:

Ks={{ 12 360 -12 360 0 0 0 0 0 0 }
{360 14400 -360 7200 0 0 0 0 0 0 }
{-12 -360 24 0 -12 360 0 0 0 0 }
{360 7200 0 28000 -360 7200 0 0 0 0 }
{ 0 0 -12 -360 24 0 -12 360 0 0 }
{ 0 0 360 7200 0 28800 -360 7200 0 0 }
{ 0 0 0 0 -12 -360 24 0 -12 360}
{ 0 0 0 0 360 7200 0 28800 -360 7200}
{ 0 0 0 0 0 0 -12 -360 12 -360}
{ 0 0 0 0 0 0 360 7200 -360 14400}

Any help would be appreciated
• September 30th 2008, 12:30 PM
jnwebb
So, I pretty much have it!!!

kglobal = Table[f[i, j], {i, ndof}, {j, ndof}]

Ks = For[n = 0, n < tels, n++, span = IntegerPart[n/nels] + 1; klocal =\[NoBreak]k[ratios[[span]]*t/nels]; startdof = n*2;
For[i = 1, i <= 4, i++,
For[j = 1, j <= 4, j++,
kglobal[[startdof + i, startdof + j]] = klocal[[i, j]]
]
] Clear[span, klocal, startdof]
] MatrixForm[kglobal]

The problem is the output has a Null(the matrix I want)...I am sure that this is something little.

• September 30th 2008, 01:47 PM
shawsend
Here's what I came up with and as I've mentioned in here before, if you want to get really good with Mathematica, hang out with the Drexel gang:

Math Forum Discussions - comp.soft-sys.math.mathematica

They're much sharper than me (just takes 3 days usually)! Also, the code below is just a quick fix. I'm sure it can be improved and encapsulated further.

Code:

```k[L_] = {{12, 6*L, -12, 6*L}, {6*L, 4*L^2, -6*L, 2*L^2}, {-12, -6*L, 12, -6*L},     {6*L, 2*L^2, -6*L, 4*L^2}}; t = 12; nspans = 2; nels = 2; ratios = {10, 10}; ndof = 10; tels = 4; ke = Range[0, tels - 1]; span = IntegerPart[ke/nels]; kglobal = Table[0, {i, ndof}, {j, ndof}]; For[n = 0, n < tels, n++, span = IntegerPart[n/nels] + 1;     klocal = k[ratios[[span]]*(t/nels)]; startdof = n*2;     For[i = 1, i <= 4, i++, For[j = 1, j <= 4, j++,       kglobal[[startdof + i,startdof + j]] = klocal[[i,j]]]]]; MatrixForm[kglobal]```
• October 1st 2008, 11:11 AM
jnwebb
I have an object with 10 points. I have created a vector:

DOF={0,1,2,3,4,5,6,7,8,9}

Further in my notebook I have solved to find:
DOFS={0,4,8} - these numbers will be different

I would now like to reorder these so that DOFS comes first and then the rest follow in numerical order.

realigned={0,4,8,1,2,3,5,6,7,9}

right now I have:

Code:

```realigned = Table[0, {i, 10}, {j, 1}]; count = 1 For[i = 1, i <= 3, i++,           realigned[[count]] = DOFS[[i]];           count = count + 1]; For[i = 1, i <= 10, i++,         If[realigned[i] == DOFS[[i]],         Continue[]]; realigned[[count]] = i; count = count + 1```