-5
$\begingroup$

I am trying to programming the following in Mathematica; however I am having trouble getting this to work inside the For loop.

$$\text{For}\left[i=1;i^2<10,i\text{++},\sum _{k=0}^{18} t^k (k+\text{x1})^2,\text{Collect}[\%,\text{x1}],\text{SeriesCoefficient}[\%,\{\text{x1},0,i\}],\text{CoefficientList}[\%,t],\text{L2}=\text{FindSequenceFunction}[\%,k],\sum _{k=0}^{\infty } \text{L2} t^k;\text{Print}[\%]\right]$$

Translating the LaTeX into Mathematica:

For[i=1,i^2<10,i++,
    theSum = Sum[t^k (k+x1)^2, {k, 0, 18}];
    collected = Collect[theSum, x1];
    mySeriesCoefficients = SeriesCoefficient[collected, {x1, 0, i}];
    myCoefficientList = CoefficientList[mySeriesCoefficients, t];
    L2 = FindSequenceFunction[myCoefficientList,k];
    myNewSum = Sum[L2 t^k, {k, 0, Infinity}];
    Print[myNewSum];
]

Someone below used Table, would that be a better approach?

$\endgroup$
6
  • $\begingroup$ You must post code (and format with the {} button in the editor) not LaTex. Please edit your question and insert the code instead. Also, describe the issue you are having and the expected outcome. $\endgroup$
    – Edmund
    Commented Feb 3, 2017 at 21:04
  • $\begingroup$ Hi Eddmun sorry but i do not know how to paste code i do undertand how to paste code i try but paste as text with format do not work ( please can you show me how) i try to calculate the generating function of a series using findsequence but i need the result in file thanks $\endgroup$ Commented Feb 3, 2017 at 21:12
  • $\begingroup$ Paste the text and then select the code and push the {} button. $\endgroup$ Commented Feb 3, 2017 at 21:29
  • $\begingroup$ I have started the code for you. Please finish it and make sure there aren't any syntax errors. $\endgroup$
    – QuantumDot
    Commented Feb 3, 2017 at 21:37
  • $\begingroup$ There are a variety of errors in there. Let's start with what is notable. The first three arguments for the For loop need to be comma separated , (see i=1;i...) and for multiple statements in the For loop you need to use ;. Second, you're better off using Table for this... $\endgroup$
    – SumNeuron
    Commented Feb 3, 2017 at 21:40

1 Answer 1

2
$\begingroup$

New Answer

If you are insistent on using the For loop, then you can see - from translating you LaTeX into Mathematica, that the errors go away, and the code appears to work:

Module[
  (*Local Variabels*)
  {theSum, collected, mySeriesCoefficients, myCoefficientList, L2, myNewSum},
  For[i = 1, i^2 < 10, i++,
    theSum = Sum[t^k (k + x1)^2, {k, 0, 18}];
    collected = Collect[theSum, x1];
    mySeriesCoefficients = SeriesCoefficient[collected, {x1, 0, i}];
    myCoefficientList = CoefficientList[mySeriesCoefficients, t];
    L2 = FindSequenceFunction[myCoefficientList, k];
    myNewSum = Sum[L2 t^k, {k, 0, Infinity}];
    Print[myNewSum]
  ]
]
(*OUTPUT:
  (2 (-1+2 t))/(-1+t)^2

  1/(1-t)

  \!\(
  \*UnderoverscriptBox[\(\[Sum]\), \(k = 0\), \(\[Infinity]\)]\(
  \*SuperscriptBox[\(t\), \(k\)]\ FindSequenceFunction[{}, k]\)\)
*)

This was achieved following the comments below your original post i.e. multiple commands within a For loop need to be separated by ; rather than ,. It also helps to replace % with a local variable, as % is meant for retrieving the most recent output. I am unsure as to whether or not that will work inside a For loop.

Either way, (as see by the question's rating), just posting LaTeX and broken code is not likely to get you very far. Especially as it was pointed out in the comments that it would be easier for others to help you if you provided code (which I typed out in about a minute after code was not provided). A question that has no effort put into it, is not likely to get anyone willing to put effort in to resolving it. This commentary is not meant to lambaste you, rather help you make more constructive questions in the future. Do not be discouraged as I am sure many of the community will be happy to assist with future, well-prosed questions.

Welcome to the Mathematica S.E. community :)

Original Answer

So this is not the full "answer" (if you were expecting to receive the full code), but should give you the framework you need to do what you are trying to do.

Just as an aside, I wouldn't call this a ''simple'' instruction. As good practice, if your code doesn't work, trying building up to it from smaller chunks.

Anyway, you can build what you are trying to do via the Table function.

squaresOfILessThan10 = Table[If[i^2 < 10, i, Nothing], {i, 1, 10}];    

Table[(*For i = 1, i^2<10, i++*)
 Table[
  Collect[Sum[t^k (k + x1)^2, {k, 0, 18}], x1]
  , {x1, 0, i}
  ]
 , {i, squaresOfILessThan10}]
