# Math 300 - Final Exam

Suggetsed solution could be found by this link.

There are 8 short 6-points questions:

• MATLAB part (4 questions): 1, 2, 3, 4
• Python part (4 questions): 6, 7, 8, 9

You can drop 1 out of this 8 questions! Just skip it in your document or type "DROP".

Duration: you have 75 minutes (the entire class duration).

• You can type your answers in any text editor you like. My suggestion is that you keep it simple and use Notepad/Notepad++/TextEdit/gedit/etc. . More advanced word processor like Microsoft Word, Libre Office Writer, Apple Pages, etc. would also be fine. If you are using any other text editor, please double check with me before leaving the class if I can view your document. You can just use a pen and a paper, if you want.
• PLEASE, use the same format as I used in my practice exam solutions: add Q before the number of the question, surround it with parentheses and add colon at the end. Do not write anything else in that line - your answer should start on the next line. Add an empty line after every answer.

(Q1):
c

(Q2):
x = sin(pi/18) + log(6^3/5) + sum(1:100)

(Q3):
...

Late submission: if I recieve your document after 12:00pm, it will not be accepted and you will get 0 points for the exam. So if you are not sure if the message was delivered, please double check with me before leaving the class!

## Provide answers to the following questions:

### MATLAB (35 points)

1. (6 points) Provide at most two MATLAB commands that will create a variable named y and assign it the value of the following sum: $$\sum_{n=3}^{11} \frac{1}{2n+3}$$. The command should NOT produce any output to the console.
2. (6 points) Assume that there is a varible v defined in the current MATLAB session which represents a row-vector with 100 elements (numbers). Provide at most three MATLAB commands that will create a new varible named A which should represent a 100x101 matrix, such that its first 100 columns and 100 rows constitue an identity 100x100 matrix, and the last column is a column-vector having the same elements as v (i.e. v transposed), i.e. $$A = \left[ I_{100} | v^{T} \right]$$.
3. (6 points) What is the name of a MATLAB data type that repesents a floating point number and uses 64-bit to store it in memory?
4. (6 points) Assume that there are two variables defined in the current MATLAB session: A which is a 3x3 matrix, and b which is a column-vector with 3 elements. These two variables represent a system of linear equations in the matrix form, i.e. Ax=b. Assume that this system is consistent and has a unique solution. Provide a single MATLAB command that will output solution of the system of linear equations given by A and b. Recall that the solution could be found using the formula x=A-1b. Alternatively, you can use MATLAB builtin function that solves a system of linear equations.
5. (11 points) Assume that there is a variable x defined in the current MATLAB session, which represents some floating point number. Provide a MATLAB script that will do the following:
1. If x is an integer (just a whole number, which is not necessarily represented by any integer data type) and the square root of x is a number that is less or equal than 10, prints $$\pi$$ raised to the x power.
2. If x is greater than 100 or sin(x) is a strictly positive number, prints $$\pi$$ raised to the 1/x power.
3. If non of the above condtions is true, prints the following message: "No answer!".

### Python (35 points)

1. (6 points) Here is a Python command (and its output) that divides 13 by 4:

>>> 13/4
3

As you can see, the result of the divsion is 3. Why is it not 3.25 (which is the exact result of division of 13 by 4)? How should you modify this command to get exactly 3.25?
2. (6 points) Assume that there is a variable A defined in the current Python session which represents a 3x10 matrix of integeres. Provide a single Python command that will create a new matrix variable named B consisting of all even coulmns of A (i.e. column 2, column 4, ... , column 10).
3. (6 points) Provide a single Python command that creates a function variable f1 to represent the following function $$f_1(x) = e^{x^2+2x+1} + \cos(x)$$ (hint: use lambda expression).
4. (6 points) Assume that there is a varible v defined in the current Python session which represents a column-vector with 100 elements. Provide a single Python command that will transposes vector v (make it a row-vector) .
5. (11 points) Here are two Python implementations of some very well known mathematical function:

