필터 지우기
필터 지우기

How to write sum over expressions in MATLAB if some elements are required to be skipped?

조회 수: 3 (최근 30일)
Hi everyone I wanted to know if there is an easy way to generate MATLAB codes from the complicated symbolic mathematical expressions as shown in the figure. Here, consider that i,j,k runs from 1 to N=10.
I wanted to use that expression further, say solve for some variable as below:
myfun1 = @(x,lambda1) (2*x+lambda1-2);
Any help or suggestion on this aspect will be highly appreciated.
Thanks.

채택된 답변

Walter Roberson
Walter Roberson 2018년 6월 16일
No, symsum has no possibility of excluding a particular value from the indices.
You can take the symsum and subtract off the value when the indices are those unwanted ones.
However, symsum() is not really the right tool to use when you have a known list of indices (in this case, 1 to 10), as symsum() is intended for finding theoretical formulae, such as converting the taylor expansion of exp(x) back into exp(x) . When you have indexing of elements to do, then symsum() can never be used, as it is not possible to index an array at symbolic locations.
For fixed sets of indices, you should be calculating the values individually, and adding them together. You can typically do that in a vectorized fashion. For example, instead of
syms x
symsum( x*(x+1), x, 1, 20 )
you would
sum((1:20).*(2:21))
or
sum( arrayfun(@(x) x.*(x+1), 1:20 ) )
If you used that last form you could exclude values, such as
sum( arrayfun(@(x) x.*(x+1), setdiff(1:20, 7) ) )
to skip the 7*8 term

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by