Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 以图形方式表示迭代的密度_Matlab_Contour_Density Plot - Fatal编程技术网

Matlab 以图形方式表示迭代的密度

Matlab 以图形方式表示迭代的密度,matlab,contour,density-plot,Matlab,Contour,Density Plot,诸位 我在MATLAB中工作,我使用蒙特卡罗技术来拟合模型。基本上,如果我们假设我的模型是一个简单的函数,比如 y=m*x^2+c 我的参数m和c都在0.5到10之间变化,我可以从这样的参数空间中随机抽取,每次都得到一个新的y。如果我画出y的所有实现,我会得到类似下图的东西: 是否有一种方法来表示实现的密度?我的意思是,有没有一种方法(而不是绘制所有的实现)来获得某种等高线图,它位于迭代次数的最小值和最大值之间,它的颜色表示某个时间间隔内的实现量 谢谢大家 这不是很漂亮,但您可以改变参数并使用

诸位

我在MATLAB中工作,我使用蒙特卡罗技术来拟合模型。基本上,如果我们假设我的模型是一个简单的函数,比如

y=m*x^2+c

我的参数m和c都在0.5到10之间变化,我可以从这样的参数空间中随机抽取,每次都得到一个新的y。如果我画出y的所有实现,我会得到类似下图的东西:

是否有一种方法来表示实现的密度?我的意思是,有没有一种方法(而不是绘制所有的实现)来获得某种等高线图,它位于迭代次数的最小值和最大值之间,它的颜色表示某个时间间隔内的实现量


谢谢大家

这不是很漂亮,但您可以改变参数并使用散布/绘图,使其在视觉上更具吸引力。 另外,我假设的是高斯分布,而不是随机分布(完全随机的颜色会给你一个均匀的密度)。此外,此代码还可以针对速度进行优化

n = 1000;
l = 100;
x = linspace(1, 10, l);
y = repmat(x.^2, n, 1);
c = repmat(normrnd(1, 1, n, 1), 1, l);
m = repmat(normrnd(1, 1, n, 1), 1, l);

y = y.*m + c;
p = plot(y', '.');
figure; hold on;
for i = 1:l
    [N,edges,bin] = histcounts(y(:, i));
    density = N./sum(N);
    c = zeros(n, 3);
    for j = 1:n
        c(j, :) = [1-density(bin(j))/max(density), 1-density(bin(j))/max(density), 1-density(bin(j))/max(density)];
    end
    scatter(ones(n, 1)*x(i),y(:, i),[],c,'filled'); 
end
给予


这将为x中的每个位置创建y值的直方图,然后计算点中每个y值和颜色的概率密度。在这里,每个位置x的y值被分别标准化,以根据需要重新标准化的绘图的总体密度对点进行着色。

您可以为
x
的离散点计算
y
,同时为
c
m
设置随机值。然后使用
hist
函数,您可以找到给定
x
函数值的“非标准化密度”。然后可以对其进行规格化以获得值的真实密度,这样分布曲线下的总面积总和为
1

为了使其可视化,沿
X
Y
的值构建网格
[X,Y]
,并将密度值设置为
Z
。现在可以绘制海浪或其等高线图

代码如下:

clear;

n = 1000000; %number of simulation steps

%parameter ranges
m_min = 0.5; m_max = 10;
c_min = 0.5; c_max = 10;

%x points
x_min = 1; x_max = 4; x_count = 100;
x = linspace(x_min, x_max, x_count);
x2 = x.^2;

y_min = 0; y_max = m_max*x_max*x_max + c_max; y_step = 1;

m = rand(n, 1)*(m_max - m_min) + m_min;
c = rand(n, 1)*(c_max - c_min) + c_min;

c = repmat(c, 1, x_count);

y = m*x2 + c;

x_step = (x_max- x_min)/(x_count-1);
[X, Y] = meshgrid(x_min:x_step:x_max, y_min-y_step:y_step:y_max+y_step);
Z = zeros(size(X));

bins = y_min:y_step:y_max;

for i=1:x_count

    [n_hist,y_hist] = hist(y(:, i), bins);

    %add zeros on both sides to close the profile
    n_hist = [0 n_hist 0];
    y_hist = [y_min-y_step   y_hist   y_max+y_step];

    %normalization
    S = trapz(y_hist,n_hist); %area under the bow
    n_hist = n_hist/S; %scaling of the bow

    Z(:, i) = n_hist';
end

surf(X, Y, Z, 'EdgeColor','none');
colormap jet;
xlim([x_min x_max]);
ylim([y_min y_max]);
xlabel('X');
ylabel('Y');

figure
contour(X,Y,Z, 20);
colormap jet;
colorbar;
grid on;
title('Density as function of X');
xlabel('X');
ylabel('Y');
另一个有趣的视图是根据
x
值绘制的每个部分:

以下是此绘图的代码:

clear;

n = 1000000; %number of simulation steps

%parameter ranges
m_min = 0.5; m_max = 10;
c_min = 0.5; c_max = 10;

%x points
x_min = 1; x_max = 4; x_count = 12;
x = linspace(x_min, x_max, x_count);
x2 = x.^2;

m = rand(n, 1)*(m_max - m_min) + m_min;
c = rand(n, 1)*(c_max - c_min) + c_min;

c = repmat(c, 1, x_count);

y = m*x2 + c;

%colors for the plot
colors = ...
[ 0 0 1; 0 1 0; 1 0 0; 0 1 1; 1 0 1; 0 0.75 0.75; 0 0.5 0; 0.75 0.75 0; ...
1 0.50 0.25; 0.75 0 0.75; 0.7 0.7 0.7; 0.8 0.7 0.6; 0.6 0.5 0.4; 1 1 0; 0 0 0 ];

%container for legend entries
legend_list = cell(1, x_count);

for i=1:x_count
    bin_number = 30; %number of histogramm bins
    [n_hist,y_hist] = hist(y(:, i), bin_number);
    n_hist(1) = 0; n_hist(end) = 0; %set first and last values to zero

    %normalization
    S = trapz(y_hist,n_hist); %area under the bow
    n_hist = n_hist/S; %scaling of the bow
    plot(y_hist,n_hist, 'Color', colors(i, :), 'LineWidth', 2);
    hold on;

    legend_list{i} = sprintf('Plot of x = %2.2f', x(i));
end

xlabel('y');
ylabel('pdf(y)');
legend(legend_list);
title('Density depending on x');
grid on;
hold off;

你如何定义密度?你需要某种定义才能实现你所期望的