### Computing for Mathematics - Example individual coursework

This jupyter notebook contains questions that will resemble the questions in your individual coursework.

**Important** Do not delete the cells containing: 

```
### BEGIN SOLUTION

### END SOLUTION
```

write your solution attempts in those cells.

Note that this notebook also includes some cells that check your answers.

#### Question 1

For each of the following functions **output** \\(\frac{dy}{dx}\\).

a. \\(y= 2 x (x - 3) (x - 5)\\)


```python
### BEGIN SOLUTION
import sympy as sym
x = sym.Symbol("x")
y = 2 * x * (x - 3) * (x - 5)
sym.diff(y, x)
### END SOLUTION
```




$\displaystyle 2 x \left(x - 5\right) + 2 x \left(x - 3\right) + \left(x - 3\right) \left(2 x - 10\right)$




```python
q1_a_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q1_a_answer.is_algebraic_expr(), feedback_text
except AttributeError:
    print(feedback_text)
```


```python
x = sym.Symbol("x")
expected_answer = 2 * x * (x - 5) + 2 * x * (x - 3) + (x - 3) * (2 * x - 10)
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.simplify(q1_a_answer - expected_answer) == 0, feedback_text
```

b. y =\\(\frac{3x ^ 3 + 6 \sqrt{x} + 3) }{ (3  x ^{(1 / 4)})}\\)


```python
### BEGIN SOLUTION
y = (3 * x ** 3 + 6 * sym.sqrt(x) + 3) / (3 * x ** (sym.S(1) / 4))
sym.diff(y, x)
### END SOLUTION
```




$\displaystyle \frac{9 x^{2} + \frac{3}{\sqrt{x}}}{3 \sqrt[4]{x}} - \frac{6 \sqrt{x} + 3 x^{3} + 3}{12 x^{\frac{5}{4}}}$




```python
q1_b_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q1_b_answer.is_algebraic_expr(), feedback_text
except AttributeError:
    print(feedback_text)
```


```python
x = sym.Symbol("x")
expected_answer = sym.diff((3 * x ** 3 + 6 * sym.sqrt(x) + 3) / (3 * x ** (sym.S(1) / 4)), x)
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.simplify(q1_b_answer - expected_answer) == 0, feedback_text
```

3. \\(y=2 x (x - 3) (x - 5)\\)


```python
### BEGIN SOLUTION
y = 2 * x * (x - 3) * (x - 5)
sym.diff(y, x)
### END SOLUTION
```




$\displaystyle 2 x \left(x - 5\right) + 2 x \left(x - 3\right) + \left(x - 3\right) \left(2 x - 10\right)$




```python
q1_c_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q1_c_answer.is_algebraic_expr(), feedback_text
except AttributeError:
    print(feedback_text)
```


```python
x = sym.Symbol("x")
expected_answer = sym.diff(2 * x * (x - 3) * (x - 5), x)
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.simplify(q1_c_answer - expected_answer) == 0, feedback_text
```

### Question 2

Consider the functions \\(f(x)=-x^3+2x^2+3x\\) and \\(g(x)=-x^3+3x^2-x+3\\).

a. Create a variable `turning_points` which has value the turning points of \\(f(x)\\).


```python
### BEGIN SOLUTION
f = - x ** 3 + 2 * x ** 2 + 3 * x
f_dash = sym.diff(f, x)
turning_points = sym.solveset(f_dash, x)
### END SOLUTION
```


```python
feedback_text = """
Your output is not a sympy Finite Set which is what is expected here.
"""
try:
    assert type(turning_points) is sym.FiniteSet
except NameError:
    print("You did not create a variable called `turning_points`")
```


```python
expected_answer = sym.solveset(- 3 * x ** 2 + 4 * x + 3, x)
feedback_text = f"""Your answer is not correct.

The expected answer is obtained by equating the derivative of f to 0 which gives:

{expected_answer}
"""
assert set(turning_points) == set(expected_answer), feedback_text
```

b. Create a variable `intersection_points` which has value the points where \\(f(x)\\) and \\(g(x)\\) intersect.


```python
### BEGIN SOLUTION
g = - x ** 3 + 3 * x ** 2 - x + 3
intersection_points = sym.solveset(sym.Eq(f, g), x)
### END SOLUTION
```


