How to vary parameters of an equation?

3

For an equation of type y = ax + b , we have two values for a and two for b , that is, we have four different equations. We already have a code that returns the values of x and y for each equation separately. We want to know if there is a way to loop return the respective values of each x and y when we change the values of "a" and "b". That is, how to find y1, y2, y3, y4, x1, x2, x3 e x4 (for each combination of a and b) without having to run the code four times.

obs: the program used is R

    
asked by anonymous 23.03.2014 / 20:29

1 answer

2

Suppose you have two parameters a , two parameters b , and a value of x :

a<- c(1,2)
b <-c(3,4)
x <- 10

You want to find all y possible with the four parameter combinations, given the value of x . So the first thing to do is to generate these combinations:

parametros <- merge(a, b, by=NULL)
names(parametros) <-c("a", "b")
parametros
  a b
1 1 3
2 2 3
3 1 4
4 2 4

Now we define the function that generates the value of y and we can use mapply to find all the y values of the parameter combinations:

y<-mapply(function(x,a,b) y=a*x +b, x=x, a=parametros[1,], b=parametros[,2])
y
[1] 13 33 14 34
    
23.03.2014 / 21:01