一、一維離散數據微分、積分求解
1. 離散積分
function result = integral(x,y,c)
result = [];
for i=1:1:length(y)
c=y(i)*(x(2)-x(1))+c ;
result(end+1) = c;
end
end
2. 離散微分
function result = differential(x,y)
h = x(2)-x(1);
result = [];
for i=1:1:length(y)
if i <= length(y)/2
result(end+1) = (y(i+1)-y(i))/h;
else
result(end+1) = (y(i)-y(i-1))/h;
end
end
end
3. 測試代碼
x = 0:0.001:2;
y = x;
y1 = integral(x,y,1);
plot(x,y);hold on
plot(x,y1);
figure;
y2 = differential(x,y1)
plot(x,y1);hold on
plot(x,y2)
Reference
- matlab離散數據微積分