(*{{t + 4 t^2 + 9 t^3 + 16 t^4 + 25 t^5 + 36 t^6 + 49 t^7 + 64 t^8 + 
   81 t^9 + 100 t^10 + 121 t^11 + 144 t^12 + 169 t^13 + 196 t^14 + 
   225 t^15 + 256 t^16 + 289 t^17 + 324 t^18, 
  1 + 4 t + 9 t^2 + 16 t^3 + 25 t^4 + 36 t^5 + 49 t^6 + 64 t^7 + 
   81 t^8 + 100 t^9 + 121 t^10 + 144 t^11 + 169 t^12 + 196 t^13 + 
   225 t^14 + 256 t^15 + 289 t^16 + 324 t^17 + 361 t^18}, {t + 
   4 t^2 + 9 t^3 + 16 t^4 + 25 t^5 + 36 t^6 + 49 t^7 + 64 t^8 + 
   81 t^9 + 100 t^10 + 121 t^11 + 144 t^12 + 169 t^13 + 196 t^14 + 
   225 t^15 + 256 t^16 + 289 t^17 + 324 t^18, 
  1 + 4 t + 9 t^2 + 16 t^3 + 25 t^4 + 36 t^5 + 49 t^6 + 64 t^7 + 
   81 t^8 + 100 t^9 + 121 t^10 + 144 t^11 + 169 t^12 + 196 t^13 + 
   225 t^14 + 256 t^15 + 289 t^16 + 324 t^17 + 361 t^18, 
  4 + 9 t + 16 t^2 + 25 t^3 + 36 t^4 + 49 t^5 + 64 t^6 + 81 t^7 + 
   100 t^8 + 121 t^9 + 144 t^10 + 169 t^11 + 196 t^12 + 225 t^13 + 
   256 t^14 + 289 t^15 + 324 t^16 + 361 t^17 + 400 t^18}, {t + 
   4 t^2 + 9 t^3 + 16 t^4 + 25 t^5 + 36 t^6 + 49 t^7 + 64 t^8 + 
   81 t^9 + 100 t^10 + 121 t^11 + 144 t^12 + 169 t^13 + 196 t^14 + 
   225 t^15 + 256 t^16 + 289 t^17 + 324 t^18, 
  1 + 4 t + 9 t^2 + 16 t^3 + 25 t^4 + 36 t^5 + 49 t^6 + 64 t^7 + 
   81 t^8 + 100 t^9 + 121 t^10 + 144 t^11 + 169 t^12 + 196 t^13 + 
   225 t^14 + 256 t^15 + 289 t^16 + 324 t^17 + 361 t^18, 
  4 + 9 t + 16 t^2 + 25 t^3 + 36 t^4 + 49 t^5 + 64 t^6 + 81 t^7 + 
   100 t^8 + 121 t^9 + 144 t^10 + 169 t^11 + 196 t^12 + 225 t^13 + 
   256 t^14 + 289 t^15 + 324 t^16 + 361 t^17 + 400 t^18, 
  9 + 16 t + 25 t^2 + 36 t^3 + 49 t^4 + 64 t^5 + 81 t^6 + 100 t^7 + 
   121 t^8 + 144 t^9 + 169 t^10 + 196 t^11 + 225 t^12 + 256 t^13 + 
   289 t^14 + 324 t^15 + 361 t^16 + 400 t^17 + 441 t^18}}*)

Wolfram's book Elementary Introduction focuses heavily on Table, so it wouldn't be bad to get familiar with it :)

$\endgroup$
8
  • $\begingroup$ Thanks Sumneuron this is the idea how could it possible to avoid the {} of the vector cause findsequence could interprete thanks anyway $\endgroup$ Commented Feb 3, 2017 at 22:10
  • $\begingroup$ You can always unwrap it.... Could you perhaps explain what exactly you are trying to do with your function? $\endgroup$
    – SumNeuron
    Commented Feb 3, 2017 at 22:12
  • $\begingroup$ i try to calculate the generating function of a series using findsequence but i need the result in file $\endgroup$ Commented Feb 3, 2017 at 22:23
  • $\begingroup$ @antonioasis the Mathematica community is very helpful and supportive. However, for the community to be able to assist you they need a clear idea of what you want to do and some context as to why. Please consider editing your post to give us context, and maybe even an example of some expected output. Just posting a LaTeX rending of your function and expecting someone to provide the corrected code is uncommon. In addition, it may benefit you to change your question's title to something more specific such as "How to get the coefficients of a generator function?" $\endgroup$
    – SumNeuron
    Commented Feb 4, 2017 at 8:39
  • $\begingroup$ thanks SumNeuron for your help i normaly use mathematic to get numerically result i work from mathematical concept but Mathematica is easy to use to use some library as function but to programiing is to hard cause to do someting so simple to calculate and order some coefficient you need time to get some efficient way to write for it i realizae to get code. HOW I CAN POST CODE FROM NOTE BOOK ? PLEASE COULD YOU SHOW ME $\endgroup$ Commented Feb 4, 2017 at 11:06

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