Вопрос

I would like to calculate roots for different y-values of a quadratic equation. But when I run the following code get an error I do not understand. If y is just one numeric value, instead of the vector c(0.6,0.4,0.9), it works. I am using sapply wrongly or is it the passing of variables to the function that are wrong?

# Paramters for quadratic poly
a<-0.875
b<-0.3779
c<-0.098
y<- c(0.6,0.4,0.9)

# quadratic function
fun<-function(x) c-y+b*x+a*x^2

# Finding root in specific interval function
root<- function (x) uniroot(fun,c(0,2))$root

# Finding roots for a list of y values
res<-sapply(y,root)
Это было полезно?

Решение

You can do this for example :

 sapply(y,function(y) uniroot(fun,c(0,2),y=y,c=c,a=a,b=b))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top