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!