求高手帮忙Mathematica

来源:百度知道 编辑:UC知道 时间:2024/09/22 10:30:48
求“我国现在人口13.8亿,按每年1.87%的速度增长,问多少年后人口超过15.5亿?”在Mathematica里用While,Do,For求解的语句。

首先,set up你的公式(n=# of 年,P=人口,亿)

In[1]:= g[P_, n_] := 1.0187^n*P

i. For loop(最简单 ^0^)

In[2]:= For[P0 = 13.8; nf = 0, g[P0, nf] < 15.5, nf++; Pf = g[P0, nf]]

In[3]:= {Pf, nf}

Out[3]= {15.711, 7}

ii. Do loop(不知道为什么,我没弄nd1的时候n总是0...do loop没学好 @_@)

In[4]:= P0 = 13.8; nd1 = 0;
Do[Pd = g[P0, nd1]; nd = nd1; If[Pd > 15.5, Break[]], {nd1, 10}];

In[6]:= {Pd, nd}

Out[6]= {15.711, 7}

iii. While loop(跟do loop一样讨厌)

In[7]:= P0 = 13.8; nw = 0;
While[g[P0, nw] < 15.5, nw++; Pw = g[P0, nw]];

In[9]:= {Pw, nw}

Out[9]= {15.711, 7}

对了,我好久没念经济了,所以你最好检查下公式有没有错。错的话改下公式就没问题了。