Difficulty using "symsum" within a function definition

조회 수: 1 (최근 30일)
Riacon12
Riacon12 2018년 4월 27일
댓글: Riacon12 2018년 4월 27일
I'm trying to define a function "ansin" that takes in an array of test values and outputs the function values of the n-th degree Taylor polynomial for sin(x). I am totally unfamiliar with symsum and just tried to do the best I could to copy the notation in the MatLab documentation . Below is the summation that I'm trying to emulate, the code I used and the error I got.
The only difference between the formula and my code is that I've replaced all the n's with k's so that "n" can stand for the degree of taylor polynomial I want. Also I am not summing to infinity but to "n".
testcases = -2*pi + (4*pi).*rand(2000,1);
pred1 = ansin(testcases,1);
pred2 = ansin(testcases,2);
pred3 = ansin(testcases,3);
pred4 = ansin(testcases,4);
function y = ansin(x,n)
y = symsum(((-1)^(k-1))*((x^(2*k-1))/factorial(2*k-1)), k, 1, n);
end
Undefined function or variable 'k'.
Error in Taylor_Polynomial_Practice>ansin (line 10) y = symsum(((-1)^(k-1))*((x^(2*k-1))/factorial(2*k-1)), k, 1, n);
Error in Taylor_Polynomial_Practice (line 2) pred1 = ansin(testcases,1);
  댓글 수: 1
Riacon12
Riacon12 2018년 4월 27일
I'd also like to add that I know there is taylor series functionality right in Matlab, but I'm using this example to get some experience with the symsum command.

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

답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 27일
This is correct. You do not have a definition for k.
syms k
  댓글 수: 2
Riacon12
Riacon12 2018년 4월 27일
Then I get the following:
Error using symengine Not a square matrix.
Error in sym/privBinaryOp (line 973) Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in ^ (line 310) B = privBinaryOp(A, p, 'symobj::mpower');
Error in Taylor_Polynomial_Practice>ansin (line 9) y = symsum(((-1)^(k-1))*((x^(2*k-1))/factorial(2*k-1)), k, 1, n);
Error in Taylor_Polynomial_Practice (line 2) pred1 = ansin(testcases,1);
Riacon12
Riacon12 2018년 4월 27일
Nevermind, I needed to use ".^" instead of "^". Thank you for your help!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by