Using diff() in a for loop
이전 댓글 표시
I'm trying to plot the difference between two values against an integer, but I get the error "Difference order N must be a positive integer scalar" when using diff. I currently have a function to estimate the volume of a sphere, and another to compare it to the real volume and plot it. Below is my code:
.m
function volume = approximate(b)
for k = 0:b
sum = ((-1).^k) / (2 * k + 1);
end
radius = 46.8;
volume = 4/3 * (4 * sum) * radius.^3;
end
difference.m
b = 0:1:10;
radius = 46.8;
trueValue = (4/3) * pi * radius.^3;
for i = 1:length(b)
difference = diff(approximate(i), trueValue);
end
plot(b, difference);
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Volume Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!