为什么绘制这个方程不会生成正确的曲线?

问题描述 投票:4回答:2

目的是绘制以下等式:P * sin(x)/ x + cos(x),对于P = 1.6和x在[0,5]中,忽略绿色填充区域,应该看起来像:

enter image description here

但是,从以下代码:

x = 0 : 0.01 : 5;     % ka/pi, where k-wavevector, a-lattice spacing.

P = 1.6;              % 2*m*U_0 / hbar^2.
rhs =  P * sinc(x*pi) + cos(x*pi);
rhs2 = P * ( sin(x*pi) / x*pi) + cos(x*pi);

plot(x, rhs, '--b', x, rhs2, 'b', x, -1*ones(size(x)), 'r', x, 1*ones(size(x)), 'r')
axis([0 5 -3 3])
xlabel('ka/pi')
legend('P*sinc(x) + cos(x)', '(2mU_0b)/(hbar^2) * sin(ka)/ka + cos(ka)', 'y = -1', 'y = 1')

我目前得到的是:

enter image description here

我在这做错了什么?


我在Windows 10,Octave-4.2.1上

matlab octave trigonometry
2个回答
5
投票

sinc的MATLAB定义是sinc(t) = sin(pi t)/(pi t),即你不能在rhs定义中乘以pi

x = 0 : 0.01 : 5;     % ka/pi, where k-wavevector, a-lattice spacing.

P = 1.6;              % 2*m*U_0 / hbar^2.
rhs =  P * sinc(x)+ cos(x*pi);
rhs2 = P * (sin(x*pi) / x*pi) + cos(x*pi);

plot(x, rhs, 'b', x, rhs2, '--b', x, -1*ones(size(x)), 'r', x, 1*ones(size(x)), 'r')
axis([0 5 -3 3])
xlabel('ka/\pi')
legend('P*sinc(x) + cos(x)', '(2mU_0b)/(hbar^2) * sin(ka)/ka + cos(ka)', 'y = -1', 'y = 1')

enter image description here

另外请注意,对于t=0 sinc(t)=1,而你的rhs2sin(x pi)/(x pi)x=0返回NaN,因此两个信号的差异,因为第二个是纯余弦。


我在OP的sinc实现中错过了元素明智的划分和缺少括号,请参阅am304's answer。请注意,即使使用元素明智的划分和括号,你仍然会错过x=0的观点,因为这将导致NaN


5
投票

您的代码中有两个错误:

  • 正如Adriaan指出的那样,第一个是错误地使用sinc函数。
  • 第二个,你是否忘记在实现自己的sinc版本时进行元素划分

这是更正后的版本 - 请注意./

x = 0 : 0.01 : 5;     % ka/pi, where k-wavevector, a-lattice spacing.

P = 1.6;              % 2*m*U_0 / hbar^2.
rhs =  P * sinc(x)+ cos(x*pi);
rhs2 = P * (sin(x*pi) ./ (x*pi)) + cos(x*pi);

plot(x, rhs, 'b', x, rhs2, '--b', x, -1*ones(size(x)), 'r', x, 1*ones(size(x)), 'r')
axis([0 5 -3 3])
xlabel('ka/pi')
legend('P*sinc(x) + cos(x)', '(2mU_0b)/(hbar^2) * sin(ka)/ka + cos(ka)', 'y = -1', 'y = 1')

结果图是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.