Matlab仿真(随机)

时间:2018-05-07 12:32:46

标签: matlab simulation stochastic

假设我有一个离散化的SDE系统

x(:, t+1) = x(:, t) + f1(x(:, t)).*x(:, t)*dt + f2(x(:, t))./(x(:, t).*y(:, t))* sqrt(dt)*rand1;

y(:, t+1) = f2(x(:, t)).*y(:, t)./x(:, t)*dt + f1(x(:, t)).*y(:, t)*sqrt(dt)*rand2;

我想用10000轨迹模拟系统,

对于时间t = 100天,这样:从星期一到星期五,

f1(x(:, t)) = 2*x(:, t).^2./(y(:, t) + x(:, t) + c)

f2(x(:, t)) = y(:, t).^2;而星期六和星期日

f1(x(:, t)) = x(:, t)./y(:, t)f2(x(:, t)) = y(:, t); 我该如何模拟SDE系统?

这是我的方法

dt = 0.01;
time = 100;
num_iteration = ceil(time / dt);
num_trajectory = 10000; 
%% Initial Values
y0 = 1; 
x0 = 1;
y = zeros(num_trajectory, num_iteration) + y0; 
x = zeros(num_trajectory, num_iteration) + x0; 
days = 0;

for t=1: num_iteration
    current_time = t * dt;
    rand1 = randn(num_trajectory, 1);
    rand2 = randn(num_trajectory, 1);

    if ceil(current_time) == current_time
        days = days+1;

        if (mod(days, 7) | mod(days+1, 7)) == 0
            f1 = 2*x(:, t).^2./(y(:, t) + x(:, t) + c);
            f2 = y(:, t).^2;
        else
            f1 = x(:, t)./y(:, t);
            f2 = y(:, t); 
        end
    end

    x(:, t+1) = x(:, t) + f1*x(:, t)*dt + f2/(x(:, t).*y(:, t))* sqrt(dt)*rand1;
    y(:, t+1) = f2*y(:, t)./x(:, t)*dt + f1*y(:, t)*sqrt(dt)*rand2;   
end

1 个答案:

答案 0 :(得分:0)

你的方法似乎很好。但是,您的代码中存在逻辑错误。在行

if (mod(days, 7) | mod(days+1, 7)) == 0

表达式(mod(days, 7) | mod(days+1, 7))将始终计算为1(尝试弄清楚为什么),因此(mod(days, 7) | mod(days+1, 7)) == 0将始终为false,并且if语句将始终将控制权传递给{{1部分。

因此,这应该是类似

else

但这也令人困惑(你没有在代码中记录星期几'0'的哪一天)。

更清楚的是:

if mod(days, 7) == 0 || mod(days+1, 7) == 0

更好的是,创建一个小函数if ( mod (days, 7) == 0 % day is a sunday || mod (days, 7) == 6 % day is a saturday ) % do stuff else % do other stuff end ,为您执行该测试,从而产生超清代码,例如

isWeekend