问题描述
我需要在循环中添加 N 像 (n=0 n++) 直到它停止到 n=1000 我得到了 A 的所有循环结果
def f(t):
return(3*t**2+2)
N=1000
x1=1
x2=2
dx=(x2-x1)/N
A=0
x=x1
while x<=x2:
dA=f(x)*dx
A=A+dA
x=x+dx
print("A = ",A)
解决方法
尝试 for 循环:
ggplot(data = ny_dataframe_scratch,aes(x=timepoint,y = relative_wound_healing,fill = Condition)) +
geom_col(width = 5,position = position_dodge()) +
scale_x_continuous(breaks=c(0,6,18,24,32,44),name = "Time point,hours") +
scale_y_continuous(name = "Relative scratch area") +
scale_fill_manual(values=c("palevioletred4","slategray")) +
geom_point(data = ny_dataframe_scratch,position = position_dodge(width = 5),y=relative_wound_healing,fill = Condition))
This is the output of dput():
structure(list(timepoint = c(0,44,Condition = structure(c(2L,2L,1L,1L),.Label = c("Control","Knockout"),class = "factor"),relative_wound_healing = c(1,1,0.819981,0.78227,0.811902,0.873852,0.893572,0.910596,0.39819,0.436948,0.559486,0.534719,0.591295,0.612154,0.222731,0.2592,0.453575,0.37238,0.477891,0.505393,0.05243246,0.0809449,0.2108063,0.261122,0.3750218,0.4129873,0.0240122,0.0778219,0.0806758,0.2495444,0.3203724)),class = "data.frame",row.names = c(NA,-36L))
,
使用 range(N)
和其他一些更改来获得更准确的结果:
from statistics import mean
def f(t):
return(3 * t**2 + 2)
N = 1000
x1 = 1
x2 = 2
Dx = x2 - x1
A = mean(f(x)
for i in range(N)
for x in [x1 + Dx * (i+0.5) / N]) * Dx
print("A = ",A)
它打印 8.99999975
,而你的打印 9.009500499999444
。数学上正确的结果是 [t^3+2t]_1^2 = (2^3+2*2)-(1^3+2*1) = 12-3 = 9。