Implementation 1

def known_function(n):
res=1
for i in range(1, n+1):
res = res * i
return res

Implementation 2

def known_function(n):
if n == 0:
return 1
else:
return n * known_function(n-1)

The domain of this function is the set of all non-negative integers, so you should not expected any other number to be passed as an input parameter n.

Part 1 (4 points): In "Implementation 2" you see a specific technic when function calls itself. What is the name of this technic?

Part 2 (7 points): What mathematical function is implemented here? Provide the name of the function. If you do not know the name, provide a generic formula for known_function(n) in terms of numbers 1, 2, 3, ... , n, where n > 1 is an integer.

### Algorithm Implementation (30 points)

1. (30 points)

You should implement only ONE of the following algorithms using either MATLAB, or Python. Clearly state which algorithm you are implementing and what language you are using!

If you are using Python be accurate with indentations and do not forget to import all required packages!

Algorithm 1

Implement a function named another_rule(f,a,b,n) that approximates the values of the definite integral using one of the Newton–Cotes formulas:

1. The function has 4 input parameters:
• f - function to be integrated.
• a - lower limit of integration.
• b - upper limit of integration.
• n - number of partitions.
2. The function returns a floating point number - value of the defintie integral.
3. Here is how your approximation should be done. Consider the integral $$\int_a^b f(x)\,dx$$, where $$f$$ is your input parameter f. Let $$a=x_0\lt x_1\lt\dots\lt x_n=b$$ be an equally-spaced partition of $$[a,b]$$ (a and b input parameters). If $$h=(b-a)/n$$ is the uniform width of the subintervals (where $$n$$ is your input parameter n), then the integral can be approximated as $\int_a^b f(x)\,dx \sim \sum_{i=1}^{n} \left( h \cdot \frac{f(x_{i-1})+f(x_{i})}{2} \right).$
4. Your function should print approximated value of the integral.
5. Your function should plot all the points rerpesenting value of your function at endpoints of the subintervals i.e. $$f(x_0), f(x_1), \dots, f(x_n)$$) using "+" point marker. The points should be connected with a dashed line (in other words, plot the original function with grid having n+1 points with left endpoint at a and right endpoint at b, where bothe a and b are included.
Check your algorithm: if $$f(x) = 3x^2+2$$, $$a=1$$, $$b=2$$ and $$n = 100$$, your function should produce approximately 9.

Algorithm 2

Implement a function named solve_newton(f,x0,e) that solves equation of the form $$f(x)=0$$ using Netwon's Method:

1. The function has 3 input parameters:
• f - function representing $$f(x)$$.
• x0 - starting estimate for the root $$x$$.
• e - precision.
2. The function returns a floating point number - approximation of the solution of $$f(x)=0$$.
3. Here is how the algorithm should work.
• Consider a continuous function $$f(x)$$ (f input parameter), such that there exists at least one real number satisfying $$f(x)=0$$, i.e. $$x$$ is the solution of the equation $$f(x)=0$$.
• Let $$x_0$$ (x0 input parameter) be a real number which is sufficiently close to this solution $$x$$.
• Then you can approximate this solution $$x$$ by successively evaluating values $$x_1, x_2, \ldots , x_{k-1}, x_k, \ldots$$ using recursive formula $x_k = x_{k-1} - \frac{2e}{f(x_{k-1} +e) - f(x_{k-1}-e)} f(x_{k-1})$ until you get to some $$x_k$$, such that $$|x_k - x_{k-1}| \lt e$$ (where $$e$$ is the input parameter e). Then this $$x_k$$ is the approximation of your solution $$x$$.
4. Your function should print approximated value of the fixed point.
5. Your function should plot the original function f on the interval [x-5, x+5] using a grid with at least 100 points, where x is your approximated value.
Check your algorithm: if $$f(x) = x^2+2x-15$$, $$x_0=1$$, and $$e = 10^{-6}$$, your function should produce approximately 3.