SUNY Geneseo Department of Mathematics
Friday, February 3
Math 230 02
Spring 2017
Prof. Doug Baldwin
Has anyone tried the questions and suggestions survey?
Ideas or questions from the reading relevant to the following examples
+
to add, -
to subtract.*
, ./
, .^
Example: Create two equal-number-of-elements vectors of numbers, say v1 and v2, then write a Matlab statement that computes the elementwise averages of the numbers in v1 and v2 and puts the result in a third variable (e.g., if v1 = [1,2,3] and v2 = [2,4,6] then the average would end be [1.5, 3, 4.5])
v1 = [ 1, 2, 3, 4 ];
v2 = [ 10, 20, 30, -10 ];
v3 = ( v1 + v2 ) ./ 2
Example: Write a function that takes two arguments, a and b, representing the lengths of the legs of a right triangle, and that calculates the length of the hypotenuse. The function should work either on scalar or vector arguments.
function [ c ] = hyp( a, b )
%HYP Calculate the length of the hypotenuse of a right triangle
% whose other sides have lengths a and b
c = sqrt( a.^2 + b.^2 );
end
See handout for details of what to do.
See section 3.1 in our textbook.
Files that store a series of Matlab commands. Script files can be executed. (So scripts are like functions, except that they don’t have arguments or result variables.)
Scripts can be divided into independently executable sections by comments beginning with a double percent sign. Use the “Run Section,” “Run,” etc. buttons in the toolbar to run the section containing the cursor, run the whole script, etc.
Here are the commands that compute the elementwise average of two vectors in a script. This should be saved in a “.m” file, e.g., “vectoravg.m”:
%% Set up two vectors.
v1 = [ 1, 2, 3, 4 ];
v2 = [ 10, 20, 30, -10 ];
%% Calculate pairwise averages.
( v1 + v2 ) ./ 2
Lab day for vector lab