在Matlab中创建3D绘图

use*_*251 1 arrays matlab plot loops

我想创建一个地球覆盖的草的最后部分的3D图(从现在开始的20亿年)(= A)作为草的死亡率(= D)和草的生长速率的函数(= G).

A的最终值(距离现在20亿年)可以使用具有以下discritised方程的循环来计算:

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
Run Code Online (Sandbox Code Playgroud)

现在我停留在如何创建A的最终值的3D图表作为变化的G和D的函数.我得到了这个,但经过几次试验后,它一直给出错误:

%(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
...??
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?谢谢!

And*_*uri 5

你的代码错了,就像grassvector(t)= A; 由于尺寸不一致,因此无法执行.但是,我想你可能想做:

grassvector=zeros([size(Ggrid),endloop]);
Run Code Online (Sandbox Code Playgroud)

并在循环中:

grassvector(:,:,t)=A;
Run Code Online (Sandbox Code Playgroud)

此外,虽然完全unnecesary计算,你可能要初始化AA=0.001*ones(size(Dgrid)),因为它更有意义逻辑.

无论如何:这就是你最终可以如何绘制的:

surf(Ggrid,Dgrid,A,'LineStyle','none');
xlabel('growth rate ')
ylabel('death rate ')
zlabel('grass')
colorbar
Run Code Online (Sandbox Code Playgroud)

得到:

在此输入图像描述

但是,由于我实际上对你的研究感兴趣,我决定制作一些情节,看看草的生长速度有多快.这是一些不错的绘图代码.你可以在这里修改不同的东西,以便能够改变它的外观.我使用自定义色彩映射,因此如果它不起作用,请删除该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
Run Code Online (Sandbox Code Playgroud)

结果: