簡體   English   中英

在Matlab中創建3D圖

[英]Creating a 3D plot in Matlab

我想創建一個3D圖,該圖是地球上覆蓋的草的最后部分(=從現在起20億年)(= A)作為草的死亡率(= D)和草的生長速率變化的函數的圖(= G)。

A的最終值(距現在20億年)可以使用帶有以下等式的循環來計算:

A(t + dt)= A(t)*((1-A(t))* GD)* dt + A(t)

%Define variables and arrays

D=0.1;                              %constant value 
G=0.4;                              %constant value
A=0.001;                            %initial value of A at t=0
t=0;
dt=10E6; 
startloop=1;                        %define number of iterations
endloop=200;                  

timevector=zeros(1,endloop);        %create vector with 0
grassvector=zeros(1,endloop);

%Define the loop

for t=startloop:endloop
    A=A.*((((1-A).*G)-D)) + A;   
    grassvector(t)=A;
    timevector(t)=t*dt;
end

現在,我仍然停留在如何根據變化的G和D創建A的最終值的3D圖的過程中。我得到了這一點,但是經過幾次試驗,它始終會出錯:

%(1) Create array of values for G and D varying between 0 and 1

A=0.001;
G=[0.005:0.005:1];           %Vary from 0.005 to 1 in steps of 0.005
D=[0.005:0.005:1];           %Vary from 0.005 to 1 in steps of 0.005

%(2) Meshgrid both variables = all possible combinations in a matrix

[Ggrid,Dgrid]=meshgrid(G,D);

%(3) Calculate the final grass fraction with varying G and D

D=0.1;
G=0.4;
A=0.001;
t=0;
dt=10E6; 
startloop=1;                        %define number of iterations
endloop=200;                  

timevector=zeros(1,endloop);        %create vector with 0
grassvector=zeros(1,endloop);

%Define the loop

for t=startloop:endloop
    A=A.*((((1-A).*Ggrid)-Dgrid)) + A;   
    grassvector(t)=A;
    timevector(t)=t*dt;
end

%(4) mesh together with D and G
...??

有人可以幫忙嗎? 謝謝!

您的代碼是錯誤的,因為grassvector(t)= A; 由於大小不一致,無法執行。 但是,我認為您可能想要執行以下操作:

grassvector=zeros([size(Ggrid),endloop]);

並在循環中:

grassvector(:,:,t)=A;

另外,盡管在計算上完全不必要,但您可能希望將A初始化為A=0.001*ones(size(Dgrid)) ,因為從邏輯上講它更有意義。

無論如何:這是您最終如何繪制的方式:

surf(Ggrid,Dgrid,A,'LineStyle','none');
xlabel('growth rate ')
ylabel('death rate ')
zlabel('grass')
colorbar

給出:

在此處輸入圖片說明

但是,由於我實際上對您的研究感興趣,因此我決定作一些圖,以了解草的生長和生長速度。 這是一些不錯的繪圖代碼。 您可以在此處修改其他內容,以便更改其外觀。 我使用自定義顏色圖,因此,如果不起作用,請刪除colormap(viridis())行。 如果您喜歡顏色圖, 請訪問此

fh=figure();
filename='grass.gif';
for t=startloop:endloop
    clf
    hold on
    surf(Ggrid,Dgrid,grassvector(:,:,t),'LineStyle','none');
    [c,h]=contour3(Ggrid,Dgrid,grassvector(:,:,t)+0.05,[0:0.1:1],'LineColor',[153,0,18]/255,'LineWidth',2);
    clabel(c,h);
    xlabel('growth rate ')
    ylabel('death rate ')
    zlabel('grass')
    title(['Years passed: ' num2str(t*dt/1000000) ' million'])
    colormap(viridis())
    axis([0 1 0 1 0 1])
grid on
    view(-120,40);

    frame = getframe(fh);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    if t == 1;
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',0.1);
    end
end

結果:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM