函数随机迭代MATLAB方案



任何人都可以通过MATLAB代码帮助我进行以下迭代方案,在 Mathbb r $ in Mathbb r $中,我们可以使用$ x in mathbb r $,然后使用概率$ p_1,p_2,p_2 = 1-p_2$,我们选择$ g_1 $和$ g_2 $,这样就以$ 10 $ times做同样的事情,然后我开始使用概率$ q_1,q_2 = 1-q_1 $从$ 11^{th} $ iTeration选择它们,何处$ p_1 ne p_2 ne q_1 ne q_2 $。

我确实喜欢这个,但似乎没有奏效我描述的情况。感谢您的帮助。

x0=something;
cumweight= [p_1; 1-p_1];
g1=x^2+1;
g2=3*x+x^2+1;
x = zeros(n,1);
y = zeros(n,1);
    n=20;
for i=1:10
    r = rand;
    choice = find(cumweight >r, 1 );
    switch (choice)
        case 1
            g1(i+1)=g1(x0);
        case 2 
            g2(i+1)=g2(x0);
    end
end

这是一个脚本,它独立于彼此而独立于概率选择G和Theta (i([p,1-p]对于I< = 10和(ii([q,1-q] for 11< = i< = 20

您可以在它上构建以执行所需的x(n 1(的确切计算。

clc
clear all
close all
%x0=something;
p_1 = 0.2;
q_1 = 0.3;
g1=1;
g2=2;
theta1 = 1;
theta2 = 2;
n = 20;
x = zeros(n,1);
y = zeros(n,1);    
for i=1:20
    r1 = rand; % random number of choice of g1/g2
    r2 = rand; % random number of choice of theta1/theta2
    if(i <= 10)
         test = p_1;
    elseif(i <= 20) 
         test = q_1;
    end
    choiceG = test <= r1;
    choiceTheta = test <= r2;
    % chose theta
    if(choiceTheta)
        theta = theta1;
    else
        theta = theta2;
    end
    % chose G
    if(choiceG)
        % the text below is chosen with probability p_1 for i <=10 and
        % with probability q_1 for 11 <= i <= 20
        g = g1;
    else
        % the text below is chosen with probability (1 - p_1) for i <=10 and
        % with probability (1-q_1) for 11 <= i <= 20
        g = g2;
    end
    disp([' chose g' num2str(g) ' and theta' num2str(theta)]) % only for ilustration purpose
    % you can set the required computation for x_{n+1} here
end

最新更新