advertisement

David Gitz ECE 355 Lab 2 11-September-07 Introduction: This lab was designed to introduce the Matlab script environment, how to write a simple Matlab routine, a simple Matlab function, and how to manage the function file in a Matlab environment. Exercise 1 Objective: Create a function to find the average value of an input vector. Implementation: %Filename: average.m %Author: David Gitz %Date Created: 10-September-07 %Purpose: Takes a vector input and returns the average value of the %vector. function output = average(input) output = sum(input)/length(input); Application: >> x = 0:.1:10; >> average(x) ans = 5 Exercise 2 Objective: Create a function to calculate the maximum value of an input vector. Implementation: %Filename: max.m %Author: David Gitz %Date Created: 10-September-07 %Purpose: Takes a vector input and returns the maximum value of the %vector. function ouptut = peak(input) tmp = 0; for i = 1:length(input) if input(i)>tmp tmp = input(i) end end Application: >> x = 0:.1:10; >> max(x) ans = 10 Exercise 3 Objective: Create a function to process 3 input signals. Implementation: %Filename: process.m %Author: David Gitz %Date Created: 10-September-07 %Purpose: Takes 4 input vectors and processes them according to the matrix: %[1 0 1; 0 1 2] and returns 2 outputs corresponding to this. function [output1,output2] = process(x1, x2, x3, t) output1 = x1 +x3; output2 = x2 +(2*x3); plot(t,output1,t,output2); Application: >> >> >> >> >> t = 0:.01:10; x1 = sin(2*pi*t); x2 = sin(2*pi*2*t); x3 = sin(2*pi*4*t); process(x1,x2,x3, t); Conclusion: This lab illustrates the scripting environment in Matlab, and using functions to provide multiple inputs and outputs.