## ILAS : Introduction to Programming for Engineers
## Exam 2018

Answer all questions.

Submit your work by email to philamore.hemma.5s@kyoto-u.ac.uk

Format: .ipynb file.

Deadline: 23:59 July 23 (07/23)

Total: 50 marks

 

### Question 1
###### 1 mark
The gravitational potential, $V$, of a particle of mass $m$ at a distance $r$ from a body of mass $M$, is:

$$
V = \frac{G M m}{r}
$$

In the cell below, find $V$ when:

$G$ = *gravitational constant* = $6.674 \times 10^{-11} \textrm{Nm}^{2}\textrm{kg}^{-2}$.
<br>$M = 1.65 \times 10^{12}\textrm{kg}$
<br>$m = 6.1 \times 10^2\textrm{kg}$
<br>$r = 7.0 \times 10^3\textrm{kg}$

Use variable names `G`, `M`, `m` and `r` to represnt $G, M, m$ and $r$. 
<br>Express the numbers using __scientific notation__.


```python

```

### Question 2 
#### Part A
###### 1 mark
The volume of a cone is:
$$
V = \frac{1}{3}(base \ area)\times(perpendicular \ height)
$$



Find the internal volume of a cone of internal dimensions:

base radius, $r = 5cm$

perpendicular height, $h = 15cm$

Assign variables for $r$ and $h$ before solving.


```python
pi = 3.142
# Internal volume
```

#### Part B
###### 2 marks

The cone is filled with liquid.

The liquid is transferred to a hollow cylinder (base radius , $r_c = 4cm$).

The __volume__ of liquid in the cylinder is:

$V = (base \ area)\times(height \ of \ liquid)$

Find the __height__ of the liquid in the cylinder?

Create a variable for $r_c$ before solving.




```python


```

#### Part C
###### 1 mark
The total height of the cyclinder, $H_{tot}$ is 10cm.

Use a __comparison operator__ to show if the height of the liquid, $H$, is more than half the total height of the cylinder.  


```python

```

### Question 3
#### Part A
###### 2 marks
Use a for loop to print the square roots of the first 25 odd positive integers.


#### Part B
###### 2 marks
If the number generated is greater than 3 and smaller than 5, print "`skip`" and __`continue`__ to the next iteration *without* printing the number. 



```python



```

### Question 4

#### Part A
###### 3 marks
Write a function called `is_even` that determines if a number is even by running:

```python
is_even(n)
```

Input: `n` (an integer). 

Output: 
 - `True` if the argument is even
 - `False` if the argument is not even
 
Include a __documentation string (docstring)__ to say what your function does.

#### Part B
###### 3 marks
Edit `is_even` to:
- take two input arguments:
 - a numeric variable, `n`
 - a numpy function that returns the sqaure root of the input argument. 
- return:
 - `True` if the square root of n is even
 - `False` if the square root of n is not even

#### Part C
###### 1 mark
Make `square_root` the __default__ value of the function argument. 
<br>Force the function argument to be input using a named argument. 

#### Part D
###### 1 mark
Print the output ofthe function `is_even` for the first 25 natural numbers.


```python

```

### Question 5
###### 2 marks
In the example below, complete the comments with definition ("local variable"/"global variable") describing the scope of variables a-c.


```python
# In the code below: 
# a is a local variable / global variable
# b is a ...
# c is a ...
# d is a ...

def my_function(a):
    b = a - 2
    return b

c = 3

if c > 2:
    d = my_function(5)
    print(d)
```

    3


### Question 6
###### 3 marks
Write a function called counter:

Input: A list. e.g. `[7, 3, 5, 8, 50]`

Output: Returns the numer of times a number greater than 5 appears in a list.



```python

```

### Question 7

The magnitude of an $n$ dimensional vector can be written

$$
|\mathbf{x}|= \sqrt{x_1^2 + x_2^2 + ... x_n^2} = \sqrt{\sum_{i = 1}^{n} (x_{n})^2 }
$$

Therefore...

The magnitude of a 2D vector (e.g. $x = [x_1, x_2]$):

$$
|\mathbf{x}|= \sqrt{x_1^2 + x_2^2}
$$
<br>

The magnitude of a 3D vector (e.g. $x = [x_1, x_2, x_3]$):

$$
|\mathbf{x}|= \sqrt{x_1^2 + x_2^2 + x_3^2}
$$
<br>

#### Part A 
###### 3 marks
In the cell below, write a function called `magnitude` that computes the magnitude of an n-dimensional vector.

Input: `list` with n elements (e.g. [x, y]) if vector is 2D, [x, y, z]), if vector is 3D.

Output: Return magnitude of the vector.

Include a doc-string to say what your function does.


#### Part B
###### 1 marks
Print the output of your function to show that it works for both 2D and 3D input vectors. 
<br>Check your function, for example use the numpy function `numpy.linalg.norm()` to verify the answer is correct.  


```python
# A function that computes the magnitude of an n-dimensional vector 
```

## Question 8

The factorial of a positive integer $n$ is:

\begin{align}
n! = \prod_{i=1}^{n} i =1 \cdot 2 \cdot 3 \cdot ... (n - 2) \cdot (n - 1) \cdot n
\end{align}

We can write this *recursively*.
<br>i.e. using the value of $(n-1)!$ to compute the value of $n!$:

$$
n! = (n-1)! n 
$$

Note: $0! = 1$
 
e.g. 
<br> $1! = 1 \cdot 0! = 1 $
<br> $2! = 2 \cdot 1! = 2 \cdot 1 = 2$
<br> $3! = 3 \cdot 2! = 3 \cdot 2 \cdot 1 = 6$
<br> $4! = 4 \cdot 3! = 4 \cdot 3 \cdot 2 \cdot 1 = 24$



#### Part A  
###### 4 marks
Write a __recursive function__ called `factorial` to compute $n!$ of an input argument `n`:

- __Input:__ Numerical variable `n`
- __Output:__ `factorial(n) = factorial(n-1)*n` 

Include a doc-string to say what your function does.  

#### Part B  
###### 3 marks
The formula above only applies to __positive integers__(with a specific exception for 0). 

Edit your function to raise an error if the input value is a positive integer or zero. 





```python

```

## Question 9
###### 6 marks
Create an array, `x` with 100 values from 0 to 20, inclusive. 

Compute $y=\sin(x)$.

Plot $y$ vs. $x$ with a blue line. 

Replace all values of $y$ that are larger than 0.5 with 0.5.

Replace all values of $y$ that are smaller than $-$0.75 with $-0.75$.

Plot $x$ vs. $y$ using a red line on the same graph. 




```python

```

## Question 10

Using the example data set `a`,`b`:

#### Part A
###### 2 marks
Plot a scatter graph of `a` against `b`, with `a` on the horizontal axis and `b` on the vertical axis. 
<br>Remember to use `%matplotlib inline` to make figures appear in Jupyter Notebook

#### Part B
###### 5 marks
Try fitting 3 different polynomial functions to the data. Plot each curve as a line of on the graph.

#### Part C
###### 3 marks
Show which polynomial function gives the best fit using the root mean squared error (RMSE). Print the equation of the *optimal* curve i.e. the curve with the *smallest* RMSE. 

#### Part D
##### 1 mark
Display the RMSE of each curve as a figure legend.



```python
import numpy as np
a = np.array([88438,45505,75127,115571,89911,87432,100083,85589,73104,86890,78580,70785,41050,57610,107537,59262,73038,87891,75368,111638,74911,71599,96774,79667,90725,93816,75859,64969,205688,71500,53098,71250,89615,94747,50400,63673,78257,72785,83015,150000,84699,67191,86298,117705,88935,89643,106678,97894,132164,59387,60684,96151,68794,74559,29430,88362,111792,57205,83651,87518,80129,86801,110761,63274,66143,110694,52590,59994,80460,103589,68298,59056,40294,161848,103100,86354,37428,43307,80792,77368,109159,71538,84783,86250,82900,74728,48597,75549,106942,102167,62708,60630,70273,84918,88693,74141,46627,119112,88260,97262,86095,110472,82734,84761,91715,103292,86339,147993,77560,100625,68094,78250,75426,86138,112344,115000,98846,90499,80029,61959,76779,68833,81026,66361,92737,76692,64974,103869,51951,108854,61038,75938,75346,40639,73156,80067,82322,52353,
              62832,207262,160106,77740,72011,167094,58458,41639,79528,66583,83993,138082,77366])
b = np.array([1.7,-0.4,0.5,2.6,1.4,1.5,1.5,1.7,-0.5,1.6,0.9,1.1,-1.7,0.3,1.8,0.5,1,1.9,0.1,2,1.7,1,1.2,1.5,1,1.1,1.2,0,2.6,1.4,-0.8,1.6,1.1,1.2,-1.4,-0.5,1.9,0,1.5,2.4,1.5,0.7,1.8,2,2.4,1.6,2,2.3,2,0.1,0.3,2.3,0,0,-1.7,1.9,2,0,0.9,1.3,0.4,1.6,2.3,-0.1,1.7,2.1,-0.9,0.1,1,1.9,0.4,-0.3,-2.4,2.7,1.3,2,-1.3,-1.5,0.7,1.1,2.3,1.1,0.7,0.9,1.1,0.1,-0.9,1.4,2.1,1.2,0.1,0.8,0.3,1.4,1.5,1,-0.5,2.4,0.9,1.5,1.6,1.2,1.3,1.8,0.8,1.8,1.9,2.6,1.5,1.8,1.8,0.6,0.7,1.2,1.5,2.5,1.1,1.6,1.6,1,0,0,1,0.5,1.7,0.6,0.1,1.7,0.2,2.1,0.1,0.9,0.8,-1.3,1.3,0.5,1.5,-0.6,1.2,2.4,2.6,1.1,0.8,2.5,-0.2,-2,0.1,0.1,1.6,2.6,1.2])
```

