%% Worksheet 1: The Basics
% This homework is designed to teach you to think in terms of matrices and vectors 
% because this is how Matlab organizes data. You will find that complicated operations 
% can often be done with one or two lines of code if you use appropriate functions 
% and have the data stored in an appropriate structure. The other purpose of this 
% homework is to make you comfortable with using help to learn about new functions.
%
% Look at the corresponding pdf-file for some math formulas.
%% 1.
% Make the following variables
% 
% a.) a = 10
% 
% b.) b = 2.5 x 10^{23}
% 
% c.) c = 2 + 3i , where i is the imaginary number
% 
% d.) d =   $e^{\frac{2}{3}\pi\cdot i }$
% 
% (Use exp,i,pi)


%% 2.
%  a) aVec = [ 3.14 15 9 26 ]
% 
%  b) bVec = column([2.71 8 28 182]) % column is not a Matlab function.
% 
%  c) cVec= [ 5 4.8 ... −4.8 −5 ] % (all the numbers from 5 to -5 with increments 
% of -0.2)                  
% 
%  d) dVec = [10^0 10^0.01 ... 10^0.99 10^1]  logarithmically spaced numbers 
% between 1 and 10.                               
% 
%  e) eVec = 'Hello there' (eVec is a string, which is a vector of characters)


%% 3.
% Make the following variables:
% 
% a)A = 9x9-matrix filled with values 2.  Use *ones* or *zeros*
% 
% b) B= 9x9-matrix with zeros except [1 2 3 4 5 4 3 2 1]  on the main diagonal. 
% Use diag (or zeros and linear indexing of type 1:k:81)
% 
% c) a 10x10-matrix, where the vector 1:100 runs down the columns. Use reshape
% 
% 
% 
% d) $D = \left[ \begin{array}{cccc}                               NaN&NaN&NaN&NaN\\                              
% NaN&NaN&NaN&NaN\\                            NaN&NaN&NaN&NaN                                                                                 
% \end{array} \right ]$
% 
% e) E=[13 -1 5;-22 10 -87]
% 
% f) F = a 5x3-matrix of random integers with values on the range. (use rand, 
% ceil, floor)


%% 4.
% a) Given vector x, evaluate vector y defined by the formula.
% 
% $$y=\frac{1}{\sqrt{2\pi}}e^{-x^2/2}$$
% 
% Let x be the interval [-1,1] divided into 10 pieces of equal length and 
% make a plot of y vs. x
% 
% Hint: remember to use elementwise operations where needed, i.e. .* and 
% ./ etc.


%% 5.
% The Taylor expansion of $e^x$ is:
% 
% $$\sum_{k=0}^{\infty}\frac{x^k}{k!}$$
% 
% Observe the convergence of the sum with the help of cumsum  and exp


%% 6. 
% Solve the system of linear equations 
% 
% $$\begin{cases}2x+y = 3 \\x-2y = -1\end{cases}$$
% 
% Check the result.

A = [2 1; 1 -1]; b = [3;-1];
x = A\b
%% 
% Using the same technique, solve the system and check.
% 
% $$\begin{cases}    35 x_1      +14x_3  +  16x_4   +  2x_5  = 67\\    27x_1    
% +  7 x_2  + 14x_3  +   4x_4  +  7x_5 =  45 \\   -13 x_1    -2  x_2 +  6 x_3  
% + 10  x_4+   8x_5 = 9 \\    30 x_1    -1  x_2 -12  x_3  + 7  x_4 -11x_5 = 13 
% \\     7 x_1   + 14 x_2+    7x_3    -3 x_4  -10x_5 = 15\end{cases}$$


%% 7.
% Using Matlab indexing, compute the perimeter sum of the matrix "magic(8)".
% 
% Perimeter sum adds together the elements that are in the first and last 
% rows and columns of the matrix. Try to make your code independent of the matrix 
% dimensions using "end".


%% 8.
% a) Plot the graph of f(x) = sin(x) on interval [0,1]
% 
% b) Plot the graph of the function $f(x)=\frac{1}{4}x \sin(x) $ on interval 
% [0,40] and in the same picture, plot lines $g_1(x) = \frac{1}{4}x $ and $g_2(x) 
% = -\frac{1}{4}x$.
% 
% c) Plot a curve with y  coordinate of $\sin(t)$ and  x coordinate of $\cos(t)$ 
% when $t \in [0,2 \pi]$.
% 
% d) Plot a curve 
% 
% $$\begin{cases}x = \sin(t) \left( e^{\cos(t)}- 2\cos(4t) - \sin\left(\frac{t}{12}\right) 
% \right) \\y = \cos(t) \left( e^{\cos(t)}- 2\cos(4t) - \sin\left(\frac{t}{12}\right) 
% \right)\end{cases}$$
% 
% Test different intervals for t; you'll probably need a fairly long interval. 
% 
% In some cases the picture may look nicer using axis square, axis equal


%% 9. 
% Suppose we have 
% 
% $$f(x) = x - e^{-x^2}$$
% 
% and
% 
% $$f'(x) = 1+2xe^{-x^2}$$
% 
% Use Newton's method (teachers and Google will help if you've forgotten 
% what it is) and for or while loop to find the root of $f(x)$.