Group Abstract Group Abstract

Message Boards Message Boards

0
|
145 Views
|
13 Replies
|
9 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to present the detailed calculation result of sequence summation

Posted 5 days ago

Given a general term formula of a sequence segmented by parity.

a[n_] = Piecewise[{{5 n + 1, Mod[n, 2] == 0}, {2^(n/2), 
    Mod[n, 2] == 1}}]

Calculate the sum of its first n terms

s[n_] = Sum[a[j], {j, 1, n}]

The result obtained is not what I want.

enter image description here

I want to obtain the following result.

enter image description here

POSTED BY: Bill Blair
13 Replies

I suspect that disentangling complex piecewise formula algorithmically is not as robust as humans might think. There are probably cases where pulling apart a piecewise defined formula into a sum of simple piecewise expressions does not help, but it does here. Note that in the OP Sum[] handles the Piecewise[] well enough to produce a DifferenceRoot[]. The complexity I speak of is propagated to the DifferenceRoot[]. FunctionExpand[] cannot expand the one produced in the OP, but it can expand the two produced separately from the two formulas in the OP's piecewise a[n].

a[n_] = Piecewise[{{5 n + 1, Mod[n, 2] == 0}, {2^(n/2), Mod[n, 2] == 1}}];
pwApart = HoldPattern@Piecewise[lis_, 0] :> (Piecewise[{#}, 0] & /@ lis);
s[n_] = Sum[
     a[j] /. pwApart
     , {j, 1, n}] // FunctionExpand // Total // Simplify
(*
1/8 (-7 + 7 (-1)^n - 8 Sqrt[2] + 2^(3 + n/2) - (-1)^n 2^(3 + n/2) + 
   2^((5 + n)/2) + (-1)^n 2^((5 + n)/2) + 2 (7 + 5 (-1)^n) n + 10 n^2)
*)

If you don't like (-1)^n, convert it to Piecewise[]:

Assuming[n \[Element] Integers && n > 0,
 s[n] /. (-1)^n :> Piecewise[{{-1, Mod[n, 2] == 1}}, 1] // 
   PiecewiseExpand // Simplify
 ]
(*
Piecewise[
 {{(1/4)*(-7 - 4*Sqrt[2] + 2^(3 + n/2) + 2*n + 5*n^2), Mod[n, 2] == 1}},
 Sqrt[2]*(-1 + 2^(n/2)) + 3*n + (5*n^2)/4]
*)

Piecewise[] has its own evaluation/display rules that seem to make the exact desired form in OP impossible to achieve. (In particular, there is always a default value, which is displayed with the condition True.) If you don't like the above, see if the following suits you:

s[n] /. (-1)^n :> 
  Piecewise[{{-1, Mod[n, 2] == 1}, {1, Mod[n, 2] == 0}}, 
   Indeterminate]
POSTED BY: Michael Rogers
Posted 2 days ago

I’ve learned several key and practical points that have perfectly resolved my confusion. First, I’ve grasped the core trick of disentangling a complex piecewise function into individual piecewise components using the pattern-matching method

pwApart = HoldPattern@Piecewise[lis_, 0] :> (Piecewise[{#}, 0] & /@ lis)

This approach breaks down the original complicated piecewise general term into simpler, separate parts, which makes the subsequent summation operation much more manageable and avoids the messy results caused by directly summing the combined piecewise function.
Second, I’ve understood the importance of the combination of FunctionExpand, Total and Simplify functions in post-processing the summation result. These functions work together to transform the initial DifferenceRoot form into a concise algebraic expression, which is exactly the explicit form I was eager to obtain.
Third, your guidance on converting the (-1)^n term to a piecewise function is incredibly useful. The method of using Assuming to set the domain constraint of positive integers, then replacing (-1)^n with a piecewise expression and applying PiecewiseExpand helps to present the summation result in a more intuitive piecewise form that aligns with the parity segmentation feature of the original sequence. I also noticed your reminder about the default value mechanism of the Piecewise function, which helped me understand why the result format has a slight difference from my initial expectation. Your solution is not only a fix for this specific problem but also provides me with systematic thinking for handling piecewise sequence summation in the future. Thank you again for taking the time to share your expertise and clear up my confusion!

POSTED BY: Bill Blair

Your summation formula looks wrong. Try it for some values of n.

POSTED BY: Gianluca Gorni
Posted 3 days ago

What matters is the correctness of the method applied to this problem. In contrast to the result presented in the figure, we only mandate such a display form.

POSTED BY: Bill Blair

Your mandated form is wrong. Try this:

a[n_] = Piecewise[{{5 n + 1, Mod[n, 2] == 0},
    {2^(n/2), Mod[n, 2] == 1}}];
s[n_Integer] := Sum[a[j], {j, 1, n}];
desiredS[n_] := 
  Piecewise[{{5 ((n + 1)/2)^2 + (n + 1)/2 + 2^((n + 1)/2) - 2,
     Mod[n, 2] == 1},
    {5/4 n^2 + n/2 + 2^(n/2 + 1) - 2,
     Mod[n, 2] == 0}}];
Table[s[n] == desiredS[n] // Simplify, {n, 10}]

{False, False, False, False, False, False, False, False, False, False}
POSTED BY: Gianluca Gorni
Posted 2 days ago

What is the proper approach when a piecewise form is employed? Is it feasible to apply the Piecewise function to formulate the general term formula of the sequence, thereby guaranteeing the validity of the subsequent summation operation?

POSTED BY: Bill Blair

There are many different, equivalent ways of writing an expression, all of them correct. It can be tricky to convert an output of Mathematica into the exact form you have in mind.

POSTED BY: Gianluca Gorni

Another way:

a[n_] = (5 n + 1) (1 - Mod[n, 2]) + 2^(n/2) Mod[n, 2]
s[n_] = Sum[a[j], {j, 1, n}]
Piecewise[{{Simplify[s[n], Mod[n, 2] == 0], Mod[n, 2] == 0},
  {Simplify[s[n], Mod[n, 2] == 1], Mod[n, 2] == 1}}]
POSTED BY: Gianluca Gorni
Posted 4 days ago
a[n_] = (5 n + 1) (1 - Mod[n, 2]) + 2^(n/2) Mod[n, 2]

This general term formula is ingeniously constructed and perfectly solves the problem. Why is it absolutely necessary to adopt this specific form of the general term formula? Why is it impossible to derive the desired summation formula when we use a piecewise-defined general term formula instead? What is the underlying reason for this?

POSTED BY: Bill Blair

Simpler solution without Piecewise :

a1[n_] = 
  Piecewise[{{5 n + 1, Mod[n, 2] == 0}, {2^(n/2), Mod[n, 2] == 1}}];
c1[n_] = 
 Sum[(a1[n][[1, 1, 1]]) /. n -> 2 k, {k, 1, Floor[n/2]}] + 
   Sum[(a1[n][[1, 2, 1]]) /. n -> 2 k - 1, {k, 1, Ceiling[n/2]}] // 
  Expand

(*-Sqrt[2] + 2^(1/2 + Ceiling[n/2]) + 6 Floor[n/2] + 5 Floor[n/2]^2*)
POSTED BY: Mariusz Iwaniuk
Posted 4 days ago

Can your method be manipulated to obtain the summation formula shown in the image at the bottom of my question?

POSTED BY: Bill Blair
a1[n_] = 
  Piecewise[{{5 n + 1, Mod[n, 2] == 0}, {2^(n/2), Mod[n, 2] == 1}}];
c1[n_] = 
 Sum[(a1[n][[1, 1, 1]]) /. n -> 2 k, {k, 1, Floor[n/2]}] + 
  Sum[(a1[n][[1, 2, 1]]) /. n -> 2 k - 1, {k, 1, Ceiling[n/2]}]; 
Piecewise[{{Simplify[c1[n], Mod[n, 2] == 0], 
    Mod[n, 2] == 0}, {Simplify[c1[n], Mod[n, 2] == 1], 
    Mod[n, 2] == 1}}] // ExpandAll
POSTED BY: Mariusz Iwaniuk
Posted 3 days ago

The conversion implemented by this method is incomplete. As I have attempted previously, the output results still retain the Floor and Ceiling functions.

POSTED BY: Bill Blair
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard