Task 1

Write a function implementing the following map $$ \begin{align} f \colon &\mathbb{R} \to \mathbb{R}\\ & x \mapsto x^2 + \frac{\sin(x+13)}{x} \end{align} $$


In [33]:
### Task 2
Create a function $g$ mapping a function $f: \mathbb{R} \to \mathbb{R}$ to the function $h(x) = |f(x)|$.

With f being the function of task 1, you should get 
    `g(f)(-1) = 0.46342708199956506`f(x::Real) = -x^2 + sin(x+13)/x


Out[33]:
f (generic function with 1 method)

Task 2

Create a function $g$ mapping a function $f: \mathbb{R} \to \mathbb{R}$ to the function $h(x) = |f(x)|$.

With f being the function of task 1, you should get g(f)(-1) = 0.46342708199956506


In [ ]:
g(f::Function) = abs∘f

In [39]:
h = g(f); g(f)(-1)


Out[39]:
0.46342708199956506

Task 3

One options to calculate the inverse of a real number $x$ is given by the recurrence relation.

$$ y_{k+1} = 2y_k - y_k^2 x $$

with sattisfies $y_{k+1} \to \frac{1}{x}$ for $k \to \infty$.

Create a function newtonInv calculating the inverse of a given value $x$ using this iterations. The arguments of the function should be

  • x (real number to be inverted)
  • y0 (starting value for iteration, optional, default: 0.1)
  • iterations (number of iterations to performed, optional, default: 10, keyword option)

Bonus Task: Modify the function such that it has an option "relTol" specifying a relative tolerance (default: 1e-8). Instead of performing a fixed amount of iterations, the modified function should iterate until the solution satisfies the given tolernace.


In [57]:
newtonInv(5)
    y = y0;
    for i in 1:iterations
        y = 2*y-y^2*x
    end
    return y
end


Out[57]:
newtonInv (generic function with 2 methods)

In [61]:
newtonInv(5)


Out[61]:
0.2

Task 4

Write a function crossSwap!(x,y) which exchanges x[1] with y[end] and x[end] with y[1].


In [66]:
crossSwap!([1,2],[4,5])## Task 5

Create a function f(x,+/-1) mapping the interval [-1,1] to right(+1) respectively left part of the unit circle in the complex plane.

$$
\begin{align*}
    f(x, 1) = e^{ \pi i \frac{x}{2}}\\
    f(x,-1) = e^{ \pi i (\frac{x}{2} + 1)}
\end{align*}
$$int2circle(x,::Val{1}) = exp(x/2*pi*im)
int2circle(x,::Val{-1}) = exp((1+x/2)*pi*im)crossSwap!(x,y) = x[1],x[end],y[1],y[end] = y[end],y[1],x[end],x[1]


Out[66]:
crossSwap! (generic function with 1 method)

In [67]:
crossSwap!([1,2],[4,5])


Out[67]:
(5, 4, 2, 1)

Task 5

Create a function f(x,+/-1) mapping the interval [-1,1] to right(+1) respectively left part of the unit circle in the complex plane.

$$ \begin{align*} f(x, 1) = e^{ \pi i \frac{x}{2}}\\ f(x,-1) = e^{ \pi i (\frac{x}{2} + 1)} \end{align*} $$

In [17]:
int2circle(x,::Val{1}) = exp(x/2*pi*im)
int2circle(x,::Val{-1}) = exp((1+x/2)*pi*im)


Out[17]:
int2circle (generic function with 2 methods)

In [ ]: