This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Drexel University  ENGR 231 Linear Engineering Systems Copyright 2011 Drexel University Page 1 Lab 3: MATLAB™ Scripts and Linear Solution Sets Oleh Tretiak and DJ Bucci Goals for Lab 3. The aims of this lab are to learn: 1. Scripts, mfile cells, and publishing 2. Solution set of a linear system. 3. Visualization of the solution set. Prelab Exercise: Scripts, cells, and publishing. Last week we saw how to solve problems by using MATLAB scripts. This week we will learn some additional features that allow us to write scripts more conveniently, to test them, and to communicate the solutions. This is through a method known as cell mode scripting . To introduce this, we present the mfile of Ex 1 as shown below: Ex 1: %% A plot that demonstrates a trigonometric identity %% Problem statement. % We wish to demonstrate the identity sin(x)^2 + cos(x)^2 = 1 % by plotting the sine, cosine, and the sum of their squares over the % interval 0 to 2pi. %% Define the Domain (XPoints) % Recalling how plot works, we first need to generate a set of x values, % or rather the domain over which we are going to plot. We do this using the % linspace function. npts = 40; % Number of plot points ang = linspace(0, 2*pi, npts); % The domain, or rather x values %% Compute the sines and cosines and plot them % From here, we can take the sine and cosine of the domain generated above. cosval = cos(ang); sinval = sin(ang); %% % Now we want to form sin(ang)^2 + cos(ang)^2 from the values of the % sine(ang) and cos(ang) values generated from above. If correct, all of the % data points should equal 1. v2 = cosval.^2 + sinval.^2; %% % Notice that we use .^2 instead of ^2 to generate the points. We’ll learn why this is so in a later lab. %% Plot and label all of the figures % Plotting and labeling everything yields the following result. We see that % sum of those squared values end up all being equal to 1, thus the identity! plot(ang, cosval, 'b' , ang, sinval, 'g' , ang, v2, 'rx' ) title( 'A wellknown trig identity, sin^2 + cos^2 = 1' ) xlabel( 'theta, radians' ) legend( 'cosine' , 'sine' , 'sum of squares' ) Drexel University  ENGR 231 Linear Engineering Systems Copyright 2011 Drexel University Page 2 Ex 1 is an example of a cell mode script that. Cell mode scripts work the same as normal scripts, except for the following properties: 1. The file consists of 7 cells. Each cell is started by two percent characters followed by a space. 2. Each cell can be executed on its own. This is done by clicking anywhere within the cell (it will then become highlighted) and then clicking the "evaluate current cell" icon, Cells are useful when one is writing a script, since one can check whether some code is valid without executing other code. For example, when we click the fourth cell, we see a plot, and some newly defined variables in the workspace. If we wanted to change the plot from red to green we could edit the plot line and just execute the fourth cell again....
View
Full Document
 Fall '10
 OlehTretiak
 Trigonometry, Linear Engineering Systems

Click to edit the document details