这是计算 Scratch 中积分从一个点到第二个点的面积的可靠方法吗?

Would this be a reliable way of calculating the area from a point to a second point of an integral in Scratch?

提问人:CrSb0001 提问时间:10/3/2023 更新时间:10/3/2023 访问量:31

问:

因此,为了好玩,我做了一个编码练习(在 Scratch 中),并决定尝试创建一段代码,该代码能够计算 ∫ab f(x) dx 形式的函数 f(x)=x y其中 y 是某个常数)的面积,我认为我可能能够做到。


我对此所做的尝试始于知道我有有助于解决这个问题的代码:

[scratchblocks]
define pow(base)(power)
set [result v] to [1]
set [base v] to (base)
set [power v] to (round (power))
set [done v] to [0]
if <(power) < [0]> then
set [base v] to [((1) / (base))]
set [power v] to [-1(() *(round (power))]
end
repeat until <[done v] = [1]>
if <(([power v]) mod (2)) = [0]> then
set [result v] to (([result v]) * ([base v]))
change [power v] by (-1)
end
if <(([power v]) mod (2)) > [0]> then
set [done v] to [1]
end
if <<not <<[done v] = [1]>>>
set [base v] to (([base v]) * ([base v]))
set [power v] to (([power v]) / (2))
end
end
[/scratchblocks]

enter image description here

我还必须创建一些新代码来实际计算积分,但这并不难:

[scratchblocks]
define area from x to y of the form a^zda(x)(y)(z)
set [x v] to (x)
set [y v] to (y)
set [z v] to (z)
pow (x) ((z)+(1))
set [re2 v] to (result)
pow (y) ((z)+(1))
set [re3 v] to (result)
set [re4 v] to (((re3)-(re2))/(3))
[/scratchblocks]

enter image description here

现在,为了测试,我做了 ∫34 a 2da,这应该得到我们 (4^3)/3-9=64/3-9=21+(1/3)-9=12+(1/3)≈12.333enter image description here

它似乎已经做到了。


我的问题


这是计算 ∫a b x^adx Scratch 形式的积分的一个点到第二个点的面积的可靠方法,还是我能做些什么来使其更可靠地做到这一点?

限制 mit-scratch

评论

0赞 CrSb0001 10/3/2023
如果您想查看该项目,这里有一个指向该项目的链接:project
1赞 Ruud Helderman 10/3/2023
编写可靠代码的三个重要规则:测试、测试、测试。更好的是:编写自动化测试。你的代码有一个错误,如果你对你的代码进行认真的测试,你就会注意到这个错误。

答: 暂无答案