<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
Step2:  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
Step5:  b. y =\(\frac{3x ^ 3 + 6 \sqrt{x} + 3) }{ (3  x ^{(1 / 4)})}\)
Step8:  \(y=2 x (x - 3) (x - 5)\)
Step11:  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)\).
Step14:  b. Create a variable intersection_points which has value the points where \(f(x)\) and \(g(x)\) intersect.
Step16:  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.
Step18:  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.
Step20:  b. Create a variable probability_of_selecting_N with value the probability that the letter N is included in the selection.
Step22:  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.
Step25:  Question 4
A sequence is given by
Step28:  b. Output an expression for \(x_2\) in terms of \(p\).
Step31:  b. Output an expression for \(x_3\) in terms of \(p\)
Step33:  c. Output the values of \(p\) for which \(x_3=1\).
Step35:  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\).
<ASSISTANT_TASK:>
Python Code:
### BEGIN SOLUTION
import sympy as sym
x = sym.Symbol("x")
y = 2 * x * (x - 3) * (x - 5)
sym.diff(y, x)
### END SOLUTION
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)
x = sym.Symbol("x")
expected_answer = 2 * x * (x - 5) + 2 * x * (x - 3) + (x - 3) * (2 * x - 10)
feedback_text = fYour answer is not correct.
The expected answer is {expected_answer}.
assert sym.simplify(q1_a_answer - expected_answer) == 0, feedback_text
Explanation: 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)\)
End of explanation
### BEGIN SOLUTION
y = (3 * x ** 3 + 6 * sym.sqrt(x) + 3) / (3 * x ** (sym.S(1) / 4))
sym.diff(y, x)
### END SOLUTION
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)
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 = fYour answer is not correct.
The expected answer is {expected_answer}.
assert sym.simplify(q1_b_answer - expected_answer) == 0, feedback_text
Explanation: b. y =\(\frac{3x ^ 3 + 6 \sqrt{x} + 3) }{ (3  x ^{(1 / 4)})}\)
End of explanation
### BEGIN SOLUTION
y = 2 * x * (x - 3) * (x - 5)
sym.diff(y, x)
### END SOLUTION
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)
x = sym.Symbol("x")
expected_answer = sym.diff(2 * x * (x - 3) * (x - 5), x)
feedback_text = fYour answer is not correct.
The expected answer is {expected_answer}.
assert sym.simplify(q1_c_answer - expected_answer) == 0, feedback_text
Explanation: \(y=2 x (x - 3) (x - 5)\)
End of explanation
### 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
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`")
expected_answer = sym.solveset(- 3 * x ** 2 + 4 * x + 3, x)
feedback_text = fYour 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
Explanation: 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)\).
End of explanation
### BEGIN SOLUTION
g = - x ** 3 + 3 * x ** 2 - x + 3
intersection_points = sym.solveset(sym.Eq(f, g), x)
### END SOLUTION
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`")
expected_answer = sym.FiniteSet(1, 3)
feedback_text = fYour 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
Explanation: b. Create a variable intersection_points which has value the points where \(f(x)\) and \(g(x)\) intersect.
End of explanation
### 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
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`")
expected_answer = sym.S(4) / 3
feedback_text = fThe 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 
Explanation: 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.
End of explanation
import itertools
### BEGIN SOLUTION
letters = "MONGOLIA"
words = list(itertools.combinations(letters, 4))
number_of_selections = len(words)
### END SOLUTION
expected_answer = 70
feedback_text = fThe 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`")
Explanation: 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.
End of explanation
### BEGIN SOLUTION
probability_of_selecting_N = sum('N' in word for word in words) / len(words)
### END SOLUTION
expected_answer = 35 / 70
feedback_text = fThe 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`")
Explanation: b. Create a variable probability_of_selecting_N with value the probability that the letter N is included in the selection.
End of explanation
### 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
expected_answer = 48 / 1680
feedback_text = fThe 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`")
Explanation: 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.
End of explanation
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
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`")
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`")
Explanation: 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.
End of explanation
### BEGIN SOLUTION
p = sym.Symbol("p")
generate_x(n=2, p=p)
### END SOLUTION
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)
p = sym.Symbol("p")
expected_answer = p + 1
feedback_text = fYour answer is not correct.
The expected answer is {expected_answer}.
assert sym.simplify(q4_a_answer - expected_answer) == 0, feedback_text
Explanation: b. Output an expression for \(x_2\) in terms of \(p\).
End of explanation
### BEGIN SOLUTION
generate_x(3, p=p)
### END SOLUTION
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)
p = sym.Symbol("p")
expected_answer = 2 * p ** 2 + 3 * p + 1
feedback_text = fYour answer is not correct.
The expected answer is {expected_answer}.
assert sym.simplify(q4_b_answer - expected_answer) == 0, feedback_text
Explanation: b. Output an expression for \(x_3\) in terms of \(p\)
End of explanation
### BEGIN SOLUTION
sym.solveset(sym.Eq(generate_x(3, p=p), 1), p)
### END SOLUTION
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
Explanation: c. Output the values of \(p\) for which \(x_3=1\).
End of explanation
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
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 = fYour 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`")
Explanation: 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\).
End of explanation
<END_TASK>