Numerical Integration inc. a Sumation in equation
조회 수: 2(최근 30일)
I have a sumation in the equation i need to integrate,
symsum(sym('k'), k, 0, 10)
works fine, as does
X = 0:1:10; Y = X.^2; Z = trapz(X,Y)
however I need to somehow combine them, like
X = 0:1:10; Y = X.^2*symsum(sym('X'), X, 0, 10); Z = trapz(X,Y)
but this just gives the error
??? Undefined function or method 'normalizesym' for input arguments of type 'double'.
Error in ==> sym.symsum at 65
if builtin('numel',x) ~= 1, x = normalizesym(x); end
How can I put a equation that is summed over a range of values, into an equation that is then numerically integrated?
(p.s. For reasons out of my control I have a nasty density fn with a summation expression nested within it, which then gets put into another density function which has to be numerically integrated. The above is a just a simple example as Im struggling to get the basics right before tackeling the monster I have to integrate!
Walter Roberson 2011년 3월 2일
After the X = 0:1:10 statement, X has a definite numeric value. When you encounter symsum(sym('X'), X, 0, 10) then the X that is used for the second parameter is the numeric value. Numeric values cannot, however, be used as the names of the variable to sum over. You would need to, e.g., symsum(sym('X'),sym('X'),0,10)
However, symsum(sym('X'),sym('X'),0,10), with its constant bounds, would be a constant, and thus would not need to be calculated within the definition of Y.
Perhaps you are looking for something closer to
Y(J) = X(J).^2 * symsum(sym('k'), k, 0, X(J))
except vectorized. In this particular case you can drop in the identity that the sum of I from 0 to N is N*(N-1)/2, allowing you to vectorize as
Y = X.^2 .* X .* (X-1) ./2
More generally, if you have symsym(f(K),K=A..B) with f(K) giving a definite numeric value for each numeric K, and with B varying, then you can calculate the symsum once with symbolic upper bound, and thereafter subs() the actual upper bound. Or, if the numeric results would be adequately represented as floating point values, apply matlabFunction to the symsum result; you can see from the examples on that page that the result will be vectorized:
mf1 = matlabFuntion( symsum(sym('k'), sym('k'), 0, sym('N')) );
Y = X.^2 .* mf1(X);