## Questionnare
This is a question about your experience of the course this year.　
<br>Please answer honestly and constructively.
<br>You may give written answers in English or Japanese. 

この問題は、この授業の君の経歴を訪ねるアンケートである
<br>ただし、真面目、かつ真剣な答案が望ましい。
<br>英語や日本語で回答できる。勿論、点数に関係ないか、出来れば英語でお願いします。








__a) ENTRY LEVEL__ 
<br>Please write the title of your major at your home university.　
<br>君の大学の専門の名前を書きなさい。

Please write which number best describes your knowledge of this subject before starting the course?　
<br>この授業のする前に、授業の内容に対して、自分に最も近い状況を、次の番号の中から選んてください。
1. No previous study.　全然知識無し
1. A basic understanding.　基本の知識が持つ
1. A good fundamental understanding.　ある程度の知識が持つ、深い理解した
1. Comprehensive previous study.　私はプログラムマスター


```python
# Major 専門名: 
    
# Knowledge of this subject before starting the course　この授業の前の知識レベル: 
```

__b) ENJOYMENT__
<br>Write what you enjoyed most/found most interesting  about the course.　
<br>この授業の最も面白いところを書きなさい。


```python

```

Write what you enjoyed least / found least interesting about the course.　
<br>この授業の最もつまらないところを書きなさい。


```python

```

__c) KNOWLEDGE TRANSFER__

Please choose the number indicating how much you feel your understanding improved as a result of taking this course.　
<br>この授業した後、どのくらい上昇したが、次の番号の中から選んてください。

1.	__Dramatic improvement.__ I can confidently and independently use and apply my knowledge of programming in Python.　<br>__驚き激増した.__　自力でPythonを使える、思ったままPythonプログラムイング出来る。

2.	__Significant improvement.__ I have a much better understanding of this subject than when starting the course.　<br>__かなり増えた.__　授業の前と比べて、プログラムイングのことよく分かりました。

3.	__Slight improvement.__ My knowledge has improved but I do not feel confident in my understanding of this topic.　<br>__少し増えた.__　ある程度の知識を受けたが、まだ分からいことある。

4.	__No improvement.__ My knowledge of this topic has improved very little as a result of taking this course.　<br>__全然増えてない.__　授業を受けでも、前と比べて、あまり増えることはない。
  







```python

```

__d) DELIVERY__
<br>Please rate the __use of english__ by writing the number you feel is most appropriate next to each of the communication formats listed below .　
<br>先生の英語（文章）は読めやすさ、次の番号の中から選んてください。
1. Easy to understand.　読め易い
1. A few things were unclear / not well explained.　少し読めないところがある
1. Many things were unclear / not explained well.　たくさん読めないところがある
1. I could understand very little. 　読めるところも少ない




```python
# Written instructions in Jupyter notebooks `Jupyter notebooks`の使え方の説明（理解安さは何番）: 
    
# Written instructions for Test-Yourself exercises 毎章の練習問題の説明（理解安さは何番）:

# Written instructions for coursework assignement 最後の宿題の説明（理解安さは何番）:
    
# Verbal instructions/explanation 全体的の説明（理解安さは何番）:
```

Please rate __how well-explained and understandable__ the course material was by writing the number you feel is most appropriate next to each of the communication formats listed below .　
<br>先生の説明（文章）は理解やすかったが、次の番号の中から選んてください。
1. Easy to understand.　理解易い
1. A few things were unclear / not well explained.　少し理解できないところがある　
1. Many things were unclear / not explained well.　たくさん理解できないところがある
1. I could understand very little. 　理解できるところも少ない




```python
# Written instructions in Jupyter notebooks `Jupyter notebooks`の使え方の説明（理解安さは何番）: 
    
# Written instructions for Test Yourself exercises 毎章の練習問題の説明（理解安さは何番）:

# Written instructions for coursework assignement 最後の宿題の説明（理解安さは何番）:
    
# Verbal instructions/explanation 全体的の説明（理解安さは何番）:
```

_e) FURTHER STUDY__

Are you interested to continue studying programming?
<br>プログラミングを引き続き学びたいですか.






```python

```

__f) ADDITIONAL COMMENTS__
<br>__[optional]__

Please include any additional ideas you have about the course. 
<br>この授業についてコメントがあれば自由に記載してください


```python

```


```python

```

c41e30259893d05ce822a21e425256cd05854679
ipynb
Jupyter Notebook
Exam.ipynb
Tommy3121173/tommy
429aefb377f84a1d49e85f825a32ac2c160ebc85
MIT
Exam.ipynb
Tommy3121173/tommy
429aefb377f84a1d49e85f825a32ac2c160ebc85
MIT
Exam.ipynb
Tommy3121173/tommy
429aefb377f84a1d49e85f825a32ac2c160ebc85
MIT
Qwen/Qwen-72B
1. YES
2. YES