How to solve a nested summation with recursive function?

조회 수: 2 (최근 30일)
Irvanda Kurniadi Virdaus
Irvanda Kurniadi Virdaus 2018년 11월 7일
편집: Irvanda Kurniadi Virdaus 2018년 11월 7일
Hi,
I have a question related to solving a nested summation equation with a recursive function.
Suppose I have an equation of:
So, I tried to solve this problem by using a for loop to solve the summation equation and using a recursive function to expand the order of nested summation.
test(3,0)
function [sum] = test(a,b)
sum = 0;
for i=1:a-b
if a-b == 1
sum = 1;
else
sum = sum + (a-b) * test(a,b+i);
end
end
end
From the code I got
ans = 9
I appreciate any help, thanks.

채택된 답변

Matt J
Matt J 2018년 11월 7일
편집: Matt J 2018년 11월 7일
I think your code is correct, and your calculation by hand is not.
  댓글 수: 1
Irvanda Kurniadi Virdaus
Irvanda Kurniadi Virdaus 2018년 11월 7일
편집: Irvanda Kurniadi Virdaus 2018년 11월 7일
Ah you are right, I think I messed up the break down on the second nested sum.
It should be:
3(2+0)+3(1)+3(0)=9
Thanks for correcting

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by