class5b_matlab_activities_engr195%28II%29s10

class5b_matlab_activities_engr195%28II%29s10 - ENGR 195(II)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 195(II) – Spring 2010 Activity 1 Class 5b Activity 1 – User-Defined Function (Estimated Time = 20 minutes) You will write a user-defined function that calculates the average of a vector. o The input argument for the function will be a vector. o The output argument will be the average of the vector. o The code will calculate the average using the SUM() and LENGTH() functions. A. Open function_header_template.m in the MATLAB editor and re-save as vector_avg.m. This will become the user-defined function vector_avg . 1. On the first line, before the help lines provided in the function_header_template.m file, write the function definition. The general format is: function [output variables] = function_name(input variables) for example: function [average_value] = vector_avg(my_vector) 2. Immediately following the function definition, edit the comment lines that describe authorship and help a user learn how to implement your user-defined function. You should clearly list all required input and output arguments with comments that explain each. Use the following format: % FUNCTION NAME Very brief purpose of function on this line % INPUTS: List them below one line per input argument % % OUTPUTS: List them below one line per output argument % Save your function file and check that you have provided proper help comments by typing help vector_avg at the MATLAB prompt. (Your identification and date information and all help information should be echoed in the command workspace.) 3. Following the comments, write the code necessary to calculate the average of a vector using the sum and length commands. Remember that you must assign your output argument within the function code. for example:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

class5b_matlab_activities_engr195%28II%29s10 - ENGR 195(II)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online