开发者

Graphing equations involving absolute values

开发者 https://www.devze.com 2023-03-18 23:41 出处:网络
How would you draw in R the graph of the equation y| = 开发者_如何转开发1 - |x| fun1 <- function(x) 1-abs(x)

How would you draw in R the graph of the equation

|y| = 开发者_如何转开发1 - |x|


fun1 <- function(x) 1-abs(x)
fun2 <- function(x) -(1-abs(x))
plot (fun1, -1, 1,xlim=c(-2,2),ylim=c(-2,2),pty="s",col=2);abline(h=0,v=0)
plot (fun2, -1, 1,col=2,add=TRUE)


Here's an approach using ggplot.

x = seq(-1, 1, by = 0.01)
y = 1 - abs(x)
p1 = qplot(x, geom = 'blank') + 
     geom_point(aes(y = y), colour = 'blue') + 
     geom_point(aes(y = -y), colour = 'red')

Graphing equations involving absolute values


x <- (-1):1
y <- 1 - abs(x)
plot(x, y, type="l")
lines(x, -y)
0

精彩评论

暂无评论...
验证码 换一张
取 消