Chapter 2: Basic Commands in MATLAB

Introduction

To get started writing code in MATLAB, several commands are essential to begin to operate the program. A command is a basic instruction that the user gives the program. These instructions, if given correctly, cause MATLAB to output a response such as printing a solution to a problem, executing a step in a larger process, or relaying information about the code. This chapter details several common commands which will frequently be used when writing scripts. The MATLAB user interface includes a command window designed specifically to facilitate the user executing simple commands or interacting with specific parts of larger codes, which will be addressed in the discussion of scripts. The command window and commands as a concept are fundamental concepts for coding. As a result, the following chapter has been written to instruct users on the variety, effects, and syntax required for command use in the broader context of MATLAB.

Help Resources

MATLAB is a complicated programming tool that contains many functions which can be used in an assortment of applications.

When writing code for ME 160, there are several ways for the user to learn more about functions and their operations while writing code. When the user knows the name of a command and is wanting to learn more about its function or how it should be written into the code, the user should use the help command. For example, to learn more about the if function, the user would time help if into the command window.

Many other resources exist outside of the program within both the MathWorks’ webpage and resources produced by third-party vendors. Appendix I of this text discusses many resources that are provided outside of MATLAB software itself. Specifically, resources provided within the documentation page are most valuable within the context of ME 160. The documentation page for a specific function can be reached by following the hyperlink at the bottom of the help page in MATLAB or by searching for the “MathWorks MATLAB Documentation” in a search engine.

As the reader is most likely just beginning to code in MATLAB, interactive tutorials offered on the MathWorks webpage may prove as valuable supplements to in-class work. Mathworks has provided many interactive MATLAB coding tutorials which enable the user to walk through exercises in a virtual “MATLAB Onramp” course. For more information about MathWorks interactive programs, refer to Appendix I at the end of this text.

Comments

Comments are a built-in way MATLAB has enabled users to make notes within a code without affecting the code’s function. To make a comment within a code, the user can type % at the beginning of a line of code. MATLAB will then ignore everything typed after that percent sign, enabling the user to type information regarding the function of the code or what a part of the code does. Comments are essential to writing efficient, long codes that can easily be read and edited by both the initial author and their peers. By placing many comments throughout codes in ME 160, the author will ensure that the code is easily read and edited by themselves, their peers, and the faculty grading final codes. Forming the habit of commenting often throughout a code is critical to ensuring that codes produced within ME 160 are useful and effective.

Comments have also been designed to help users who are editing code and would like to “turn off” a section of code. In the instance that a section of code within a MATLAB script does not work or should not be run, the user could place comments with % signs at the beginning of each line that should be ignored by the code. In order to speed up the process of “commenting out” large sections of code, MATLAB has created the notation %{ and %} which can be placed on different lines of code and will comment everything written in lines of code between the symbols. An example of standard comments and a block comment follows below. In the example, a portion of code that does not work correctly has been commented out and a note to correct the code has been made. Comments such as the one in line three are essential to ensure that codes are as legible as possible for all users.

Arithmetic in MATLAB

MATLAB is a well-versed tool for completing simple mathematical operations or solving algebraic equations. To complete these operations, the user must be aware of the notation that is required for the computer to understand what was input.

General Operations

To complete addition, subtraction, multiplication, or division the user must be precise with how they write their expressions. The included image depicts how to properly input operations. The first thing the user should be mindful of is the lack of an equal sign in mathematical operations. MATLAB uses single equal signs to assign a value to a variable, which is not what we want to do when solving an expression. Instead, press enter without typing an equal sign to tell MATLAB to solve the expression. Note that operations do not depend on if spaces are present between the numerical values and the operator. For multiplication and division note the use of an asterisk (*) and a forward slash (/) as their respective operators. Several operations can be done by inputting values with consideration of conventional order of operations rules (think the acronym PEMDAS for parenthesis, exponents, multiplication, division, addition, and subtraction if you forget).

Exponents and roots can be calculated in a similar manner in MATLAB. To compute an operation with an exponent, use a caret (^) followed by the value of the power. To compute roots, the same notation can be used with the root depicted as an exponent. For example, four squared could be written as 4^2 in MATLAB and the square root of four could be written as either 4^(1/2). The sqrt function can also be written using the sqrt() function, with all values which would be under the radical being contained within the parenthesis.

 

