diff():计算一个向量相邻元素之间的差异
x = [1 2 5 2 1];
diff(x)
ans =
1 3 -3 -1
给定一个函数f(x)=sin(x),当x0 = pi/2时,使用h=0.1求f(x)微分
x0 = pi/2;
h = 0.1;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)];
m = diff(y)./diff(x)
m =
-0.0500
计算f(x)=sin(x)在[0,2pi]区间上的微分
h = 0.5;
x = 0:0.5:2*pi;
y = sin(x);
m = diff(y)./diff(x)
m =
0.9589 0.7241 0.3120 -0.1764 -0.6217 -0.9147 -0.9838 -0.8120 -0.4415 0.0372 0.5068 0.8522
二阶、三阶段微分求法:
x = -2:0.005:2;
y = x.^3;
m = diff(y)./diff(x);
m2 = diff(m)./diff(x(1:end-1));
plot(x,y,x(1:end-1),m,x(1:end-2),m2);
xlabel('x','FontSize',18);
ylabel('y','FontSize',18);
legend('f(x)=x^3','f''(x)','f''''(x)');
set(gca,'FontSize',18);