matlab用Euler方程求,MatLab到Python-Euler方程

所以我正在努力学习python,我认为一个好的方法是把我以前在MatLab中完成的问题集转换成python。下面是我正在使用的MatLab代码% C14 halflife is 5726 years

% The time constant tau is t(1/2)/ln2 = 8260 y

N0=10000; %initialize N0

tau=8260; %Carbon 14

tmax=40000; %max time value, will be on the x-axis

% Generate data using exact values

t1=linspace(0,tmax,100);

N1=N0*exp(-t1/tau);%Here we introduce the equation for nuclear decay

figure

plot1 = plot(t1,N1);

% Generate data using Euler

Step=1000;

N=N0;

NumRes=N;

tx=0:Step:tmax;

% This is the taylor series generation of data.

for t=Step:Step:tmax

N=N-Step*N/tau;

NumRes=[NumRes,N];

end

% Plot the approximation

hold on

plot2 = plot(tx,NumRes,'+');

我得到了python解决方案的确切部分,如下所示。但是我不能得到近似部分。在

^{pr2}$

我不知道如何得到近似部分,但这是我的尝试。。。在

^{3}$

我收到的错误消息plt.plot(tx,result)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3154, in plot

ret = ax.plot(*args, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 1811, in inner

return func(ax, *args, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 1427, in plot

for line in self._get_lines(*args, **kwargs):

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 386, in _grab_next_args

for seg in self._plot_args(remaining, kwargs):

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 364, in _plot_args

x, y = self._xy_from_xy(x, y)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 223, in _xy_from_xy

raise ValueError("x and y must have same first dimension")

ValueError: x and y must have same first dimension

我对python非常陌生,显然我的代码是错误的。我希望你能提供任何帮助。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值