写在前面
用MATLAB作图,查看其曲线和求导曲线是否和教材相一致,其中用了sin求导(角度制),以一个函数为例:
y = C s i n d ( x ) y=\frac{C}{sind(x)} y=sind(x)C
(sind(x)是以角度为单位后同),然后对它进行求导:
d y d x = − C ⋅ c o s d ( x ) ( s i n d ( x ) ) 2 = − y ⋅ c o s d ( x ) s i n d ( x ) = − y ⋅ c o t d ( x ) \frac{dy}{dx}=-\frac{C\cdot cosd(x)}{(sind(x))^2}=-\frac{y\cdot cosd(x)}{sind(x)}=-y\cdot cotd(x) dxdy=−(sind(x))2C⋅cosd(x)=−sind(x)y⋅cosd(x)=−y⋅cotd(x)
y = 1 s i n d ( x ) y=\frac{1}{sind(x)} y=sind(x)1是单调递减函数,教材的图应该是对他进行了取绝对值的操作(要不原来是个负数),也即:
∣
d
y
d
x
∣
=
y
⋅
c
o
t
d
(
x
)
|\frac{dy}{dx}|=y\cdot cotd(x)
∣dxdy∣=y⋅cotd(x)
编写下面的代码
clear
close all
pm_theta_half=8:0.1:28;
pm_lambda_B0=1.9;
pm_constant=0.2644;
pm_lambda_B = pm_constant./sind(pm_theta_half);
dif_pm_lambda_theta_num=abs((diff(pm_lambda_B))./(diff(pm_theta_half)));
dif_pm_lambda_theta_cal=pm_lambda_B.*cotd(pm_theta_half);
colororder({'[0.2,0.5,0.7]','[0.17 0.31 0.16]'})
yyaxis left
plot(pm_theta_half,pm_lambda_B,...
'LineWidth',3, ...
'Color',[0.2,0.5,0.7]);
ylabel('Bragg wavelength, \mum','FontSize',14);
yyaxis right
plot(pm_theta_half(1,1:end-1),dif_pm_lambda_theta_num, ...
'-.',...
'LineWidth',3, ...
"Color",[0.17 0.31 0.16])
hold on;
scatter(pm_theta_half,dif_pm_lambda_theta_cal);
xlabel('Angle, \theta/2, degrees','FontSize',14)
ylabel('\Delta \lambda_b/degree \mum/deg','FontSize',14);
xaxis(8,28);
xticks(linspace(8,28,11))
ax = gca;
ax.FontSize = 14;
但是发现用离散求导和实际求导差很大,(绿色画圈的是求导计算式的值(不符合实际,范围和教材的图差别很大),点划线是离散求导的值(符合实际,范围和教材的图大致一样))。
利用MATLAB自带的diff函数进行求解,终于找到了问题的所在!
syms x C
f=C/sind(x);
df1=diff(f,x,1)
经过求解如下:
角度制的三角函数求导时要化成弧度制再进行求导!(国际单位rad!!!)我们再进行一次求导:
d y d x = d ( C s i n d ( x ) ) d x = C s i n ( π ⋅ x / 180 ) d x = − C ⋅ c o s ( π ⋅ x / 180 ) ( s i n ( π ⋅ x / 180 ) ) 2 ⋅ π 180 = − y ⋅ c o s ( π ⋅ x / 180 ) s i n ( π ⋅ x / 180 ) ⋅ π 180 = − y ⋅ c o t ( π ⋅ x 180 ) ⋅ π 180 = − y ⋅ c o t d ( x ) ⋅ π 180 \frac{dy}{dx}=\frac{d(\frac{C}{sind(x)})}{dx}=\frac{\frac{C}{sin(\pi\cdot x/180)}}{dx}=-\frac{C\cdot cos(\pi \cdot x/180)}{(sin(\pi \cdot x/180))^2}\cdot \frac{\pi}{180}=-\frac{y\cdot cos(\pi \cdot x/180)}{sin(\pi \cdot x/180)}\cdot \frac{\pi}{180}=-y\cdot cot(\frac{\pi \cdot x}{180})\cdot \frac{\pi}{180}=-y\cdot cotd(x)\cdot \frac{\pi}{180} dxdy=dxd(sind(x)C)=dxsin(π⋅x/180)C=−(sin(π⋅x/180))2C⋅cos(π⋅x/180)⋅180π=−sin(π⋅x/180)y⋅cos(π⋅x/180)⋅180π=−y⋅cot(180π⋅x)⋅180π=−y⋅cotd(x)⋅180π
可以看出角度制的三角函数求导要乘上π/180 !
修改代码中求导的那一项(第8行)如下:
dif_pm_lambda_theta_cal=pm_lambda_B.*cotd(pm_theta_half)*pi/180;
这样图就对应上了!