me 1020 engineering programming with matlab chapter 1 in...

27
ME 1020 Engineering Programming with MATLAB Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16 Topics Covered: Use MATLAB as a calculator Save files to folders and open files from folders Create row vector arrays Create plots Open MATLAB. Several windows are of importance. Use the Command Window to type in the following in order to solve Problem 1.1(a):

Upload: others

Post on 01-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

ME 1020 Engineering Programming with MATLAB

Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Topics Covered:

Use MATLAB as a calculator

Save files to folders and open files from folders

Create row vector arrays

Create plots

Open MATLAB.

Several windows are of importance. Use the Command Window to type in the following in order to solve

Problem 1.1(a):

Page 2: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

The output of the calculation appears in the Command Window. The semi-colon (;) suppresses output to the

Command Window. The Workspace Window shows the names of the Variables. The Command History

Window shows the entered commands. Previous commands can be accessed quickly by using the Up Arrow

and Down Arrow. Try this!

Clear the Command Window by typing clc into the Command Window.

Page 3: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Remove the variable u from the Workspace by typing the command clear u in the Command Window.

Page 4: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Remove all of the variables from the Workspace by typing the command clear in the Command Window.

Clear the Command History Window by right-clicking the Command History Window Banner and selecting

Clear Command History.

Page 5: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16
Page 6: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

In the previous example, the equals sign (=) is called the Assignment or Replacement Operator. Type in the

following session to demonstrate that the Assignment Operator is different than the traditional equals sign in

mathematics.

The second command line that was typed in is shown in the Command History Window. It states the

following:

“Replace variable x with the current contents of variable x plus 1.”

In the third command line, the caret symbol (^) denotes exponentiation. Use MATLAB to solve Problem 1.3(a)

as shown below:

Page 7: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Another method of computation is to create a Script File. Use the Home/New Script tab to create a new Script

File:

Page 8: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

The new window that has appeared is called the Editor/Debugger. Use the Editor in MATLAB to solve Problem

1.3(a) by typing the following and pushing the green Run Button.

Page 9: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

The Save As Window appears, indicating that the Script File must be saved prior to execution. Save the Script

File onto the Desktop as shown below. The extension for MATLAB Script Files is (*.m).

Page 10: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16
Page 11: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Press the Change Folder Button. When you do this, the Current Folder Window shows the location of the file

and the contents of the folder that the Script File was just saved to.

Page 12: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16
Page 13: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Once the Script File is saved, the results of the computation again appear in the Command Window. Close the

MATLAB program by clicking on the red X’s in the upper right-hand of the Editor Window and the main

MATLAB window. Reopen MATLAB. Go to the Desktop using the File Open Button, and double-click onto the

just-created Problem1_3a.m file:

Page 14: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16
Page 15: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Make the following change in the Script File and save the program using the Save Button under the Editor

drop-down menu prior to running the program.

Page 16: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

In the modified Script File, the built-in variable pi was used. Other built-in functions exist, as shown in the

following session where Problem 1.16(a) is to be solved.

Create a new Script File using the New File Button on the Editor. MATLAB gives the new file the temporary

name Untitled2.

Page 17: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Save it to the Desktop using the Save As drop-down menu as shown below:

Page 18: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Notice that you can toggle back and forth between the two saved files by clicking on the names in the Editor

Window. Try it!

Page 19: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Change the Current Folder to the Desktop by searching with the File Path Window:

Page 20: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Notice that the two new Script Files in the Desktop Folder can be seen in the Current Folder Window.

Page 21: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Create a new Script File to solve Problem 1.13 as shown below:

The command disp(‘X’) is used to display text as part of the output of the program onto the Command

Window. The variable x is set up using square brackets as follows:

x = [start:step:stop]

which creates a Row Vector Array. The y Row Vector Array is evaluated in line 4, where the equation for y is

evaluated for each of the x values. The number of columns in this particular row vector is m = 21, which was

determined by using the command length(y) as shown in the Workspace Window. Modify the Script File to

plot y versus x:

Page 22: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

The “choppiness” of the graph can be reduced by increasing the number of data points that are calculated.

Modify the Script File by changing the step size to 0.02.

Page 23: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Further modify this Script File to provide the user the opportunity to input the step size by entering it directly

in the Command Window as the Script File runs. Use semi-colons (;) to suppress the output of the x and y row

vectors to the Command Window.

Page 24: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Notice that the y graph is even smoother with this smaller value of step size.

Go to the Publish tab.

Page 25: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Go to the Edit Publishing Options drop-down menu.

Change the Output File Format from html to doc, then press Close.

Now Publish the Script file.

Page 26: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

The output can be copied and pasted in MS Word. Both the Script file and the output is provided.

%Prob. 1-13

x=1:0.2:5

y=7*sin(4*x)

l=length(y)

third_element=y(3)

x =

Columns 1 through 7

1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000

Columns 8 through 14

2.4000 2.6000 2.8000 3.0000 3.2000 3.4000 3.6000

Columns 15 through 21

3.8000 4.0000 4.2000 4.4000 4.6000 4.8000 5.0000

y =

Columns 1 through 7

-5.2976 -6.9732 -4.4189 0.8158 5.5557 6.9255 4.0944

Columns 8 through 14

-1.2203 -5.7948 -6.8542 -3.7560 1.6206 6.0141 6.7596

Page 27: ME 1020 Engineering Programming with MATLAB Chapter 1 In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap01a.pdf · Chapter 1 In-Class Assignment (a): 1.1, 1.3, 1.13, 1.16

Columns 15 through 21

3.4048 -2.0153 -6.2130 -6.6419 -3.0420 2.4032 6.3906

l =

21

third_element =

-4.4189

Published with MATLAB® R2012b