【问题标题】:Matlab equivalent of Maple densityplotMatlab 等效于 Maple 密度图
【发布时间】:2018-01-26 20:05:34
【问题描述】:

我想创建这个函数的密度图:

在 Maple 中,可以使用 densityplot 函数来实现这一点(代码在最后),它给出:

但是,我不确定在 MATLAB 中使用什么来绘制类似的图形。

这是我当前的 MATLAB 代码:

x = [0:10:100];
y = [-50:10:50];
s = [10, 0];
i = [50,25];
for ii = 1 : length(x)
    sir(ii) = -10 * 9.8 * log10((power((x(ii) - s(1)),2) + power((y(ii) - s(2)),2)) / (power((x(ii) - i(1)),2) + power((y(ii) - i(2)),2)));  
end

有人可以在 MATLAB 中提出一个等效的建议吗?


对于 Maple 中的密度图,我使用了

densityplot(sir(x,y), x=0..100, y=-50..50, axes=boxed, style=patchnogrid, scaletorange=-5..50, colorscheme = [black, "green", "white"])

【问题讨论】:

  • 请查看我的编辑,我已经展示了如何平滑色带并使其看起来更像您的原件(如果您有兴趣!)
  • 这似乎更像原版。但是,我仍然缺少 S 位置的浅绿色白点。你碰巧知道我怎么能做到这一点吗? scaletorange=-5..50, colorscheme = [black, "green", "white"] 在枫树中成功了。
  • 您使用的是g 的什么值?如果我使用g=9.8,我会得到一个更大的白点(密度> 50 的更大的补丁)。如果你回答我会修改我的问题以获得确切的情节:)
  • 请看我的编辑

标签: matlab plot matlab-figure maple


【解决方案1】:

您可以使用surf(3D 曲面图)来实现此目的,但您需要比 10 步更精细的网格才能看起来不错!

您还需要meshgrid 来获取xy 坐标的所有组合。

请参阅 cmets 了解更多详情。

% Set up grid points
x = 0:0.1:100;
y = -50:0.1:50;
[x,y] = meshgrid(x,y);
% Set up parameters i, s and g
i = [50 25]; s = [10 0]; g = 9.8;
% Work out density  
% - no need for loop if we use element-wise operations ./ and .^
% - power(z,2) replaced by z.^2 (same function, more concise)
% - You forgot the sqare roots in your question's code, included using .^(1/2)
% - line continuation with "...", could remove and have on one line
sir = -10*g*log10( ((x-s(1)).^2 + (y-s(2)).^2).^(1/2) ./ ...
                   ((x-i(1)).^2 + (y-i(2)).^2).^(1/2)        ); 
% Plot, and set to a view from above
surf(x,y,sir,'edgecolor','none','facecolor','interp');
view(2);
% Change the colour scheme
colormap('bone')

结果:

匹配您的示例

您使用了 Maple 命令scaletorange=-5..50。这限制了-550 (docs) 之间的比例,所以由于sir 是我们的比例变量,我们应该同样限制它。在 MATLAB 中:

% Restrict sir to the range [-5,50]
sir = min(max(sir,-5),50);
% Of course we now have to replot
surf(x,y,sir,'edgecolor','none','facecolor','interp');
view(2);

现在,如果您想要黑色/绿色,您可以使用自定义colormap,这也可以消除由'bone' colormap 只有64 种颜色引起的条带。

% Define the three colours to interpolate between, and n interpolation points
black = [0 0 0]; green = [0 1 0]; white = [1 1 1]; 
n = 1000; 
% Do colour interpolation, equivalent to Maple's 'colorscheme = [black, "green", "white"]'
% We need an nx3 matrix of colours (columns R,G,B), which we get using interp1
colormap(interp1(1:3, [black; green; white], linspace(1,3,n)));

使用g=3.5(不确定您使用的是什么),我们得到了几乎相同的情节

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 2013-11-28
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2021-09-10
    相关资源
    最近更新 更多