所以我正在努力学习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非常陌生,显然我的代码是错误的。我希望你能提供任何帮助。在