Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does plotting this equation not generate the correct curve?

The intention is to plot the following equation: P*sin(x)/x + cos(x), for P = 1.6 and x in [0, 5], which, ignoring the green-filled areas, should look someting like:

enter image description here

However, from the following code:

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')

what I currently get is:

enter image description here

What am I doing wrong here?


I am on Windows 10, Octave-4.2.1

like image 269
Ziezi Avatar asked Dec 24 '22 04:12

Ziezi


1 Answers

The MATLAB definition of the sinc is sinc(t) = sin(pi t)/(pi t), i.e. you must not multiply by pi in the rhs definition:

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

Also note that for t=0 sinc(t)=1, whereas your rhs2 has sin(x pi)/(x pi), which for x=0 returns NaN, hence the difference in the two signals, as the second is a pure cosine.


I missed the element wise division and lack of brackets in the OP's sinc implementation, see am304's answer for that. Note that even when using element wise division and brackets you'll still miss the point at x=0, since that'll result to NaN.

like image 54
Adriaan Avatar answered Jan 17 '23 15:01

Adriaan