简体   繁体   English

在Matlab中创建3D图

[英]Creating a 3D plot in Matlab

I want to create a 3D plot of the final fraction of grass covered on the Earth (=in 2 billion years from now) (=A) as a function of a varying death rate of grass (=D) and the growth rate of grass (=G). 我想创建一个3D图,该图是地球上覆盖的草的最后部分(=从现在起20亿年)(= A)作为草的死亡率(= D)和草的生长速率变化的函数的图(= G)。

The final value of A (at 2 billion years away from now) can be calculated using a loop with the following discritised equation: A的最终值(距现在20亿年)可以使用带有以下等式的循环来计算:

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

Now i'm stuck on how to create a 3D plot of this final value of A as a function of a varying G and D. I got this but after a few trials, it keeps giving errors: 现在,我仍然停留在如何根据变化的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
...??

Can someone help? 有人可以帮忙吗? Thanks! 谢谢!

Your code is wrong, as grassvector(t)=A; 您的代码是错误的,因为grassvector(t)= A; can not be executed, as the sizes are not consistent. 由于大小不一致,无法执行。 However, I think you may want to do: 但是,我认为您可能想要执行以下操作:

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

and in the loop: 并在循环中:

grassvector(:,:,t)=A;

Also, while completely unnecesary computationally, you may want to initialize A to A=0.001*ones(size(Dgrid)) , as it makes more sense logically. 另外,尽管在计算上完全不必要,但您可能希望将A初始化为A=0.001*ones(size(Dgrid)) ,因为从逻辑上讲它更有意义。

Anyways: this is how you can plot it in the end: 无论如何:这是您最终如何绘制的方式:

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

gives: 给出:

在此处输入图片说明

But, as I was actually interested in your research, I decided to make a couple of plots to see how fast the grass will grow and stuff. 但是,由于我实际上对您的研究感兴趣,因此我决定作一些图,以了解草的生长和生长速度。 Here is some nice plotting code. 这是一些不错的绘图代码。 You can modify different stuff here to be able to change the appearance of it. 您可以在此处修改其他内容,以便更改其外观。 I use custom colormaps, so if it doesn't work, delete the colormap(viridis()) line. 我使用自定义颜色图,因此,如果不起作用,请删除colormap(viridis())行。 If you like the colormap, 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

Results: 结果:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM