2017-11-18 99 views
1

我试图用R绘图来获取某些函数的曲线。有时候,我得到了很奇怪的结果。下面是一个例子R绘图函数给出了奇怪的答案

u=c(2,2,2,2,2) 
elas=function(P){ 
    prob=P/sum(u,P) 
    return(prob) 
} 
plot(elas,0,6) 

该代码给出了这样的情节: enter image description here

这显然是不对的。正确的代码应该是这样的: enter image description here

我知道,如果我改变代码的第3行是

prob=P/(sum(u)+P) 

它的工作。但我不明白为什么我原来的代码不起作用。这是否意味着我不能绘制嵌入另一个函数的函数?

回答

1

sum(u,P)是一个等于uP中所有值之和的单个值。因此,在elas中,P get的每个值除以相同的数字(在您的示例中为313)。

sum(u) + P是包含Psum(u)的每个单独值的向量添加到它。因此,在elas(我在下面称为elas2)的第二个版本中,P/(sum(u) + P)导致Psum(u) + P逐元素划分。

考虑下面的例子。

u=c(2,2,2,2,2) 
x=seq(0,6,length=101) 

sum(u,x) 
[1] 313 
sum(u) + x 
[1] 10.00 10.06 10.12 10.18 10.24 10.30 10.36 10.42 10.48 10.54 10.60 10.66 10.72 10.78 
[15] 10.84 10.90 10.96 11.02 11.08 11.14 11.20 11.26 11.32 11.38 11.44 11.50 11.56 11.62 
[29] 11.68 11.74 11.80 11.86 11.92 11.98 12.04 12.10 12.16 12.22 12.28 12.34 12.40 12.46 
[43] 12.52 12.58 12.64 12.70 12.76 12.82 12.88 12.94 13.00 13.06 13.12 13.18 13.24 13.30 
[57] 13.36 13.42 13.48 13.54 13.60 13.66 13.72 13.78 13.84 13.90 13.96 14.02 14.08 14.14 
[71] 14.20 14.26 14.32 14.38 14.44 14.50 14.56 14.62 14.68 14.74 14.80 14.86 14.92 14.98 
[85] 15.04 15.10 15.16 15.22 15.28 15.34 15.40 15.46 15.52 15.58 15.64 15.70 15.76 15.82 
[99] 15.88 15.94 16.00 
par(mfrow=c(1,3)) 

elas=function(P) { 
    P/sum(u,P) 
} 

dat = data.frame(x, y=elas(x), y_calc=x/sum(u,x)) 

plot(dat$x, dat$y, type="l", lwd=2, ylim=c(0,0.020)) 
plot(elas, 0, 6, lwd=2, ylim=c(0,0.020)) 
curve(elas, 0, 6, lwd=2, ylim=c(0,0.020)) 

enter image description here

dat$y - dat$y_calc 
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
[43] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
[85] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
elas2 = function(P) { 
    P/(sum(u) + P) 
} 

dat$y2 = elas2(x) 

plot(dat$x, dat$y2, type="l", lwd=2, ylim=c(0,0.4)) 
plot(elas2, 0, 6, lwd=2, ylim=c(0,0.4)) 
curve(elas2, 0, 6, lwd=2, ylim=c(0,0.4)) 

enter image description here

+0

如果正确地明白,如果我绘制函数,而不是一个数据帧,R仍然将模拟的“x”的第一,然后把所有的值在x向量中的函数来计算函数的值并将其存储为y。然后用x和y形成一个数据框,然后绘制数据框。 – AYY

+0

但是,如果我有一些像sum(x)这样的函数,它会将所有x的值相加并使用该值,而不是每个x来计算函数值。但是,如果我真的需要在那里有一些函数,如sum,并且希望在计算y和绘图之前将每个x与u相加,我该怎么办?在这个简单的例子中,我可以只写sum(u)+ x,但是当涉及到更复杂的函数时,我不知道如何处理它。 – AYY

+0

是的。当你给'plot'一个函数(比如'elas')时,'plot'调用'curve'函数。您可以在控制台中输入'curve'来查看'curve'的代码。查看该行的函数代码:'x < - seq.int(from,to,length.out = n)'。这就是'curve'生成它要绘制的x值的地方,也可以用来生成y值。 – eipi10

1

总和(U + P)的每个值的在U =总和加上P

总和(U)+ P =在u值的总和加上P.

实施例:u = c(1,2,3), P = 5

sum(u+P) = (1+5) + (2+5) + (3+5) = 6+7+8 = 21 
sum(u) + P = (1+2+3) + 5 = 6 + 5 = 11 

对于

elas <- function(P){ 
    prob=P/(sum(u+P) 
    return(prob) 
} 

u <- c(2,2,2,2,2)

y <- elas(0:6) 
print(y) 
#output of print y: 
0.00000000 0.03225806 0.06451613 0.09677419 0.12903226 0.16129032 
0.19354839 
plot(0:6,y) 

enter image description here

对于

elas <- function(P){ 
    prob=P/(sum(u) + P) 
    return(prob) 
} 
y <- elas(0:6) 
print(y) 
#Output of print(y) 
plot(0:6,y) 
0.00000000 0.09090909 0.16666667 0.23076923 0.28571429 0.33333333 0.37500000 

enter image description here