# Chapter 4: Writing Scripts

## Variables

Variables are critical pieces of MATLAB codes that users can assign values to when writing scripts. This chapter will address uses of variables to solve algebraic operations and to analyze data. The following image shows an example of the format for creating a variable. In this example, the variable’s name is the letter ’n’ and the variable’s value is 6.02e23. A variable has value since MATLAB will know that everywhere in a script where ‘n’ is typed the value 6.02e23 is transferred. This functionality at its simplest form enables users to keep code clean by writing variables instead of a number repeatedly. Importantly, if the user changes the value of the variable where it is initially defined in a code, then the value changes everywhere else in a code.

Variable named “n” being assigned the value of $6.02{\times10}^{23}$.

Example of variables being assigned in the command window.

### Variable Operations

Once variables have values assigned to them, they become useful tools for numeric operations, and data analysis, and will be critical in functions, which will be addressed next. The following

images show examples of how variables can be used in numeric and matrix applications. Once variables have been assigned numeric values, the user can view the workspace window to check the value assigned to each variable. This feature is valuable for long codes with many variables, as the user is able to check the value for each variable and keep track of which variable names have been used.

When creating equations within MATLAB codes, values assigned to variables can be checked to verify if equations are operating how the user intended them to. For example, the user can manually calculate the expected values generated by an equation and can check actual values displayed in the workspace to ensure that a code functions as intended by the user.

## Examples

1. Define two variables a and b which are assigned the values 12 and 3, respectively. Create a code that outputs the values for a divided by b, a multiplied by b, and a to the b power. Assign the outputs the values x, y, and z, respectively.

Note that the answer displayed in the command window only depicts values that were not suppressed in the code by semicolons at the end of a line.

2. The Pythagorean theorem can be used to determine the length of the right triangle’s sides. Write a MATLAB script that computes the hypotenuse for a right triangle with short sides x = 3 and y = 5. The most useful form of the Pythagorean theorem is provided.

$Hypotenuse=\ \sqrt{x^2+y^2}$

## Problems

1. The following equation can be used to calculate the distance that an object will free fall over some amount of time. Assuming that the object starts at zero velocity and has no air resistance, create a code that can calculate the total distance traveled by an item in free fall for a given gravitational acceleration, $g\ (\frac{meters}{{second}^2}),$ and time, $t\ (seconds).$

$∆h= 1/2 gt^2$

2. An engineer would like to easily calculate the volume and surface area of a classroom in a building. Create a script file within MATLAB that can calculate the volume of the room and the surface area of wall #1, wall #2, and the floor, as labeled in the image, by assigning values to variables x, y, and z, and completing arithmetic operations with them. Use comments to label each part of the script.

3. A scientist working with high-power electron microscopes would like to convert units from micrometers and nanometers to meters so students can understand how small these units are. Create a MATLAB script that converts a variable with units in meters to micrometers $(μm)$ and nanometers $(nm)$. The necessary conversion factors are provided below. Use comments to label your code.

$10^6 μm=1 m$

$10^9 nm=1 m$