Create real function from symbolic sum formula

In summary, without Evaluate, the Sum is held unevaluated and is then evaluated either way depending on if the argument is numeric or symbolic. With Evaluate, the Sum is evaluated immediately, while x is still symbolic.
  • #1
Swamp Thing
Insights Author
908
572
In this code, I define a function of x as the sum of the first x integers.
Code:
In[7]:= fnSum[x_] := Sum[k, {k, 1, x}]

In[8]:= fnSum[x]

Out[8]= 1/2 x (1 + x)

In[9]:= fnSum[3.5]

Out[9]= 6

I would like now to take the symbolic formula underlying fnSum, and use it with real arguments. How can I assign that formula to a new real function, such that fnSum[3.5] will return the correct non-integer value instead of just 6 ?

Of course, this is just an example -- I would like to know how to do this in general.
 
Physics news on Phys.org
  • #2
In your function definition use Evaluate on the right hand side
 
  • Informative
Likes Swamp Thing
  • #3
Let me see if i understand you. You want the function, if ##x## is integer to return $$\sum_{n=1}^{x}n$$ but if x is real you want it to return $$(\sum_{n=1}^{[x]}n)+x-[x]$$

where the symbols [x] denote the integer part of x.

Did i get this right?
 
  • #5
Ok well if i understand you now, why not define the function as ##\frac{x}{2}(x+1)## in first place instead of the sum definition?
 
  • #6
Because I want to do the same with other sums like x^3, x^5 etc and it would be tedious to type each formula.

Using the Evaluate suggestion, I just do:
fnSum3[x_] = Evaluate[Sum[k*k*k, {k, 1, x}]]

which I might even generalize with a variable power.
 
  • Like
Likes Dale
  • #7
Ok i see now thanks. Well its good thing that @Dale was very helpful with his suggestion.
 
  • #8
It has taken me over a year to understand the power of Mathematica when it comes to this kind of thing, and now I'm able to ask sensible questions anticipating that neat solutions will exist.
 
  • Like
Likes Dale
  • #9
Swamp Thing said:
It has taken me over a year to understand the power of Mathematica when it comes to this kind of thing, and now I'm able to ask sensible questions anticipating that neat solutions will exist.
Yes, it is very powerful. I have used it for everything from learning physics to helping my wife design a good patchwork quilt.

Do you understand why using Evaluate had the result that it did?
 
  • #10
Dale said:
Do you understand why using Evaluate had the result that it did?

Not really.

I looked at the Wolfram page on Evaluate, and it says "causes expr to be evaluated even if it appears as the argument of a function whose attributes specify that it should be held unevaluated."

If I type in Attributes[Sum], it includes "HoldFirst" in the list, i.e. the first argument is to be held unevaluated.

But if I define my own function with OR without evaluate, it doesn't list any attributes, e.g. Attributes[MyFunction] is empty.

In any case, it's not clear how deferring evaluation (or not) would affect whether the argument is to be rounded off or not before calculation.
 
  • #11
Actually, the important part is not Sum, it is := which is SetDelayed. SetDelayed has the attribute HoldAll, so the entire right hand side is unevaluated and gets evaluated fresh each time the left hand side is called.

When Sum is evaluated, if x is a number then it evaluates the Sum numerically, and if x is a symbol it evaluates the Sum symbolically.

So, without Evaluate the Sum is held unevaluated until the left hand side is called and then it is evaluated either way depending on if the argument is numeric or symbolic.

But with Evaluate the Sum is evaluated immediately, while x is still symbolic. This returns the algebraic expression which is then evaluated when the left hand side is called.
 
  • Informative
Likes Swamp Thing

1. How do you create a real function from a symbolic sum formula?

To create a real function from a symbolic sum formula, you need to replace the variable in the formula with a numerical value and then solve the formula to get the corresponding output. This process is repeated for multiple values of the variable to create a set of input-output pairs, which can then be plotted to visualize the real function.

2. What is a symbolic sum formula?

A symbolic sum formula is a mathematical expression that represents the sum of a sequence of terms. It contains a variable, usually represented by the letter "n", that is used to indicate the position of each term in the sequence. This type of formula is commonly used in mathematics and computer science to represent patterns and series.

3. What is the difference between a real function and a symbolic sum formula?

A real function is a mathematical rule that assigns a unique output to every input. It is typically represented by an equation or a graph. On the other hand, a symbolic sum formula is a mathematical expression that represents the sum of a sequence of terms. While a real function can be created from a symbolic sum formula, they are not the same thing.

4. What are some examples of real functions created from symbolic sum formulas?

Some common examples of real functions created from symbolic sum formulas include the geometric series, the Taylor series, and the Riemann zeta function. These functions are used in various fields of mathematics and have real-world applications in areas such as physics, engineering, and finance.

5. Why is it important to create real functions from symbolic sum formulas?

Creating real functions from symbolic sum formulas allows us to analyze and understand the behavior of complex mathematical expressions. By plotting these functions and observing their patterns and trends, we can gain insights and make predictions about real-world phenomena. Additionally, real functions have practical applications in fields such as data analysis, signal processing, and optimization.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
878
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Replies
2
Views
891
Replies
4
Views
421
Back
Top