```python
feedback_text = """
Your output is not a sympy Finite Set which is what is expected here.
"""
try:
    assert type(intersection_points) is sym.FiniteSet
except NameError:
    print("You did not create a variable called `turning_points`")
```


```python
expected_answer = sym.FiniteSet(1, 3)
feedback_text = f"""Your answer is not correct.

The expected answer is obtained by equating the f and g:

{expected_answer}
"""
assert set(intersection_points) == set(expected_answer), feedback_text
```

c. Using your answers to parts b., calculate the area of the region between \\(f\\) and \\(g\\). Assign this value to a variable `area_of_shaded_region`.


```python
### BEGIN SOLUTION
area_of_shaded_region = abs(sym.integrate(f, (x, 1, 3)) - sym.integrate(g, (x, 1, 3)))
area_of_shaded_region
### END SOLUTION
```




$\displaystyle \frac{4}{3}$




```python
feedback_text = "Your output is not a sympy rational which is expected here"
try:
    assert type(area_of_shaded_region) is sym.Rational, feedback_text
except NameError:
    print("You did not create a variable called `area_of_shaded_region`")
```


```python
expected_answer = sym.S(4) / 3
feedback_text = f"""The expected answer is {expected_answer} which is obtained by 
calculating the definite integral of |f - g| between 1 and 3.

"""
assert float(area_of_shaded_region) == float(expected_answer), expected_answer 
```

### Question 3

Three letters are selected at random from the 8 letters of the word `MONGOLIA`, without regard to order.

a. Create a variable `number_of_selections` with value the number of possible selections of 4 letters.


```python
import itertools
### BEGIN SOLUTION
letters = "MONGOLIA"
words = list(itertools.combinations(letters, 4))
number_of_selections = len(words)
### END SOLUTION
```


```python
expected_answer = 70
feedback_text = f"""The expected number of selections is {expected_answer}.

This is obtained using the `itertools.combinations` function."""
try:
    assert number_of_selections == expected_answer, feedback_text
except NameError:
    print("You did not create a variable `number_of_selections`")
```

b. Create a variable `probability_of_selecting_N` with value the probability that the letter `N` is included in the selection.


```python
### BEGIN SOLUTION
probability_of_selecting_N = sum('N' in word for word in words) / len(words)
### END SOLUTION
```


```python
expected_answer = 35 / 70
feedback_text = f"""The expected probability is {expected_answer}.

This is obtained using a conditional summation over the generated words."""
try:
    assert float(probability_of_selecting_N) == float(expected_answer), feedback_text
except NameError:
    print("You did not create a variable `probability_of_selecting_P`")
```

c. letters are now selected at random, one at a time, from the 8 letters of the word `MONGOLIA`, and are placed in order in a line. Create a variable `probability_of_selecting_GOAL` with value the probability that the 4 letters can form the word `GOAL`.


```python
### BEGIN SOLUTION
words = list(itertools.permutations(letters, 4))
probability_of_selecting_GOAL = sum((["A", "G", "L", "O"] == sorted(word)) for word in words) / len(words)
### END SOLUTION
```


```python
expected_answer = 48 / 1680
feedback_text = f"""The expected probability is {expected_answer}.

This is obtained using a conditional summation over the words generated using `itertools.permutations`."""
try:
    assert np.isclose(probability_of_selecting_GOAL, expected_answer), feedback_text
except NameError:
    print("You did not create a variable `probability_of_selecting_TOP`")
```

    You did not create a variable `probability_of_selecting_TOP`


### Question 4

A sequence is given by:

\\[
    x_1 = 1\\
    x_{n + 1}= x_n(p + x_n)
\\]

for \\(p\ne0\\).

a. Define a python function `generate_x(n, p)` that returns the terms of that sequence.


```python
def generate_x(n, p):
    ### BEGIN SOLUTION
    """
    Gives the nth term of the sequence for a given value of p
    """
    if n == 1:
        return 1
    previous_x = generate_x(n - 1, p)
    return previous_x * (p + previous_x)
    ### END SOLUTION
```


```python
feedback_text = """You did not include a docstring. This is important to help document your code.


It is done  using triple quotation marks. For example:

def get_remainder(m, n):
    \"\"\"
    This function returns the remainder of m when dividing by n
    \"\"\"
    ...

Using that it's possible to access the docstring,
one way to do this is to type: `get_remainder?`
(which only works in Jupyter) or help(get_remainder).

We can also comment code using `#` but this is completely
ignored by Python so cannot be accessed in the same way.

