3
$\begingroup$

I'm trying to speed up my Code, but I'm facing problems with changing the AppendTo section in my code with something "Faster". I've read through some other examples that explain the use of Reapand Sow. Understanding Sow and Reap documentation
Writing Faster Mathematica Code - Sow and Reap?
https://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/

I can't figure out how to apply the Solutions given in the links for my problem.

n = 20;
dz = 1000/n;
RollLM[l_, EM_, Ixx_, p_] := {{1, l, Power[l, 2] / (2 EM Ixx), 
Power[l, 3] / (6 EM Ixx), p Power[l, 4] / (24 EM Ixx)}, {0, 1, l / (EM 
Ixx), Power[l, 2] / (2 EM Ixx), p Power[l, 3] / (6 EM Ixx)}, {0, 0, 1, 
l , p Power[l, 2] / 2}, {0, 0, 0, 1, p l}, {0, 0, 0, 0, 1}};
BearLM[d_, c_] := {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, d, 1, 0, 0}, 
{-c, 0, 0, 1, 0}, {0, 0, 0, 0, 1}};
pBF = Function[z, Piecewise[{{989000/500, 250 <= z <= 750}}]];

Vec1A = {vA, PhiA, 0, 0, 1};
Vec2A = BearLM[810000, 850000].Vec1A;
VecAList = {Vec2A};
Do[ AppendTo[VecAList, RollLM[dz, 210000, 262440000 Pi, pBF[(i- 
0.5)dz]] .VecAList[[i]]], {i, n}];
Vec3A = BearLM[810000, 850000].Last[VecAList];
SolA = Solve[{Vec3A[[3]] == 0, Vec3A[[4]] == 0},{vA,PhiA}] 
[[1]];
$\endgroup$
  • $\begingroup$ Are a, b, vA, RF, B,PhiA meant to be numerical values? Then please define them first. That will speed up already quite a lot. $\endgroup$ – Henrik Schumacher Jan 10 at 12:12
  • $\begingroup$ I changed a, b, RF and B to the numerical values. I justed overlocked to change them in the first place. But PhiA and vA will be later one solved in the Code. $\endgroup$ – CR36 Jan 10 at 12:18
  • $\begingroup$ ListLinePlot[LeafCount /@ VecAList] tells me that this is not a good idea. Better numericise PhiA and vA and use a numerical linear solver instead. $\endgroup$ – Henrik Schumacher Jan 10 at 12:21
  • $\begingroup$ I've added the example so it is better understandable. $\endgroup$ – CR36 Jan 10 at 12:33
6
$\begingroup$

It doesn't look like AppendTo has much overhead in this case.

n = 20;
dz = 1000/n;
RollLM[l_, EM_, Ixx_, p_] := {{1, l, Power[l, 2] / (2 EM Ixx), Power[l, 3] / (6 EM Ixx), p Power[l, 4] / (24 EM Ixx)}, {0, 1, l / (EM Ixx), Power[l, 2] / (2 EM Ixx), p Power[l, 3] / (6 EM Ixx)}, {0, 0, 1, l , p Power[l, 2] / 2}, {0, 0, 0, 1, p l}, {0, 0, 0, 0, 1}};
BearLM[d_, c_] := {{1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, d, 1, 0, 0}, {-c, 0, 0, 1, 0}, {0, 0, 0, 0, 1}};
pBF = Function[z, Piecewise[{{989000/500, 250 <= z <= 750}}]];

Vec1A = {vA, PhiA, 0, 0, 1};
Vec2A = BearLM[810000, 850000].Vec1A;


VecAList = {Vec2A};
Timing[Do[AppendTo[VecAList, RollLM[dz, 210000, 262440000 Pi, 
     pBF[(i - 0.5) dz]].VecAList[[i]]], {i, n}]]

{0.03125, Null}

original = VecAList;

VecAList = {Vec2A};
Timing[Do[VecAList = {VecAList, RollLM[dz, 210000, 262440000 Pi,
     pBF[(i - 0.5) dz]].Last[VecAList]}, {i, n}]]

{0.03125, Null}

VecAList = Partition[Flatten[VecAList], 5];
original == VecAList

True

| improve this answer | |
$\endgroup$
  • $\begingroup$ So you are suggesting that the actuall solving is the factor that costs a lot of time? $\endgroup$ – CR36 Jan 10 at 12:46
  • $\begingroup$ I guess so. The second version does not use AppendTo and the timing is the same. $\endgroup$ – Chris Degnen Jan 10 at 12:48
  • $\begingroup$ So is there a better way to solve this system with Symbolic variables and also the n variable witch in this case is 20 can be 250 and then it's getting pretty slow. $\endgroup$ – CR36 Jan 10 at 12:57
  • 2
    $\begingroup$ That's kind of a different question. $\endgroup$ – Chris Degnen Jan 10 at 13:03
  • $\begingroup$ @CR36 The solving takes a significant amount of time, but the dominant factor seems to be RollLM[..].VecAList[[i]], as Henrik hinted at. $\endgroup$ – Michael E2 Jan 10 at 14:22
5
$\begingroup$

You can replace the AppendTo loop with

u = Vec2A;
VecAList = Table[
   u = RollLM[dz, 210000, 262440000 Pi, pBF[(i - 0.5) dz]].u,
   {i, 1, n}];

However, this does not speed up anything as the computations are performed symbolically and this LeafCount[VecAList[[i]] grows exponentially with i (and thus LeafCount[VecAList] grows exponentially with n).

| improve this answer | |
$\endgroup$
  • $\begingroup$ So is there something you can recomend? $\endgroup$ – CR36 Jan 10 at 16:07
  • 1
    $\begingroup$ That's kind of a different question. $\endgroup$ – Henrik Schumacher Jan 10 at 16:32
  • $\begingroup$ "[K]ind of" in the sense of "very much". Would be best to scrap this question and start afresh. $\endgroup$ – Daniel Lichtblau Jan 10 at 17:07
  • $\begingroup$ I just meant to cite @ChrisDegnen... ;) $\endgroup$ – Henrik Schumacher Jan 10 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.