2017-11-18 102 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