"""
try:
    assert  generate_x.__doc__ is not None, feedback_text
except NameError:
    print("You did not create a variable called `area_of_shaded_region`")
```


```python
try:
    assert generate_x(n=1, p=2) == 1, f"Your function did not give the expected answer for n=1, p=2"
    assert generate_x(n=1, p=1) == 1, f"Your function did not give the expected answer for n=1, p=2"
    assert generate_x(n=5, p=1) == 1806, f"Your function did not give the expected answer for n=5, p=1"
    assert generate_x(n=5, p=2) == 65535, f"Your function did not give the expected answer for n=5, p=2"
except NameError:
    print("You did not create a variable called `area_of_shaded_region`")
```

b. **Output** an expression for \\(x_2\\) in terms of \\(p\\).


```python
### BEGIN SOLUTION
p = sym.Symbol("p")
generate_x(n=2, p=p)
### END SOLUTION
```




$\displaystyle p + 1$




```python
q4_a_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q4_a_answer.is_algebraic_expr(), feedback_text
except AttributeError:
    print(feedback_text)
```


```python
p = sym.Symbol("p")
expected_answer = p + 1
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.simplify(q4_a_answer - expected_answer) == 0, feedback_text
```

b. **Output** an expression for \\(x_3\\) in terms of \\(p\\)


```python
### BEGIN SOLUTION
generate_x(3, p=p)
### END SOLUTION
```




$\displaystyle \left(p + 1\right) \left(2 p + 1\right)$




```python
q4_b_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q4_b_answer.is_algebraic_expr(), feedback_text
except AttributeError:
    print(feedback_text)
```


```python
p = sym.Symbol("p")
expected_answer = 2 * p ** 2 + 3 * p + 1
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.simplify(q4_b_answer - expected_answer) == 0, feedback_text
```

c. **Output** the values of \\(p\\) for which \\(x_3=1\\).


```python
### BEGIN SOLUTION
sym.solveset(sym.Eq(generate_x(3, p=p), 1), p)
### END SOLUTION
```




$\displaystyle \left\{- \frac{3}{2}, 0\right\}$




```python
q4_c_answer = _
feedback_text = """
Your output is not a sympy Finite Set which is what is expected here.
"""
assert type(q4_c_answer) is sym.FiniteSet, feedback_text
```

d. Using the non zero value of \\(p\\) calculated in the previous question calculate \\(x_n\\) for \\(n\in\{1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300\}\\) and create a variable `range_of_values` with minimum and maximum value or \\(x_n\\).


```python
values_of_n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300]
### BEGIN SOLUTION
values = [generate_x(n=n, p=-sym.S(3) / 2) for n in values_of_n]
range_of_values = min(values), max(values)
### END SOLUTION
```


```python
import numpy as np
expected_answer = (- sym.S(1) / 2, 1)
expected_answer = np.array(sorted(expected_answer), dtype=np.float64)
range_of_values = np.array(sorted(range_of_values), dtype=np.float64)
feedback_text = f"""Your answer is not correct, the expected answer is:

{expected_answer}
"""
try:
    assert np.allclose(expected_answer, range_of_values), feedback_text
except NameError:
    print("You did not create a variable called `all_values_of_n`")
```

f9594e1c24da1d3aacfbf23cff3eafc82887f967
ipynb
Jupyter Notebook
assets/assessment/example/main.ipynb
drvinceknight/cfm
06977f5c1ba37590b17a8d2a22f57e3575875b3b
MIT
2016-08-25T01:05:20.000Z
2021-07-04T16:17:06.000Z
assets/assessment/example/main.ipynb
drvinceknight/cfm
06977f5c1ba37590b17a8d2a22f57e3575875b3b
MIT
2016-08-24T20:08:15.000Z
2021-12-19T23:26:14.000Z
docs/_static/example-coursework/main.ipynb
drvinceknight/cfm
06977f5c1ba37590b17a8d2a22f57e3575875b3b
MIT
2016-09-22T12:36:03.000Z
2019-03-13T13:21:30.000Z
Qwen/Qwen-72B
1. YES
2. YES