User Tools

Site Tools


gibson:teaching:spring-2016:math445:lab5

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== Math 445 lab 5: programming basics ====== Matlab vocabulary for this lab <code> for fprintf function mean std * length size rand randn </code> **Problem 1:** Write a **for** loop that will print statements of the form <code> 1 times pi is 3.1416 2 times pi is 6.2832 </code> for the numbers 1 through 10. ---- **Problem 2:** If $F$ is a temperature in Farenheit, then $C = \frac{5}{9}(F-32)$ is the same temperature in Celsius. Write a function ''farenheit2celsius'' that takes a Celsius temperature as input, converts it to Celsius, prints a statement of the form ''20 Farenheit is -6.6667 Celsius.'', and returns the Celsius value as its output. ---- **Problem 3:** Write a **for** loop that uses the above function to print a list of statements <code> -10 Farenheit is -23.3333 Celsius. 0 Farenheit is -17.7778 Celsius. </code> for Farenheit temperatures from -10 to 100, in steps of 10. Challenge: Get the text to line up nicely using field-width specifiers in fprintf! ---- **Problem 4:** Write a function ''average'' that takes an input vector $x$ and returns as output the average (mean) value of its elements, according to the formula \begin{equation*} \text{average}(x) = \frac{1}{N} \sum_{i=1}^N x_i \end{equation*} where $N$ is the number of elements in $x$. Test your function by comparing its output to the output of the built-in Matlab function **mean** on a random vector of length 100 whose elements are uniformly distributed between 0 and 1. ---- **Problem 5:** Write a function ''deviation'' that takes an input vector $x$ and returns the standard deviation of its elements, according to the formula \begin{equation*} \text{deviation}(x) = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \bar{X})^2} \end{equation*} where $\bar{x}$ is the average the elements of $x$, and $N$ is the number of elements in $x$. Test your function by comparing its output to the output of the built-in Matlab function **std** on a random vector of length 100 whose elements are in a normal (Gaussian) distribution about $x=0$. ---- **Problem 6:** The formula for matrix-vector multiplication $y = Ax$ is <latex> y_i = \sum_{j=1}^n A_{ij} x_j </latex> In this formula, $A$ is an $m \times n$ matrix, $x$ is an $n$-dimensional column vector, and $y$ is an $m$-dimensional column vector. Write a function ''matvecmult'' that takes a matrix $A$ and a vector $x$ as inputs, computes $y = Ax$ according to that formula, and returns the vector $y$. Compare your ''matvecmult'' to Matlab's built-in matrix-vector multiplication operator ''*'' on a random $4 \times 4$ matrix and a random 4d column vector. ...to be continued...

gibson/teaching/spring-2016/math445/lab5.1455650936.txt.gz · Last modified: 2016/02/16 11:28 by vining