Download Iterative Verfahren der Numerik HS 2013 Prof. M. Grote / L. Gaudio

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Big O notation wikipedia , lookup

Functional decomposition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Central limit theorem wikipedia , lookup

Dirac delta function wikipedia , lookup

Non-standard calculus wikipedia , lookup

Function (mathematics) wikipedia , lookup

History of the function concept wikipedia , lookup

Factorial wikipedia , lookup

Function of several real variables wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Addition wikipedia , lookup

Transcript
Iterative Verfahren der Numerik
Prof. M. Grote / L. Gaudio
HS 2013
Universität Basel
Goal: review script-function definition, recursive function, local and global variables, how
to create meshes and 2d plots, working with sparse matrix.
Exercise 1. We want to compute the sum of all numbers between 1 and n . To do that
we define the sum all function. The sum all function takes a positive integer n, and is
defined as the sum of all numbers between 1 and n:
sum all(n) =
n
X
k.
k=1
1. Write the Matlab function [ sum ]=sum all1(n) that takes as input a positive integer n and computes sum all(n). This function should use a for loop. Moreover,
write a Matlab script call sum all1.m to test your function with several numbers
and verify if the sum is correct. Remember the exact value is n(n + 1)/2.
2. Now implement the same function using recursion. To do so, notice that:
sum all(n) = n + sum all(n − 1)( at least for n > 1).
Thus, sum all can be written as a function of itself. Use this fact to implement a recursive Matlab function sum all2.m and the corresponding script call sum all2.m
to compute sum all(n).
3. Create the following function:
function [ sum tot ] =total sum(n1,n2)
sum 1 = sum all1(n1);
sum 2 = sum all2(n2);
sum tot = sum 1+sum 2;
return
Write the script call total sum.m to test this function. The values sum 1 or
sum 2 are locally defined in the function total sum.m but not available in the
script call total sum.m or in the Matlab command. How can you access to these
values? Provide two possible ways without run again the two functions.
Exercise 2. The Fibonacci numbers are the numbers in the following sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence
relation: F1 = 0, F2 = 1, Fn = Fn−1 + Fn−2 ∀n > 2.
1. Write a Matlab function fibo1.m that takes a positive number and computes the
n-th Fibonacci number Fn . This function should use a for loop. The header of this
function should be: function [ fn ] = fibo1(n).
1
2. The n-th Fibonacci number can also be computed using recursive function. Note that
this recursion will have 2 base cases: F1 = 0 and F2 = 1. Use this fact to implement a
recursive Matlab function fibo2.m that computes the n-th Fibonacci number, Fn .
Exercise 3.
1. Create the matrix A using the following commands:
A=2*eye(500)−diag(ones(499,1),1)−diag(ones(499,1),−1);
Run the following commands to investigate the structure of A :
A(1:5,1:5), nnz(A), spy(A), spy(A(1:10,1:10));
What is the structure of A ? Calculate the inverse of A. What can you observe?
Calculate how long it takes to calculate A2 . Note the use of the commands tic and
toc to record the time of the operation.
2. Create the matrix B using the following commands. Note that A and B have the same
non-zero entries.
B=spdiags(ones(500,1)*[−1,2,−1],[−1,0,1],500,500);
The command spdiags creates a sparse matrix with the entries from the vector
(passed as the first variable) on the diagonal indicated by the second variable. These
variables can be passed individually or in groups as in the above example. The final
two variables represent the size of the matrix. Look at the Matlab help files for more
information on spdiags. Again look at the structure of B. Compare how the matrix
A and B are stored. Which uses less memory? Calculate how long it takes to calculate
B2.
3. Now, define the vector b=ones(500,1). Solve the system A x=b and B x = b
using the Matlab command \. Which is quicker? Use the command sparse to convert
the matrix A and compare again the two system solution.
Exercise 4.
1. Plot the following function z = sin(πx)cos(πy) in the domain (0, 1) × (2, 4). Make
use of the Matlab command meshgrid and surf.
2