# Chapter 1: Introduction

Individuals employed in mechanical engineering careers attempt to solve engineering problems using the tools and information available to them. In situations where engineering needs to interpret, modify, or use data or other mathematical information efficiently the engineer may turn to a coding language to help them complete a task. A computer code functions by telling a computer a set of instructions created by the individual who wrote the code. In addition to running electronic devices around the world, computer code can be written by an engineer to compute mathematical operations, create a graphical depiction of data, or complete work much faster than the engineer would have been able to do by hand. By learning to code, engineers gain a useful tool to use when solving problems, studying data, or completing calculations within research or industry.

ME 160 at Iowa State introduces students to engineering problem-solving methods. Students will be introduced to the MATLAB program as a tool that can be used to solves problems engineers encounter within their education and their careers. To ensure students are best able to learn to code with MATLAB, this guide has been created as an additional reference that supplements the content being addressed by the instructor or other course texts. The first chapter of this text provides a general history and introduction to coding and the MATLAB program, providing background knowledge for students as they begin to explore the MATLAB program.

## The history of MATLAB

The original versions of the modern MATLAB program were developed not as a unique coding language but rather strictly as a tool to compute matrix operations. This is evident as its current name, “MATLAB”, is short for matrix laboratory. Initially derived in research papers by J.H. Wilkinson at the National Physical Laboratory, computerized matrix and eigenvalue calculating methods were developed in the late 1960s. Researchers at Argonne National Laboratory later created the software EISPACK and LINPACK, which completed matrix/eigensystem and linear equation calculations, respectively, based upon the coding language Fortran.

By the 1980s Dr. Cleve Moler and his colleagues wrote the programming language MATLAB to make existing programs including EISPACK and LINPACK accessible without needing to use the coding language Fortran. As MATLAB was introduced to Moler’s Stanford classes, Moler collaborated with Stanford graduate student Jack Little to commercialize the software. Since 1984 when the first commercial version of MATLAB debuted at the Institute of Electrical and Electronics Engineers’ Conference on Decision and Control, the program’s capabilities have been expanded to encompass usages in more applications. Significant mathematical topics which have been added to the MATLAB software include differential equations, an assortment of data collection types, and an improved user interface that includes customizable windows depicting everything the user needs to read, edit, and understand a MATLAB code.

## MATLAB’s role within ME 160

Within ME 160 you will utilize the MATLAB software to compute basic operations, solve algebraic and matrix operations, and utilize codes that will input data and modify equations based on parameters selected by the user. MATLAB should become a tool that the user feels comfortable using to execute computations and to create codes to solve user specifications. The remainder of this book will address the fundamentals necessary to write code in MATLAB, standard applications of the language, and more involved topics which conclude ME 160. At the end of this book and ME 160, the user should understand the MATLAB coding language enough to apply it to solve problems within their education, internships, or future careers.

The following list contains examples of situations where MATLAB can be used as a problem-solving tool when working with topics addressed in ME 160. While the list is not exhaustive, it demonstrates a series of skills that the user should have by the completion of ME 160.

• Calculating the values for the resultant of a force system.
• Determine value regarding materials such as sheer, strain, or margins of safety.
• Determine if boats made with different dimensions or materials would sink or float in various fluids.
• Interpreting, calculating, and displaying data sets.
• Development of applications with graphical user interfaces.
• Create repeatable and sharable methods for solving engineering problems.

## Accessing MATLAB

The MATLAB program is behind a paywall that requires users or the companies they work for to purchase the software. As students at Iowa State University, you have access to the program without paying out of pocket. To download the software as a student in the College of Engineering, search “Iowa State College of Engineering Installing MATLAB” on a search engine to access Iowa State University’s instructions. Following these instructions will direct you to MathWorks Iowa State webpage, where you should create an account using your Iowa State credentials.

After creating an account, read through the instructions on the Iowa State MATLAB installation webpage to download and install the program.

Another detail to consider when downloading MATLAB is the version you are downloading. MathWorks releases two versions of MATLAB each year, entitled MATLAB R20XXa or R20XXb, where 20XX corresponds with the year and a or b will denote the first or second version put out that year, respectively. This text was created after R2019b’s release, and most images of the software will be taken from this version. If you have a slightly older edition of the software, the most significant differences will be cosmetic modifications and should not cause issues within the scope of ME 160, though I do recommend trying to keep up to date with version updates.

In addition to making MATLAB a freely downloadable software for students, Iowa State University has provided the software on computers located within engineering computer labs. These labs are an excellent resource when students would like to work on MATLAB assignments with peers or would like to bypass the need of downloading software onto a personal device. These labs will update the software to the newest version, which should be the same version used in ME 160.

