MATLAB中的递归函数求和

时间:2010-05-16 22:56:22

标签: matlab

B=[1 1 1 1 1 1....1] % vector of length N elements

Xk= sin(2*pi/16) 

我需要找到具有两个变量l和k的函数alpha(l,k)和给定alpha(1,0)= alpha(l,-1)= alpha(l,-2)的条件。 ..... = alpha(l, - (N-1))= 0即无论l的值是什么,过去的值为alpha = 0

A= input('no of iterations'); % no. of iterations user want
N=input('N values of alpha:')
alpha1=[];

for k=0:A-1

l=0:N-1    % need 10 separate alpha values for every k, which goes from 0 to A-1 

alpha(l,k)= Xk + summation( B(j)*alpha(l,k-j)) % as summation goes from j=1 to N


alpha1=[alpha1 alpha]

end;

任何人都可以帮我解决这个递归函数,我是matlab的新手。

阿尔法

1 个答案:

答案 0 :(得分:1)

当我读到这个问题时,答案基本上应该是这样的:

function x=alpha(l,k)
if k<=0
  x=0;
  return
else
  % from your code
  x = Xk + summation( B(j)*alpha(l,k-j)) % as summation goes from j=1 to N
end

当然你需要在函数中添加声明......