Engr231Lab4

Engr231Lab4 - Drexel University - ENGR 231 Linear...

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

View Full Document Right Arrow Icon
Drexel University - ENGR 231 Linear Engineering Systems Copyright 2011, Drexel University Lab 4 Page 1 Lab 4: For Loops and Saving Data via Indexing Donald Bucci and Oleh Tretiak Goals: 1. Methods for automatically repeating a calculation while some parameters are changed. 2. Using for loops to store data. Prelab Exercise – Introduction to For Loops A for loop is a statement within a set of code that allows a user to repeat a set of actions for different values of a parameter. The general context is shown below and an example; for <index_variable_name> = [<element_1>, <element_2>, …, <element_N>] <command_1> <command_2> <command_N> end Ex 1: for k = [3, 2, 1, 7, 24] k % Display the value of k a = 2*k + 3 % Calculate and display the value of a end The subset code of Example 1 will produce the values shown the table below: Iteration Number Value of ‘k’ Value of ‘a’ 1 3 2*3+3 = 9 2 2 2*2+3 = 7 3 1 2*1+3 = 5 4 7 2*7+3 = 17 5 24 2*24+3 = 51 We see that the code is executed for each value of ‘k’, producing a corresponding value of ‘a’. The set of code is repeated for a total of five times. We could also execute the same loop, but for a different value of the index variable. Ex 2: for k = 0:0.5:3 k % Display the value of k a = 2*k + 3 % Calculate and display the value of a end
Background image of page 1

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

View Full DocumentRight Arrow Icon
Drexel University - ENGR 231 Linear Engineering Systems Copyright 2011, Drexel University Lab 4 Page 2 This subset of code will produce values of a for each value of k , as they are generated from the command, k = 0:0.5:3 , which is the same as the array k = [0, 0.5, 1, 1.5, 2, 2.5, 3] . This subset of code will produce the values shown in the following table: Iteration Number Value of ‘k’ Value of ‘a’ 1 0.0 2*0.0+3 = 3.0 2 0.5 2*0.5+3 = 4.0 3 1.0 2*1.0+3 = 5.0 4 1.5 2*1.5+3 = 6.0 5 2.0 2*2.0+3 = 7.0 6 2.5 2*2.5+3 = 8.0 7 3.0 2*3.0+3 = 9.0 Now suppose we wanted to extend Example 2 and plot the values of the coordinate pair (k,a) . Recall the basic syntax of the plot command: plot(<x_index>,<y_index>,<line_options>,. ..) where <x_index> and <y_index> specify the corresponding x and y values of the coordinate pair, and <line_options> describes the shape and color of the line and data points plotted (optional). These three inputs (or two if the <line_options> input is omitted) may be repeated to plot additional sets of coordinate pairs. For more information, type ‘help plot’ in the command window, or reference Lab 1. Combined with the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Engr231Lab4 - Drexel University - ENGR 231 Linear...

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

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