【问题标题】:Creating a 3D plot in Matlab在 Matlab 中创建 3D 绘图
【发布时间】:2015-11-29 21:27:49
【问题描述】:

我想根据草的不同死亡率 (=D) 和生长情况创建地球上覆盖的草的最终比例(= 20 亿年后) (=A) 的 3D 图草率 (=G)。

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

A(t+dt) = A(t)*((1-A(t))*G-D)*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

现在我被困在如何创建 A 的最终值作为不同 G 和 D 的函数的 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和A的大小不一样。首先,整理你的代码,然后询问绘图!

标签: arrays matlab loops plot


【解决方案1】:

你的代码是错误的,因为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()) 行。如果你喜欢颜色图,visit this

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

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多