Reputation: 1522
It's a basic question and sure, there are a lot of examples in google.. but I just do not understand this small bunch of code..
V <- seq(50, 350, by = 1)
> VK
Voltage^0 Voltage^1 Voltage^2 Voltage^3
-1.014021e+01 9.319875e-02 -2.738749e-04 2.923875e-07
plot(x = V, exp(exp(sapply(0:3, function(x) V^x) %*% VK)), type = "l"); grid()
I tried to get behind this after playing a lot with the function itself but.. I cannot apply my ideas to this certain line. As far as I got I believe I can tell: sapply is a function that applies the body for each element of a vector or list or something similar. In this case this is V. The point that confuses me is the "0:3" part (which seems to be amount of elements of VK) and the end of the function %*% VK. When I do the very same on my own with different numbers than VK is summed up and then used as a coefficient for exp(exp(V^x)). But here in this case this makes no sense. Furthermore: By googling I always read that sapply yields a vector. Due to the fact that the code above generates a plot this is a 2D-vector as result?
Upvotes: 0
Views: 118
Reputation: 17369
Just for illustration, here is what the code looks like when translated into a for
loop.
V <- seq(50, 350, by = 1)
VK <- c("Voltage^0" = -1.014021e+01,
"Voltage^1" = 9.319875e-02,
"Voltage^2" = -2.738749e-04,
"Voltage^3" = 2.923875e-07)
coef <- matrix(0, ncol = 4, nrow = length(V))
for(x in 0:3){
coef[, x + 1] <- V^x
}
Y <- exp(exp(coef %*% VK))
plot(V, Y, type = "l"); grid()
It works because sapply
is an over achiever that tries not to fail by returning a matrix if it can't return a vector.
Upvotes: 1
Reputation: 362
sapply(0:3, function(x) V^x)
> sapply(0:3, function(x) V^x)
[,1] [,2] [,3] [,4]
[1,] 1 50 2500 125000
[2,] 1 51 2601 132651
[3,] 1 52 2704 140608
As stated in the comments, sapply generates a matrix (301x4
) where each column represents V^0
, V^1
,V^2
, and V^3
. Then, it is multiplied by VK (4x1
). This will generate a vector (301x1
).
> sapply(0:3, function(x) V^x) %*% VK
[,1]
[1,] -6.128411312
[2,] -6.060636871
[3,] -5.993320708
After these steps, you apply the exponential function twice to your new vector. This new vector contains your y-values for your plot.
If you wanted to apply "(..sapply(0:2,..)" instead of "(..sapply(0:3,..)", then change VK to this:
VK <- c(-1.014021e+01, 9.319875e-02, -2.738749e-04)
names(VK) <- c( "Voltage^0", "Voltage^1", "Voltage^2")
Upvotes: 1
Reputation: 7164
You are doing multiple things in one line. I guess you are getting lost in the different parts on 1 line where like 5 things happen at the same time.
plot(x = V, exp(exp(sapply(0:3, function(x) V^x) %*% VK)), type = "l"); grid()
First off, the main part is plot(x, y, type = "l")
. This just plots a vector x
against a vector y
. In your case, y
is exp(exp(sapply(0:3, function(x) V^x) %*% VK))
.
An easier and prettier way to have written the code, would've been:
inside_of_y <- sapply(0:3, function(x){ V^x %*% VK })
y_variable <- exp(exp(inside_of_y))
plot(x = V, y = y_variable, type = "l")
grid()
The first line is probably still messing with you a bit. In essence, sapply
is just a for
-loop. It is built up of two ingredients: part1
and part2
.
part1
is an object with multiple elements, like a data.frame or a vector (here: part1
is equal to c(0, 1, 2, 3)
). The elements inside this object are used as arguments for part2
. In this particular case, you defined a custom function as part2
. In this custom function, you take V
to the power of x
and multiply the result with the matrix VK
. The tricky part is that x
is equal to 0
in the first iteration, 1
in the second iteration, ..., and 3
in the last one. You just looped through 0:3
, which is part1
of your sapply(part1, part2)
.
So inside_of_y
now contains 4 elements. Then we just proceed with plotting x
against y
with the plot(x, y)
function.
Upvotes: 1