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]:
In [ ]:
g(f::Function) = abs∘f
In [39]:
h = g(f); g(f)(-1)
Out[39]:
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
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]:
In [61]:
newtonInv(5)
Out[61]:
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]:
In [67]:
crossSwap!([1,2],[4,5])
Out[67]:
In [17]:
int2circle(x,::Val{1}) = exp(x/2*pi*im)
int2circle(x,::Val{-1}) = exp((1+x/2)*pi*im)
Out[17]:
In [ ]: