## What is the optimal price for Iceskimo?
sales = 100 - 10 * price
profit = sales * (price - 2)

## We could also use the profit function directly
profit_fun = -10*price^2 + 120*price - 200

## Use the find_max function to find the price that maximizes profits
optimal_price = find_max(profit, price)
optimal_price_fun = find_max(profit_fun, price)

## Finding the optimal price for Iceskimo

### Setup the simulation


```{r}
result <- simulater(sequ = "price 0 12;", form = "## What is the optimal price for Iceskimo?;sales = 100 - 10 * price;profit = sales * (price - 2);## We could also use the profit function directly;profit_fun = -10*price^2 + 120*price - 200;## Use the find_max function to find the price that maximizes profits;optimal_price = find_max(profit, price);optimal_price_fun = find_max(profit_fun, price)", seed = 1234, name = "simdat", nr = 1000)
summary(result, dec = 2)
```

As you can see from the simulation results, the optimal price is \$6.00

### Visualize the results

```{r fig.width=7, fig.height=7}
visualize(dataset = "simdat", xvar = "price", yvar = "profit", type = "line")
```

You would get the same result if you use the profit function directly

```{r fig.width=7, fig.height=7}
visualize(dataset = "simdat", xvar = "price", yvar = "profit_fun", type = "line")
```

## Finding the optimal price for Iceskimo

### Setup the simulation

```{r}
simdat <- simulater(
  sequ = "price 0 12", 
  form = c(
    "## What is the optimal price for Iceskimo?", 
    "sales = 100 - 10 * price", 
    "profit = sales * (price - 2)", 
    "## We could also use the profit function directly", 
    "profit_fun = -10*price^2 + 120*price - 200", 
    "## Use the find_max function to find the price that maximizes profits", 
    "optimal_price = find_max(profit, price)", 
    "optimal_price_fun = find_max(profit_fun, price)"
  ), 
  seed = 1234, 
  nr = 1300
)
summary(simdat, dec = 2)
register("simdat")
```

As you can see from the simulation results, the optimal price is \$6.00

### Visualize the results

```{r fig.width = 6.46, fig.height = 6.46, dpi = 200}
visualize(
  simdat, 
  xvar = "price", 
  yvar = "profit", 
  type = "line", 
  custom = FALSE
)
```

You would get the same result if you use the profit function directly

```{r fig.width = 6.46, fig.height = 6.46, dpi = 200}
visualize(
  simdat, 
  xvar = "price", 
  yvar = "profit_fun", 
  type = "line", 
  custom = FALSE
)
```