matlab - Problems with symsum() function in Symbolic Math Toolbox -
i'm having problems when using symsum
function in matlab's symbolic math toolbox. code should returning:
ans = sin(x) + x*cos(x) - x^2 / 2 * sin(x)
i think has symbolic variables, i'm new matlab appreciated.
here's code:
syms x i; f(x) = sin(x); symsum(x^i/factorial(i)*diff(f,x,i), i, 0, 2)
which returns 0
instead of correct result indicated above.
this occurs because diff(f,x,i)
evaluates zero. when using symsum
, need aware that, matlab function, input arguments evaluated before being passed in. use for
loop (sym/diff
not vectorized in third argument – see below):
syms x y; f(x) = sin(x); n = 0:2; y = 0; = n y = y+x^i/factorial(i)*diff(f,x,i); end
alternatively, try form (in case, 3 indexes, above more efficient):
syms x y; f(x) = sin(x); n = 0:2; % increasing orders of differentiation y = diff(f,x,n(1)); yi = [y(x) zeros(1,length(n)-1)]; % index array, yi cannot symfun = 2:length(n) % calculate next derivative previous yi(i) = diff(yi(i-1),x,n(i)-n(i-1)); end % convert yi symfun output symfun y = sum(x.^n./factorial(n).*symfun(yi,x));
why diff(f,x,i)
evaluate 0 though i
symbolic? documentation sym/diff
:
diff(s,n), positive integer n, differentiates s n times.
diff(s,'v',n) , diff(s,n,'v') acceptable.
in other words, function doesn't support symbolic variables specify order of integration. order, n
(or i
in code) limited scalar. mupad's related functions have similar limitations unfortunately.
in opinion, sym/diff
should throw error if has limitation rather returning garbage. i'd recommend file service request mathworks report issue. request function updated handle symbolic variables order of integration input.
Comments
Post a Comment