I posted similar question a few days later, but now I have more complex problem. I have this dataset
product = data.frame(length = c(100, 200, 300, 400), qty = c(1, 2, 1, 3))
And price is defined by this equation (product[["length"]] * old_const * 2) + setup_price
where old_const = 0.0158
and setup_price = 20.8
product[["old_price"]] = (product[["length"]] * old_const * 2) + setup_price
Now I would like to change constant setup_price
to meet two requirements:
Total revenue with new prices is same as revenue with old prices AND Total revenue from first half of table is same as revenue from second half of table
y1 = sum((product[["length"]] * old_const * 2 + setup_price)* product[["qty"]])
y2 = sum((product[["length"]] * 2)* product[["qty"]])
y3 = sum((product[1:table_median, ][["length"]] * 2)* product[1:table_median, ][["qty"]])
y4 = sum((product[table_median:max(nrow(product)), ][["length"]] * 2) * product[table_median:max(nrow(product)), ][["qty"]])
where table_median = round(median(1:nrow(product)))
And my optimalization
const = seq(-2,2, by = 0.0001)
ev = NULL
for (i in 1:length(const)){
ev[i] = (y1 - y2 * const[i]) + (y3*const[i] - y4)
}
plot(const, ev)
And I would like to get ev = 0
nad I dont know where exactly my 0 is.
There was recomandation to use uniroot
function which I know how to use with one equation.
I had to change belt_length to length to make sense of what you shared. I've gone ahead and simply refactored your for loop to involve a function, and then use that same function in uniroot
const = seq(-2,2, by = 0.0001)
ev = NULL
custfunc <- function(x){
(y1 - y2 * x) + (y3*x - y4)
}
for (i in 1:length(const)){
ev[i] = custfunc(const[i])
}
plot(const, ev)
uniroot(custfunc,interval = c(-2,2))