MATLAB两个for循环用于范围和递归方程

时间:2015-08-16 23:48:02

标签: matlab plot

我得到了一个递推方程:X(n)= A X(n-1)/ 1 + B X(n-1)。我无法将此解决方案绘制到MATLAB 2014b中,后者返回的图形在单个图形上绘制了范围为O:10的多条曲线。到目前为止,这是以y形式制作范围的方式:

function questions1 ()
N = 100; %Xn in the form of n
X = zeros (N,1);
X(1) = 0;
A = 2;
B = 1;

for y = 0:10; %this is the range from 0:10 to plot curves
    for n = 2:N;
          X(n) = A*X(n-1)/1+B*X(n-1); %this is the recurrence equation
    end
end

hold on;
plot(X);
hold off;

1 个答案:

答案 0 :(得分:1)

请注意

X(0)=0
x(1)=a*x(0)/1+b*x(0)=a*0+b*0=0
x(2)=a*x(1)+b*x(1)=a*0+b*0=0
..
∀n, x(n)=0

所以你的递归方程非常糟糕.. 并且你在每次y迭代中替换x(n),这是一个修复:

function question1()
N = 100; %Xn in the form of n
X = zeros(N,1);
X(1) = 0;
A = 2;
B = 1;
hold on;
for y = 0:10; %this is the range from 0:10 to plot curves
    for n = 2:N;
        X(n) = A*X(n-1)/1+B*X(n-1); %this is the recurrence equation
    end
    plot(X);
end
hold off;