# Exercise 2
* Due: 21.11. at noon
* Max points: 8

## General rules
* Send the source code or the written notes of your answers as an attachment to markus.hartikainen@jyu.fi before the due time
* You will get feedback about your answers a week from the due date
* From the second week on, the exercises will be given on the previous Wednesday's lecture

## Exercises

1. **(2 points)** Assume that that you are buying and selling electricity. The more electricity you buy, the better price you get per kwH and the buying prize of water follows function $f(x) = 1-0.01x$, where $0\leq x\leq50$ is the amount of electricity you buy. On the other hand, the price that you get from selling electricity follows function $g(x) = 2-0.01x^2$ with $x$ again the amount of electricity that you sell. Formulate an optimization problem that maximizes the profit.
2. **(2 points)** Use bisection search to optimize problem
$$
\begin{align}
\min & (1-x)^2+x\\
\text{s.t. }&x\in [0,2].
\end{align}
$$
3. **(2 points)** Use golden section search to optimize the problem from above exercise.
4. **(2 points)** Use differentiation to optimize the optimization problem of exercise 1 and to verify the answer of optimization problem 2.


# Solutions


```python
#solutiion 1
#formulate optimization problem
#formulate profit= selling price - buying price
#profit (h(x))=g(x)-f(x)=2-0.01x**2-(1-0.01x) ->1+0.01x-0.01x**2
max (h(x)=1+0.01x-0.01x**2)
s.t x ∈[0,50]. 


```


```python
#solution 2
#solve the given equation with bisection_line_search method

def f(x):
    return (1-x+pow(x,2))

def bisection_line_search(a,b,f,L,epsilon):
    x = a
    y = b
    while y-x>2*L:
        if f((x+y)/2-epsilon)<f((x+y)/2+epsilon):
            y = (x+y)/2+epsilon
        else:
            x = (x+y)/2-epsilon
    return (x+y)/2

bisection_line_search(0.0,2.0,f,1e-3,1e-4)
```




    0.499561767578125




```python
#solution 3
#solve the above problem with golden_section_line_search method
import math

def f(x):
    return (1-x+pow(x,2))

def golden_section_line_search(a,b,f,L):
    x = a
    y = b
    while y-x>2*L:
        if f(x+(math.sqrt(5.0)-1)/2.0*(y-x))<f(y-(math.sqrt(5.0)-1)/2.0*(y-x)):
            x = y-(math.sqrt(5.0)-1)/2.0*(y-x)
        else:
            y = x+(math.sqrt(5.0)-1)/2.0*(y-x)
    return (x+y)/2

golden_section_line_search(0.0,2.0,f,1e-3)
```




    0.5000865351358561




```python
#solution 4
#solve problem #1 with differentiation approach and verify the answer of problem #2

## verfiying the answer for problem #2
   #max or min value of a function can be calcuated by equating it's first derivative to zero.
    #thus the first derivative of 
    f(x)=1-x+pow(x,2) is f'(x)=2x-1
    #then to solve for x we equate 
    f'(x)=0 -> 2x-1=0 ->2x=1 -> x=0.5
    #verifying that the answers by both methods (bisection_line_search and golden_section_line_search )
    #give colser guesses  
```


```python
#solution 4 
#solve problem #1 with differentiation approach 
#given profit equation
h(x)=1+0.01x-0.01x**2 and 0<= x <=50
#we can find max of it when we equate h'(x)=0
#h'(x)=0.01-2*001x=0 ->x=1/2=0.5


```

660ad9ab6a0eac146da9cbc27d58cdf9e0ba6d81
ipynb
Jupyter Notebook
Exercise2_solutions.ipynb
AwelEshetu/edxCourses
2fb072d8e45d203080d40ca383de14257a1fcbd2
CC-BY-3.0
2016-07-18T12:49:18.000Z
2017-02-21T12:58:16.000Z
Exercise2_solutions.ipynb
AwelEshetu/edxCourses
2fb072d8e45d203080d40ca383de14257a1fcbd2
CC-BY-3.0
Exercise2_solutions.ipynb
AwelEshetu/edxCourses
2fb072d8e45d203080d40ca383de14257a1fcbd2
CC-BY-3.0
Qwen/Qwen-72B
1. YES
2. YES