To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 630a5b5b authored by dkammer's avatar dkammer
Browse files

week3 ready

parent 5a5304d2
*~
\ No newline at end of file
newton_raphson
\ No newline at end of file
compile: `g++ -Werror -Wpedantic -g -o newton_raphson newton_raphson.cpp`
\ No newline at end of file
# Newton-Raphson method
taken from Pitt-Francis \& Whiteley (2017)
The Newton–Raphson method is often used to solve nonlinear equations of the form $` f(x)=0 `$. This is an iterative algorithm: given an initial guess $` x_0 `$, successive iterates satisfy
```math
x_i = x_{i−1} − \frac{f(x_{i−1})}{f'(x_{i−1})}
```
for $` i=1,2,3,… `$. This algorithm may be terminated when $` |x_i−x_{i−1}| < \epsilon `$ for some user-prescribed $` \epsilon `$.
In this exercise, we will apply the Newton–Raphson algorithm to the function
```math
f(x) = \exp{x} + x^3 − 5 ~,
```
with initial guess $` x_0 = 0 `$.
1. Write down (on paper) the Newton–Raphson iteration for this choice of $` f(x) `$.
2. By using a `for` loop, and an `array` for the iterates $` x_i `$, write a program that implements the Newton–Raphson iteration for $` i = 1,2,3,..., 100 `$. Print out the value of $` x_i `$ on each iteration, and confirm that the iteration does converge as $` i `$ increases. At this stage, do not worry about terminating the iteration when $` \epsilon `$ is sufficiently small.
3. Think of a check that can be performed on the iterates $` x_i `$, as $` i `$ becomes larger, that allows you to have confidence that your solution is correct. Implement this check in your program.
4. It is not necessary to store the value of $` x_i `$ on each iteration to implement the Newton–Raphson algorithm. All that is needed is the previous iterate, $` x_{i−1} `$, and the current iterate, $` x_i `$. Modify your code so that the array representing $` x_i `$ for $` i=1,2,…,100 `$ is replaced by two scalar variables, `x_prev` and `x_next`.
5. Modify your code so that, by use of a `while` statement, the iteration terminates when `|x_next - x_prev| < eps`. Investigate the use of different values of $` \epsilon `$.
\ No newline at end of file
#include <iostream>
int main(int argc, char* argv[])
{
// here comes your code
return 0;
}
write_mesh
\ No newline at end of file
compile: `g++ -Werror -Wpedantic -g -o write_mesh write_mesh.cpp`
\ No newline at end of file
# Write a mesh file
In this assignment, you will write a mesh file to the disk. The `write_mesh.cpp` file contains an array with node coordinates, an array with the connectivity of for the elements and a few additional information.
Write code into your `write_mesh.cpp` file that save a file named *my_mesh.msh* with the following syntax:
```
$MeshFormat
version-number file-type data-size
$EndMeshFormat
$Nodes
number-of-nodes
node-number x-coord y-coord z-coord
$EndNodes
$Elements
number-of-elements
elm-number elm-type number-of-tags < tag > … node-number-list
$EndElements
```
`version-number`, `file-type`, and `data-size` are provided in the code. Further, `node-number` is a continuously increasing number for each node starting with 1. Similarly, `elm-number` is a continuously increasing number for each element starting with 1. `elm-type` is provided. You should use `number-of-tags` equal to 2 and write tag1 as integer 0 and tag2 as integer 1. `node-number-list` is refering to the identifier numbers of the nodes and should correspond to the content of the `connectivity` array.
You may test your saved mesh file by using the following command: `gmsh my_mesh.msh`. It should load without any errors or warnings.
Notes:
1. if you need `for` loops, please refer to chapter 2 in Pitt-Francis \& Whiteley (2017)
2. for how to write to a file, please refer to chapter 3 in Pitt-Francis \& Whiteley (2017)
\ No newline at end of file
/*
Code template for group 2
*/
int main(int argc, char* argv[])
{
/*
For simplicity, the meshes are hard-coded here.
Except for a homework assignment, this would never make sense.
*/
// software general information needed for file
std::string version = "2.2";
std::string ftype = "0";
int dsize = sizeof(double);
// nodes of FEM mesh
int nb_nodes = 9;
double nodes[9][2] = { {0.0, 0.0},
{0.5, 0.0},
{1.0, 0.0},
{1.0, 0.5},
{1.0, 1.0},
{0.5, 1.0},
{0.0, 1.0},
{0.0, 0.5},
{0.5, 0.5} };
// connectivity of FEM mesh: nodes for each element
int nb_elements = 8;
int elem_type = 2; // triangles
int connectivity[8][3] = { {1, 9, 8},
{1, 2, 9},
{2, 3, 9},
{3, 4, 9},
{8, 9, 7},
{9, 6, 7},
{9, 5, 6},
{9, 4, 5} };
// here comes your code
return 0;
}
read_mesh
\ No newline at end of file
compile: `g++ -Werror -Wpedantic -g -o read_mesh read_mesh.cpp`
\ No newline at end of file
# Read a mesh file
In this assignment, you will read a mesh from a file on disk. The `read_mesh.cpp` file contains an array for node coordinates and an array for the connectivity of the elements.
Write code into your `read_mesh.cpp` file that reads the file named *square.msh* that has the following syntax:
```
$MeshFormat
version-number file-type data-size
$EndMeshFormat
$Nodes
number-of-nodes
node-number x-coord y-coord z-coord
$EndNodes
$Elements
number-of-elements
elm-number elm-type number-of-tags < tag > … node-number-list
$EndElements
```
`node-number` is a continuously increasing number for each node starting with 1. Similarly, `elm-number` is a continuously increasing number for each element starting with 1. `elm-type` indicates the element type, you can ignore elements of type 15 and 1. Use only type 2 (triangle) elements. The `number-of-tags` and the actual tags that follow (of the number indicated) can be ignored. `node-number-list` is refering to the identifier numbers of the nodes and is the information that should go into the `connectivity` array.
After reading the nodes and connectivity, use `std::max` and `std::min` to find the maximum and minimum coordinates of the mesh as well as to find the smallest and largest node number in the connecitivity to verify that your reading was successful.
Notes:
1. if you need `for` loops, please refer to chapter 2 in Pitt-Francis \& Whiteley (2017)
2. for how to write to a file, please refer to chapter 3 in Pitt-Francis \& Whiteley (2017)
\ No newline at end of file
/*
Code template for group 3
*/
int main(int argc, char* argv[])
{
/*
At this point, we do not know that tools necessary
to create an array of size given by the mesh file.
Hence, you need to hard-code it here. This is the only
time we will be doing this. In the future, you would use the
value given by the mesh file.
*/
// nodes of FEM mesh
int nb_nodes = 9; // you need to adapt this
double nodes[9][2]; // you need to adapt this
// connectivity of FEM mesh: nodes for each element
int nb_elements = 8; // you need to adapt this
int elem_type = 2; // triangles
int connectivity[8][3]; // you need to adapt this
// here comes your code
return 0;
}
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
29
1 0 0 0
2 1 0 0
3 0 1 0
4 1 1 0
5 0.2499999999994121 0 0
6 0.499999999998694 0 0
7 0.7499999999993416 0 0
8 1 0.2499999999994121 0
9 1 0.499999999998694 0
10 1 0.7499999999993416 0
11 0.7500000000003465 1 0
12 0.5000000000020591 1 0
13 0.2500000000010404 1 0
14 0 0.7500000000003465 0
15 0 0.5000000000020591 0
16 0 0.2500000000010404 0
17 0.5000000000000138 0.4999999999999811 0
18 0.708333333333241 0.7083333333334085 0
19 0.2916666666665704 0.7083333333331984 0
20 0.7083333333334353 0.2916666666667725 0
21 0.291666666666815 0.2916666666665436 0
22 0.4999999999998699 0.7797619047618962 0
23 0.2202380952380973 0.499999999999829 0
24 0.7797619047619168 0.5000000000001237 0
25 0.5000000000001644 0.2202380952380766 0
26 0.8385416666668793 0.8385416666665236 0
27 0.1614583333335019 0.8385416666669894 0
28 0.8385416666664537 0.1614583333331048 0
29 0.1614583333329948 0.1614583333335715 0
$EndNodes
$Elements
60
1 15 2 0 1 1
2 15 2 0 2 2
3 15 2 0 3 3
4 15 2 0 4 4
5 1 2 0 1 1 5
6 1 2 0 1 5 6
7 1 2 0 1 6 7
8 1 2 0 1 7 2
9 1 2 0 2 2 8
10 1 2 0 2 8 9
11 1 2 0 2 9 10
12 1 2 0 2 10 4
13 1 2 0 3 4 11
14 1 2 0 3 11 12
15 1 2 0 3 12 13
16 1 2 0 3 13 3
17 1 2 0 4 3 14
18 1 2 0 4 14 15
19 1 2 0 4 15 16
20 1 2 0 4 16 1
21 2 2 0 1 11 22 18
22 2 2 0 1 14 23 19
23 2 2 0 1 8 24 20
24 2 2 0 1 5 25 21
25 2 2 0 1 10 18 24
26 2 2 0 1 7 20 25
27 2 2 0 1 13 19 22
28 2 2 0 1 16 21 23
29 2 2 0 1 11 12 22
30 2 2 0 1 14 15 23
31 2 2 0 1 8 9 24
32 2 2 0 1 5 6 25
33 2 2 0 1 12 13 22
34 2 2 0 1 15 16 23
35 2 2 0 1 9 10 24
36 2 2 0 1 6 7 25
37 2 2 0 1 17 18 22
38 2 2 0 1 17 22 19
39 2 2 0 1 17 19 23
40 2 2 0 1 17 24 18
41 2 2 0 1 17 23 21
42 2 2 0 1 17 20 24
43 2 2 0 1 17 25 20
44 2 2 0 1 17 21 25
45 2 2 0 1 11 18 26
46 2 2 0 1 14 19 27
47 2 2 0 1 8 20 28
48 2 2 0 1 5 21 29
49 2 2 0 1 10 26 18
50 2 2 0 1 7 28 20
51 2 2 0 1 13 27 19
52 2 2 0 1 16 29 21
53 2 2 0 1 4 11 26
54 2 2 0 1 3 14 27
55 2 2 0 1 3 27 13
56 2 2 0 1 4 26 10
57 2 2 0 1 1 29 16
58 2 2 0 1 2 28 7
59 2 2 0 1 2 8 28
60 2 2 0 1 1 5 29
$EndElements
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