# channelflow.org

### Site Tools

gibson:teaching:spring-2016:math445:lab5

# Math 445 lab 5: programming basics

Matlab vocabulary for this lab

  for
fprintf
function
mean
std
*
length
size
rand
randn

Problem 1: Write a for loop that will print statements of the form

1 times pi is 3.1416
2 times pi is 6.2832

for the numbers 1 through 10.

Problem 2: If is a temperature in Farenheit, then is the same temperature in Celsius. Write a function farenheit2celsius that takes a Farenheit 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

-10 Farenheit is -23.3333 Celsius.
0 Farenheit is -17.7778 Celsius.

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 and returns as output the average (mean) value of its elements, according to the formula

where is the number of elements in . 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 and returns the standard deviation of its elements, according to the formula

where is the average the elements of , and is the number of elements in . 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 .

Problem 6: The formula for matrix-vector multiplication is

In this formula, is an matrix, is an -dimensional column vector, and is an -dimensional column vector.

Write a function matvecmult that takes a matrix and a vector as inputs, computes according to that formula, and returns the vector . Compare your matvecmult to Matlab's built-in matrix-vector multiplication operator * on a random matrix and a random 4d column vector.

Problem 7: The formula for matrix-matrix multiplication of an matrix and an, matrix is

The product is an matrix.

Write a function matmatmult that takes a matrix and a vector as inputs, computes according to that formula, and returns the matrix . Compare your matmatmult to Matlab's built-in matrix-vector multiplication operator * on a random matrix and a random matrix B.