How to generate recursive formula with two initial values?
古いコメントを表示
Hi Everyone,
I am having difficulty in writing code for recursive formula. Here is the code
clc;
clear all;
close all;
N=5;
syms a p
%intial values of D
D(1)=1+a*p;
D(2)=3+3*a*p+a^2*p^2;
%Recursive formula for D
D(N)=(2*N-1)*D(N-1)+a^2*p^2*D(N-2)
How to find the value of D for N=5?
Thanks
採用された回答
その他の回答 (1 件)
Walter Roberson
2013 年 12 月 31 日
The general form of those kinds of recursive functions is
function r = recursive_function(N)
if N > number_of_initial_values
r = SomeOperation( N, recursive_function(N-1), recursive_function(N-2), ...)
else
switch N
case 1: r = first_initial_value;
case 2: r = second_initial_value;
....
end
end
end
where SomeOperation() is the mathematical operation connecting the various values. For example for simple Fibonacci sequences,
r = recursive_function(N-1) + recursive_function(N-2);
1 件のコメント
meenakshi
2018 年 1 月 15 日
clc; clear all; close all; N=5; syms a p %intial values of D a=10; p=0.1; D(1)=1+a*p; D(2)=3+3*a*p+a^2*p^2; % %Recursive formula for D for N=3:4 D(N)=(2*N-1).*D(N-1)+a^2*p^2.*D(N-2) end
カテゴリ
ヘルプ センター および File Exchange で Symbolic Math Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!