Calculating expressions using logarithms and exponentials are easy using MATLAB as well. To calculate a base 10 logarithm, use the function “log(x)” with x being the numerical value of the logarithm. In a similar manner, values taken to a power of e can be written using “exp(x)”, which would be input as “exp(4)” in MATLAB.

Trigonometry in MATLAB

MATLAB defaults to using radians to calculate degrees, which requires the user to be careful when entering angles to ensure the value is not calculated incorrectly. The functions degree() and radian() can be used to convert between an angle in degrees and an angle in radians. When computing trigonometry operations such as sin() or cos() in MATLAB, the alternative function sind(), cosd(), or tand() can be used to inform MATLAB that the value entered in the trigonometry operation is in degrees. This saves the user the need to convert between radians and degrees.

 

Complex Expressions

MATLAB can calculate complex expressions in the same manner as simple expressions. MATLAB, like any traditional calculator, calculates specifically based on what the user inputs. While expressions are input using the same formatting regardless of their complexity, it is increasingly important to consider the order of operations that the system will use as a result of parenthesis or the lack thereof placed by the user. To get a feel for typing complex expressions within MATLAB’s command window, review the following examples and reproduce them within MATLAB yourself.

Examples

The following examples demonstrate how to input complex expressions into the command window.

 

  1. [latex]\sqrt[4]{(23-\frac{3}{2})}[/latex]

  1. [latex]\left[\frac{10\text{exp}(3/2)}{\sqrt{23}}\right][/latex]

  1. [latex]\frac{-2+\sqrt{2^2-4(4)}}{2}[/latex]

  1. [latex]14\times\left(6.022\times{10}^{23}\right)[/latex]

  1. [latex]\left[\frac{3^{3/2}-\sqrt3}{8+(\frac{4}{13})}\right]^3[/latex]

 

 

Problems

Practice writing the following expressions in MATLAB. Be careful to use proper parenthesis and order of operations. For problem 5, show each equation being solved using MATLAB.

  1. [latex]e^{\left(\frac{13}{3}\right)}[/latex]

 

  1. [latex]\sqrt[3]{23+{1.5}^3}[/latex]

 

 

  1. [latex]\left[\frac{\sqrt{72}}{5^{\left(2/3 \right)}+13}\right][/latex]

 

 

  1. [latex]10\left(1-\left(\frac{.06}{.5}\right)^2\right)[/latex]

 

 

  1. The velocity of an object is represented by the following function:

[latex]V\left(t\right)=\sqrt[3]{4t^3+\frac{1}{2}t^2+2t+100}[/latex]

 

Using MATLAB, determine the velocity of the object at times t = 10 seconds, 50 seconds, and 100 seconds. (Note: While this problem may be more easily solved using a calculator, MATLAB could be used in the future to calculate the velocity at many different times. To create that code, the author would need to correctly input the function, which is what this problem exercises.)

 

 

 

  1. A titanium ball is dropped into a viscous fluid and slowly sinks to the bottom. A 5 mm diameter spherical ball is dropped into the liquid at a constant speed such that it takes 5 seconds for the ball to drop .15m. The density of titanium is 440[latex]\frac{kg}{m^3}[/latex] and the density of the fluid is 800 [latex]\frac{kg}{m^3}[/latex]. Determine the viscosity of the fluid using the following equations. Note that D is the sphere’s diameter, [latex]\rho[/latex] (rho) is density, [latex]g[/latex] is gravitational acceleration, (9.8 m/s), and [latex]\Delta h[/latex] is the change of the ball’s height.

[latex]V_{sphere}=\ \frac{\pi(D^3)}{6}[/latex]

[latex]Weight\ of\ Sphere=W=\rho_{sphere}g(\Delta h)[/latex]

[latex]Weight\ of\ Sphere=W=\rho_{sphere}g(\Delta h)[/latex]

[latex]Weight\ of\ Sphere=W=\rho_{sphere}g(\Delta h)[/latex]

[latex]Viscosity=\mu=\frac{F_D}{3\pi(D)(Velocity)}[/latex]

 

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

A Guide to MATLAB for ME 160 Copyright © 2022 by Austin Bray and Reza Montazami is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.