## Learning MATLAB

MATLAB is free and available on campus computers without requiring a download. Search for MATLAB in the start menu to locate the program. Note that it is good practice to ensure that the version of MATLAB available on campus computers is the same version used on any personal device. This can help prevent codes from not being compatible with a new/old version.

To use MATLAB within ME 160 you must become familiar with the layout of the user interface of the software. Upon opening the software in Windows, the user will encounter the following page, which is divided into several useful windows. Each window has been colored and labeled in the image below and is discussed in the following section. MATLAB enables the user to make modifications to the position of each window by selecting alternative layouts in the “Layout” button in the toolbar. Different layouts may be more convenient for different users or applications, so feel free to find the layout that is best while coding in ME 160.

### The Command Window

The command window (shown in blue) will serve as the primary interface the MATLAB user will interact with when writing and reading simple codes or individual operations. The command window can be used as an interface to type directly into MATLAB and as a place to view the results of these codes. As ME 160 progresses codes will be written in “script” files, which will generate a new window in what is currently shown as the top half of the command window. When running a script, a user will see outputs from the code displayed in the command window and can input data or other information using the command window. The shown example of a command window depicts several mathematical operations that will be addressed at the beginning of ME 160. The highlighted portions show what the user typed into the window, while the remainder is the output by MATLAB.

When the user is done viewing code or is restarting a program, the user must clear anything written in the command window. By using the clear command, `clc`, the user can reset the command window to its original blank state. For example, completing an arithmetic operation in MATLAB such as 238*5 will result in the window to the right.  Typing `clc` and pressing enter will clear the text in the window.

When writing scripts, you will want to start with a blank command window and reset any operations that the code may have carried out. This can be accomplished by writing two lines of code at the beginning of the script with `clc` and the` close all` command. This is demonstrated in the example script below. As a result of these two lines, the text in the command window will reset each time the code is run.

Programs and the Current Folder

Script program files can be saved and referenced later or transferred to other computers, unlike code written directly through the command window. Within ME 160, it will be essential to save all scripts to the same folder, which is referred to as the current folder in MATLAB. The current folder is shown directly below the toolbar and is highlighted in yellow in the above window. By saving your scripts to the same folder you will be able to keep everything in the same place and reference different files without changing folders. Script files, which are saved in .m format, are listed within the red programs section of the MATLAB user interface image above. When a .m file is selected, information about the file will be displayed in the details window, which is depicted in orange in the above image.

As you continue in ME 160 you will save many files in this section, which will quickly reveal the importance of careful file naming. Develop a habit of including the name of the program and possibly the date the file was created within the file name to ensure it will be able to be referenced in the future. When naming files in MATLAB, it is important to note that file names cannot include spaces, hyphens, dashes, or numbers as their first digit. To make file names readable, I recommend using underscores and capital letters to make quality file names (i.e. “QuadraticEquation9_23_2019” is much more informative than “untitled5” and will be easy to locate when needed again.

### The Editor

The editor window is the home of MATLAB script files- savable codes that can be shared and saved on a computer for use. MATLAB scripts are the files where all stand-alone programs that are written in ME 160 will be saved. When working in the command window every time an operation is typed and the enter key is pressed, the operation is completed by MATLAB. This fact means that codes are run step-by-step when written in the command folder. This is not the case for scripts within the editor. Codes are written by the user and only ran when commanded by the user by selecting the run button in the editor window of MATLAB.

Multiple scripts opened at the same time overlaid with tabs showing the name of each script. Like programs in Microsoft Windows, scripts can be dragged and dropped to be viewed next to each other or above and below each other, allowing for easier usage when working on multiple scripts.

The editor window also is home to expanded views of variables. When a variable from within the variables window is selected by double-clicking, a window will overlay the editor and any open scripts to show the contents of the variable. This may be a single value, a matrix or array, or a symbolic input. Users may shift back and forth between viewing the contents of variables and scripts using the tabs that appear at the top of the editor window.

### The Workspace

When writing scripts in MATLAB many variables will be introduced to represent data or to be used in formulas. To assist the user in determining which variables have been assigned in a script, the workspace will show each variable and the values that are assigned to them. This will be helpful when writing long codes using many similar variables and will help keep track of variable meanings.

Various forms of variables that exist within the MATLAB coding language are displayed within the Workspace. This includes numbers, characters, words, and more complicated double objects with arrays and matrices, and histograms. An example of a workspace for a code with several variable types is included below. Notice how each variable is assigned its own name by the user, allowing for it to be identified within the code.