繁体   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