How do we differentiate functions involving integrals and summations using the symbolic toolbox?

조회 수: 3 (최근 30일)
How do we obtain partial derivatives (with respect to w, T, s, V_i, D_i) of the following function using the symbolic toolbox?
It appears to me that the user must 'manually' use the chain rule and second fundamental theorem of calculus and only input pieces of this function into the diff command, keeping in mind what the result should be in the case of a summation. For example,
syms V D w T s
f(V,D,w,T,s) = ( ( V*D^(1/w) )^w - T)/(s*T);
diff(f,V)

채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 12일
You have not coded a summation.
In MATLAB, individual symbols like V never stand in for matrices.
syms V D
V*D
is not algebraic matrix multiplication of a matrix of unknown size, V, and a different matrix of unknown size, D. You need to create arrays of symbols if you want algebraic matrix multiplication.
You already implicitly acknowledged that you are not working with arrays when you coded D^(1/w) because if D were a matrix then D^(1/w) would be a matrix power, which would require that matrix be square, which is incompatible with there being only a single subscript for D.
N = 5;
Pi = sym('pi');
syms w T s x
V = sym('v%d_%d', [1, N]);
D = sym('d%d_%d', [N, 1]);
f = ((V*D.^(1/w)).^w - T)/(s*T);
P_c = 1/sqrt(2*Pi) * int(exp(-x^2/2), x, -inf, f)
diff(P_c, T)
Now notice that
>> syms VV DD; ff(VV,DD) = VV*DD;
>> ff(V, D)
Error using symengine
New arrays must have the same dimensions or must be scalars.
Error in sym/subs>mupadsubs (line 150)
What this implies is that at the time that VV*DD was evaluated in building ff(VV,DD), MATLAB noticed that VV and DD were scalar symbols, and so it built something that was the equivalent of ff(VV,DD) = VV.*DD internally. Once it had done that, then substituting in the vectors V and D does not become equivalent to having coded V*D (with vector V and vector D) in the first place.
This does mean that you cannot define a straight forward symbolic expression that can accept vector inputs and treat "*" or "/" algebraically: the symbols on the left and right of the operator are examined when the right hand side is being built, and if those symbols are not scalar then the expansions are dropped in, and the operator evaluated -- after which point the function that will be created will treat all symbols remaining element-wise.
One thing to keep in mind is that the expression
f(V,D,w,T,s) = ( ( V*D^(1/w) )^w - T)/(s*T)
is not equivalent to defining
function result = f(V, D, w, T, s)
result = ( ( V*D^(1/w) )^w - T)/(s*T);
Instead,
f(V,D,w,T,s) = ( ( V*D^(1/w) )^w - T)/(s*T)
means
temporary = ( ( V*D^(1/w) )^w - T)/(s*T)
f = symfun(temporary, [V, D, w, T, s])
That is, the right hand side is fully evaluated symbolically, and the resulting expression will become the body of the function. (You can see this happening if you dig into the right parts of the implementation of the symbolic toolbox.)
Symbolic functions are not defined procedurally, in terms of binding variables and deferring execution until later. If you need that, then you can use anonymous functions. (Just remember that to differentiate an anonymous function, you have to turn it into a symbolic expression by passing parameters... at which time you are back to the treatment of scalar variables as thereafter implying element-by-element operations.)
  댓글 수: 2
Daniel Bridges
Daniel Bridges 2017년 5월 12일
편집: Daniel Bridges 2017년 5월 12일
You suggest that we create a matrix of variables, but I cannot say in advance how many terms the summation will have. My goal here is to obtain a second-check for my analytical solution (partial derivatives obtained by hand).
So is there no way to create a symbolic function involving integrals and summations of unspecified length?
Walter Roberson
Walter Roberson 2017년 5월 12일
"So is there no way to create a symbolic function involving integrals and summations of unspecified length?"
You can use anonymous functions to delay some of the binding, but as soon as you try to diff() you have to plug in symbolic variables that are treated the way I describe above.
The difficulty is not just at the level of the interface between MATLAB and MuPAD: the difficulty is at the MuPAD level itself, so even if you use tricks such as using evalin(symengine) to create functions at the MuPAD level, they are incapable of treating a variable as standing in for a matrix of unknown size for the purposes of algebraic matrix manipulation.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

José Federico Geli Manzano
José Federico Geli Manzano 2020년 12월 2일
Hi, I have slightly different problem I need to take the derivative of an infinite sum with respect to time (which is a discrete variable). A simple example:
I have tried:
>> syms c(t) sigma;
>> assume(sigma>0);
>> assume(sigma<1);
>> S=symsum((c/(1-sigma))^(1-sigma),t,1,Inf);
>> diff(S,c(t))
ans =
piecewise(c(t) == 0 & sigma < 0, 0, c(t) ~= 0 | 0 <= sigma, Inf/(-c(t)/(sigma - 1))^sigma)
There are two problems. First, it does not recognise the assume command since its giving an answer depending on the value of sigma. Second, the most important, none of the proposed solutions yields the result obtained by hand. In the summation C(t) appears only one because then it becames t+1, t+2, etc so then the derivative is trivial.
Any ideas welcomed. Thanks!

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by