r - Sapply function confuses me -


it's basic question , sure, there lot of examples in google.. not understand small bunch of code..

v <- seq(50, 350, = 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 behind after playing lot function but.. cannot apply ideas line. far got believe can tell: sapply function applies body each element of vector or list or similar. in case v. point confuses me "0:3" part (which seems amount of elements of vk) , end of function %*% vk. when same on own different numbers vk summed , used coefficient exp(exp(v^x)). here in case makes no sense. furthermore: googling read sapply yields vector. due fact code above generates plot 2d-vector result?

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 comments, sapply generates matrix (301x4) each column represents v^0, v^1,v^2, , v^3. then, multiplied vk (4x1). generate vector (301x1).

> sapply(0:3, function(x) v^x) %*% vk                [,1]   [1,] -6.128411312   [2,] -6.060636871   [3,] -5.993320708 

after these steps, apply exponential function twice new vector. new vector contains y-values plot.

if wanted apply "(..sapply(0:2,..)" instead of "(..sapply(0:3,..)", change vk this:

vk <- c(-1.014021e+01,  9.319875e-02, -2.738749e-04) names(vk) <- c( "voltage^0",     "voltage^1",     "voltage^2") 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -