Differentiation of a function to a variable order

Hi All, when I try to use symsum to calculate the sum of a series which involves a symbolic diff, for example: symsum(diff(sin(x),x,n)/factorial(n),n,0,inf) , I get 0 as the result. This is because diff takes n to be a differentiation varable, not the differentiation degree. So how can I fix that? Many thanks, Oren.

 Réponse acceptée

Walter Roberson
Walter Roberson le 11 Juin 2023

1 vote

If you could do that, there would be three possibilities:
  1. That symsum() hypothetically specially recognizes the construct diff(f(x),x,n)/factorial(n) from 0 to infinity as being the same as the taylor series of f(x) and symsum() hypothetically gives the result as being f(x); or
  2. That symsum() tries to construct an infinite sum and fails running out of memory; or
  3. Thta symsum() pokes around the expression enough to realize that it cannot figure out the answer, and returns an unevaluated symsum() to hold the place of the result
The second and third of those possibilities is of no benefit to you.
Therefore, what you are really hoping to test is whether symsum() is smart enough to recognize it has been asked for an infinite taylor series and to return the function (since the function is, according to theory, the same as the infinite taylor series calculated with infinite precision.)
The answer to that test is, "NO, symsum() is not smart enough to recognize it has been asked to calculate an infinite taylor series."
Is there a way to do what you want? Not that I know of at the moment. (I am having difficulty finding (free) documentation old enough to describe some of the internal workings of the symbolic engine to see if there might be other internal options.)

9 commentaires

You can use the subs function in MATLAB to replace n with the desired differentiation degree before using symsum.
See this example code:
syms x n
% Define the series expression
series = diff(sin(x), x, n) / factorial(n);
% Define the differentiation degree
degree = 3; % Choose the desired differentiation degree
% Substitute 'n' with the desired degree in the series expression
series_subs = subs(series, n, degree);
% Calculate the sum of the series
sum_result = symsum(series_subs, n, 0, inf);
In this code, degree represents the desired differentiation degree. By using the subs function, we substitute n in the series expression with the value of degree. Then we pass the substituted expression, series_subs, to the symsum function to calculate the sum of the series correctly.
Oren
Oren le 11 Juin 2023
Diwakar,
Thank you but wouldn't that always take the 3'd differential of the function instead of the n'th (for n between 0 and infinity)?
Oren
consider
symsum(diff(sin(x),x,n)/factorial(n),n,0,inf)
You can model that as
total = sym(0);
for n = 1 : inf
total = total + diff(sin(x),x,n)/factorial(n);
end
so for any given integer n such as 4231519 the term diff(sin(x),x,4231519)/factorial(4231519) has to be added in to the total -- or else the computation has to be such that the result is as if it had been added in.
But notice the 1:inf :
>> for n = 1 : inf; end
Warning: Too many FOR loop iterations. Stopping after 9223372036854775806 iterations.
MATLAB refuses to try to count beyond 9223372036854775806 = 2^63 . If somehow (such as with a GPU) you were able to run 100 billion n per second, then it would still take close to 3 years to get that far... and you would have gotten nowhere compared to the scale of infinity. If each calculation takes some finite amount of time, no matter how small, even if it were only 10^-43 seconds ("planck time") then calculating to infinity would take infinite time.
Therefore, any approach that actually calculates for each individual n is doomed to failure. In order to get a result other than "ran out of memory" or "gave up trying", symsum() would have to examine the structure of the terms and deduce mathematically what the right answer is .
But in order to have a chance at that, you would need to be able to represent the indefinite derivative in the symbolic engine. I do not know of any way to do that. Unfortunately, the symbolic engine makes derivatives a compiled built-in so I cannot examine the code to see if there is some option that could hypothetically be used.
Oren
Oren le 12 Juin 2023
I thank you all for your answers, however I am afraid my question may have not be clear, and sent you in the wrong direction, so let me clarify: The problem is not with symsum but with diff. For eaxple, this works:
>> symsum(sin(x+pi*k/2)/gamma(k+1),k,0,infinity)
And gives the correct answer sin(x+1). so symsum certainly can recognize that it has to handle an infiniate sum. On the other hand, even if will use diff in the context of a finite sum, it will not be able to pass the summation variable as the diffrentiation grade.
BR,
Oren
Supposing that you could create the construct, there are two possibilities:
  1. That symsum() evaluated each of the infinite terms, which would require infinite time; OR
  2. That symsum() applies mathematical analysis to the expression to try to determine if it can find a closed form expression
Therefore it would not be enough to be able to construct diff(f(x),x,n) with n being a symbolic variable representing order: it would be necessary for symsum() to recognize such a construct and process it correctly.
There is no universal rule for symsum() to be able to find "nice" representations for infinite sums: symsum() has a number of different rules programmed in, and anything that it cannot recognize as being covered by the rules returns unevaluated. Just getting a diff(f(x),x,n) construct into symsum() would not be enough if symsum() does not have rules that cover the construct... and symsum() does not have those rules.
I'm surprised diff doesn't throw an error in this case:
syms x n
diff(sin(x),x,n)
ans = 
0
As I understand it, n has to evaluate to either a numeric or symbolic non-negative integer. But here n is neither of those. What's the justification for returning 0?
It seems your intention is to use the Df = diff(f,var,n) syntax listed on that documentation page. But the call you gave also satisfies the Df = diff(f,var1,...,varN) syntax from the documentation page (with var1 being x and var2 being n.) How does Symbolic Math Toolbox resolve this ambiguity?
The n input argument is defined as being "Order of derivative, specified as a nonnegative integer." and so a symbolic variable n does not satisfy that first syntax. The symbolic variable n is not a nonnegative integer. So the toolbox interprets your call as using the second of the syntaxes I posted.
Since n does not appear as a variable in either sin(x) or the derivative of sin(x) with respect to x, that expression is a constant (with respect to n) and the derivative of a constant is 0.
syms x
diff(42, x)
ans = 
0
syms f(x)
diff(f, x, x)

returns the second derivative. That is, you can provide an ordered list of variables to take the derivative with respect to. So diff(f, x, n) is diff(diff(f, x), n) but since f(x) is independent of n, the result is 0

Paul
Paul le 12 Juin 2023
Not sure how I missed those other usages on the doc page ....

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by