2017-08-18 93 views
1

我想創建此函數的密度曲線圖:Matlab的等效楓樹densityplot

enter image description here

在楓木,人們可以使用所述densityplot函數來實現這一點(在末尾代碼),這給:

density plot

但是,我不知道用什麼密謀在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等效?


對於華普密度圖,我用

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

請看看我的編輯,我展示瞭如何順利進行,色帶,並使其看起來更像原來的(如果你有興趣!) – Wolfie

+0

這似乎更像原來的。不過,我仍然在S的位置丟失了淺綠色的白色斑點。你碰巧知道我怎麼能做到這一點? 'scaletorange = -5..50,色彩方案= [黑, 「綠色」, 「白」]'的確在楓的伎倆。 – smyslov

+0

你使用了什麼'g'的值?如果我使用'g = 9.8',我會得到一個更大的白點(更大的貼片,其密度> 50)。如果你回答,我會編輯我的問題與修復得到確切的情節:) – Wolfie

回答

3

可以使用surf(三維曲面圖)來實現這一點,但你需要一個更精細的網格比它的10個步驟爲了看上去好點!

您還需要meshgrid才能獲得所有組合的xy座標。

請參閱有關進一步的細節的意見。

% 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') 

結果:

plot

匹配您的例子

您使用的楓葉命令scaletorange=-5..50。這限制-550docs)之間的規模,這樣以來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(不知道你用什麼),我們得到幾乎相同的情節

compare