Commit 43192908 authored by Jochen Schulz's avatar Jochen Schulz
Browse files

fix path

parent dbea251e
%% Cell type:markdown id: tags:
# Exercises: charts
## problem 1
Let y1,y2 be two points in the plane. We study the edge with vertices y1 and y2. We replace this edge by 4 new edges $\overline{y_1 z_1}$, $\overline{z_1 z_2}$, $\overline{z_2 z_3}$,
$\overline{z_3 y_2}$ with vertices $z_1=\frac23 y_1 + \frac13 y_2$,
$z_3=\frac13 y_1 + \frac23 y_2$ and
$$z_2 = \frac{\sqrt{3}}{6} \left( \begin{array}{cc}
0 & 1 \\ -1 & 0 \\
\end{array} \right)
(y_1 - y_2) + \frac12 (y_1 + y_2).$$
Similar to the example of the Sierpinski triangle every new edge is replaced by this procedure by 4 edges. Write an appliication that reapplies this procedure k times and visualizes the result.
%% Cell type:code id: tags:
``` haskell
import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Default.Class
import Control.Lens
import Data.Colour
import Data.Colour.Names
listOfVerts:: (Double,Double)-> (Double,Double)-> Int ->[(Double,Double)]
listOfVerts (xa,ya) (xb,yb) 0 = [(xb,yb)]
listOfVerts (xa,ya) (xb,yb) k = (listOfVerts (xa,ya) (xz1,yz1) (k-1)) ++ (listOfVerts (xz1,yz1) (xz2,yz2) (k-1)) ++(listOfVerts (xz2 ,yz2) (xz3,yz3) (k-1))++(listOfVerts (xz3,yz3) (xb,yb) (k-1))
where xz1 = 2/3*(xa) +1/3*(xb)
yz1 = 2/3*(ya) +1/3*(yb)
xz2 = sqrt(3)/6*((ya) -(yb))+1/2*((xa) + (xb))
yz2 = sqrt(3)/6*((xb) -(xa))+1/2*((ya) + (yb))
xz3 = 1/3* (xa) + 2/3* (xb)
yz3 = 1/3* (ya) + 2/3* (yb)
a = listOfVerts (0, 0.0) (1.0 ,0.0) 4
chart = toRenderable layout
where
lineplot = plot_lines_values .~ [a]
$ plot_lines_style . line_color .~ opaque blue
$ def
layout = layout_title .~ "Koch curves"
$ layout_plots .~ [toPlot lineplot]
$ def
chart
```
%% Cell type:markdown id: tags:
## problem 2
Write a function that for a given integer $n$ draws a regular polygon with $n$ vertices.
Apply the recursive function of exercise 1 on the edges of a regular hexagon.
*Remark*: The vertices $(x_i,y_i)$ are
$$x_i=\sin( 2 {\pi i}/{n} ), \quad y_i=\cos( 2 {\pi i}/{n} ),
\quad i=1, \dots ,n$$
%% Cell type:code id: tags:
``` haskell
regPolygon:: Int -> [(Double,Double)]
regPolygon n =[(sin(2*pi*fromIntegral k/fromIntegral n),cos(2*pi*fromIntegral k/fromIntegral n))|k<-[1..n]]
a = regPolygon 6
vertOfStar:: (Double,Double)-> [(Double,Double)]->[(Double,Double)]
vertOfStar c [a] = listOfVerts a c 2
vertOfStar c (x:xs) = (listOfVerts x (head xs) 2)++ vertOfStar c xs
b = vertOfStar (head a) a
chart = toRenderable layout
where
lineplot = plot_lines_values .~ [b]
$ def
layout = layout_title .~ ""
$ layout_plots .~ [toPlot lineplot]
$ def
chart
```
%% Cell type:markdown id: tags:
## problem 3
Write an application that for a given $a>0$ plotes the function
$$f(x):= 1/(x^2+a)$$
on the interval [-3,3].
How can you generalize your application by choosing an arbitrary function as parameter.
%% Cell type:code id: tags:
``` haskell
import Graphics.Rendering.Chart
import Data.Colour
import Data.Colour.Names
import Data.Default.Class
import Graphics.Rendering.Chart.Backend.Cairo
import Control.Lens
am :: Double -> Double -> Double
am x a = 1/(x**2+a)
chart f a = toRenderable layout
where
function = plot_lines_values .~ [[ (x,(f x a)) | x <- [-3,-(2.95)..3]]]
$ plot_lines_style . line_color .~ opaque blue
$ def
layout = layout_title .~ "problem 3"
$ layout_plots .~ [toPlot function]
$ def
chart am 2
chart (\x a -> 3*x+a) 2
```
%% Cell type:markdown id: tags:
## problem 4
Look up the file 'daten.dat' in the data directory. Write an application that importes the data and plotes the function based on the given data.
%% Cell type:code id: tags:
``` haskell
import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Cairo
import Data.List.Split
b <- readFile "../data/daten.dat"
b <- readFile "../../data/daten.dat"
c = map ((\[a,b] -> (a,b)) . map (\x -> read x :: Double) . splitOn ",") (tail . lines $ b)
toRenderable $ plot (line "" [c] )
```
%% Cell type:code id: tags:
``` haskell
:hoogle splitOn
```
%% Cell type:code id: tags:
``` haskell
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment