Reputation: 335
I'm trying to graph this function function(x1,x2,x3,x4){2 * x2 * x4 + x3 ( x1 - 2 * x4)}
using curve()
in R. However, I'm being met with:
Error in f1(x) : argument "x2" is missing, with no default
x1
x2
x3
and x4
are all supposed to have different minimum and maximum values as well, but I'll worry about that after I can just get any graph to show up
Upvotes: 1
Views: 2493
Reputation: 1840
Curve
As I already mentioned in my comment I don't understand why you want to use curve
since it is build to accept a single range of numbers e.g. 1,2,3,4 and then applies a formula to it. This makes plotting straightforward since the input numbers form the x-axis (thus 1,2,3,4...) and the formula results are plotted on the y-axis. In your case you don't have a single input range (actually you have four ranges: x1
, x2
, x3
and x4
) and you cannot plot in 5D thus you have to pick one of x1:x4
to display on the x-axis. Also you have 2 * x2 * x4 + x3 ( x1 - 2 * x4)
as formula however .. x3 ( ..
cannot be directly interpreted by R therefore I added * to make 2 * x2 * x4 + x3 * ( x1 - 2 * x4)
however if you meant that X3 should be multiplied with the numbers within brackets you should edit the formula in the code below.
Alternative
As you mentioned "x1 x2 x3 and x4 are all supposed to have different minimum and maximum values as well" I would suggest to simply write your own plotting function, like so (I now displayed x1
on the x-axis):
range.plot <- function( func, x1.range, x2.range, x3.range, x4.range ) {
# Apply formula
res <- data.frame(x = x1.range, y = func(x1.range, x2.range, x3.range, x4.range))
# Plot using ggplot2
ggplot(res, aes(x, y)) + geom_line()
}
fun <- function(x1,x2,x3,x4){2 * x2 * x4 + x3 * ( x1 - 2 * x4)} # note the added *
range.plot(fun, seq(1,10), seq(10,29), seq(5,14), seq(1,10))
Upvotes: 2
Reputation: 66
Plotting a function curve in R with 2 or more variables
The link above looks similar to your question and might help you figure it out. You might have to add the function curry (in the functional package) to pre-specify parameters. Hope that helps.
Upvotes: 2