Various step size ;the derivation of f(x) = sin(x) calculated using various h values
使用不同的h差值来计算函数f(x)=sin(x)的导(微分)
g = colormap(lines); hold on ;for i =1:4;
x =0:power(10,-i):pi;
y =sin(x);m = diff(y)./diff(x);
plot(x(1:end-1),m,'color',g(i,:));
end
hold off;set(gca,'Xlim',[0, pi/2]);
set(gca,'Ylim',[0,1.2]);
set(gca,'Fontsize',18);
set(gca,'Fontname','symbol');
set(gca,'XTick',0:pi/4:pi/2);
set(gca,'XTicklabel',{'0','\pi/4','\pi/2'});
h = legend('h= 0.1','h= 0.01','h= 0.001','h= 0.0001');
set(h ,'FontName','Times New Roman');box on;
这一串代码画出的不同h值的导数图像,如下:
原因是当前matlab2021a版本不能识别‘symbol’字体。
如果需要显示出坐标轴则只需把倒数第5行删去,后两行坐标写法可以改为如下:
xticks([0:pi/4:pi/2]); %位置指定为向量
xticklabels({'0','\pi/4','\pi/2'});%关联的标签,用cell指定刻度标签
h = legend('h= 0.1','h= 0.01','h= 0.001','h= 0.0001');
set(h ,'FontName','Times New Roman');box on;