필터 지우기
필터 지우기

Symbolic summation within a for-loop

조회 수: 2 (최근 30일)
Robert Mason
Robert Mason 2016년 7월 22일
답변: Azzi Abdelmalek 2016년 7월 22일
I have a function that is dependent on two variables, f(j, k). I would like to create a loop that, for each j, sums the function for each value of k. I have tried the code:
for j=1:10;
g(j)=symsum(f(j,k), k, 1, 10);
end;
However, I get an error message "Undefined function 'symsum' for input arguments of type 'double'." on running the code. What is the best way to do the above in MATLAB?

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 22일
symsum cannot use the summation variable to index.
If f is not vectorized then you use
sum( arrayfun(@(k) f(j, k), 1:10) )
If f is vectorized you might be able to use
sum( f(j, 1:10) )
but you might need
sum( f(repmat(j,1,10), 1:10) )
depending on how f is coded.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 22일
Look at this example
syms f j k
f=j*sin(k)
for jj=1:10;
ff(k)=subs(f,j,jj);
s=symsum(ff(k), k, 1, 10);
g(jj)=double(s)
end;

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by