mydata = matrix(c(1:6, c(10,15,19,26,32,37), c(11,16,17,28,35,39)),6,3)
mydata
## [,1] [,2] [,3]
## [1,] 1 10 11
## [2,] 2 15 16
## [3,] 3 19 17
## [4,] 4 26 28
## [5,] 5 32 35
## [6,] 6 37 39
plot(mydata[,1], mydata[,2], type = "l", lwd = 8, xlim = c(01,10), ylim = c(0,40))

x1 = seq(-2,2,0.5)
x1
## [1] -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0
y1 = x1^3 - 3*x1
y1
## [1] -2.000 1.125 2.000 1.375 0.000 -1.375 -2.000 -1.125 2.000
x2 = seq(-2,2,0.01)
y2 = x2^3 - 3*x2
plot(x1,y1,type = "l")
lines(x2,y2,col = "red")

x = rnorm(100)
xgrid = seq(-4,4,0.1)
y = dnorm(xgrid)
hist(x,freq = F)
lines(xgrid,y,col="red")
points(xgrid,y,pch=3)

hist(x)

x = seq(-4,4,0.1) # A fine grid between (-4,4)
y = dnorm(x,0,1)
plot(x,y,type = "l")
lines(x,y,col = "red")
points(x,y,pch = 3)

par(mfrow = c(1,2))
plot(xgrid,y)
plot(xgrid,y,type = "l")

par(mfrow = c(2,1))
plot(xgrid,y)
plot(xgrid,y,type = "l")

x1 = seq(-3,3,0.01)
y1 = dnorm(x1)
x2 = seq(-4,4,0.1)
y2 = dnorm(x2)
par(mfrow=c(2,1))
plot(x1,y1,type = "l",col = "red")
plot(x2,y2,type = "l",col = "blue")

y = function(x) {
y = x^2 + 2*x - 3
return(y)
}
y(4)
## [1] 21
y(453)
## [1] 206112
a = function(z) {
a = z^3 + 3*z - 3
return(a)
}
a(4)
## [1] 73
a(453)
## [1] 92961033
y = function(x) {
y = x^2 + 2*x - 3
return(y)
}
a = function(z) {
a = z^3 + 3*z - 3
return(a)
}
par(mfrow = c(a,y))
## [[1]]
## NULL
##
## [[2]]
## NULL
fx = function(x) {
f = x^2 + 2*x - 3
return(f)
}
fx(4)
## [1] 21
fx(400)
## [1] 160797
dice = function(n) {
rolls = sample(1:6, size = n, replace = T)
y = sum(rolls)
print(rolls)
return(y)
}
dice(2)
## [1] 6 5
## [1] 11
dice(20)
## [1] 4 2 3 5 1 4 2 4 4 1 6 2 5 6 3 2 4 2 1 1
## [1] 62
myabs = function(x) {
if (x<0) {
print("The input is negative")
x = -x
}
else {print("The input is >=0")}
}
myabs(-5)
## [1] "The input is negative"
myabs(5)
## [1] "The input is >=0"
roll.num = c(1,2,3,10)
M = length(roll.num)
sum.rolls = rep(0,M)
for (i in 1:M) {
n = roll.num[i]
sum.rolls[i] = dice(n)
}
## [1] 1
## [1] 6 5
## [1] 5 3 6
## [1] 4 5 2 4 5 6 6 1 4 1
for (i in 1:20) {
print(i^3)
}
## [1] 1
## [1] 8
## [1] 27
## [1] 64
## [1] 125
## [1] 216
## [1] 343
## [1] 512
## [1] 729
## [1] 1000
## [1] 1331
## [1] 1728
## [1] 2197
## [1] 2744
## [1] 3375
## [1] 4096
## [1] 4913
## [1] 5832
## [1] 6859
## [1] 8000
x = seq(-6,6,0.1)
M = length(x)
fx.output = rep(0,M)
for (i in 1:M) {
fx.output[i] = fx(x[i])
}
plot(x,fx.output,type = "l")
