Introduction to Programming Concepts with MATLAB
Front Cover
Information
License
Dedication
Thoughts
Author Bios
Preface
Contact Information
Acknowledgements
What is New With This Edition
A Note to Students
A Note to Instructors
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.1 – MATLAB Introduction
Learning Objectives
What is MATLAB?
What is MATLAB used for in engineering and science?
How can I get MATLAB onto my computer?
Are there any free alternatives to MATLAB?
Where can I find more information and help with MATLAB online?
Multiple Choice Quiz
Problem Set
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.2 – Hello World
Learning Objectives
Where can I find and open the MATLAB program?
Step 1: Create a New m-file
Step 2: Write the ‘Hello World’ Code
Example 1
Step 3: Run the Program
Step 4: Make Your Program a Little Fancier
Example 2
Multiple Choice Quiz
Problem Set
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.3 – MATLAB Environment
Learning Objectives
MATLAB Environment Windows and Parts
Navigation Ribbon
Working Folder Location
Current Folder
Command Window
Editor Window
Workspace
Status Bar
Multiple Choice Quiz
Problem Set
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.4 – Changing MATLAB Preferences
Learning Objectives
How can I change the window layout in MATLAB?
Changing Basic User Preferences
Multiple Choice Quiz
Problem Set
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.5 – The m-file
Learning Objectives
What is an m-file?
How do I save my m-file?
How do I input variables and expressions into the m-file?
How do I run the m-file?
What are the clc and clear commands?
How can I place comments in my m-file?
Example 1
Can I separate my code into parts within the m-file?
What does the color highlighting in the m-file mean?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.6 – The Command Window
Learning Objectives
What is the Command Window and how can I use it?
How can I suppress outputs in the Command Window?
Example 1
Can I view help from the Command Window?
Can the Command Window do it all?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 1: INITIAL SETUP AND BASIC OPERATION
Lesson 1.7 – Publishing an m-file
Learning Objectives
What does publishing do?
How can I publish in MATLAB?
How can I get a PDF file of my published code?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.1 – Variables and Naming Rules
Learning Objectives
What is a mathematical variable?
What is a programming variable?
How can I give my results a variable name of their own?
Example 1
What are some possible problems with naming an expression?
Example 2
Are there benefits to good practices for variable naming?
Are there some guidelines for variable naming that I can follow?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.2 – Characters and Strings
Learning Objectives
What is a character?
Example 1
What is a string?
Example 2
What makes characters/strings special?
Example 3
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.3 – Working with Strings
Learning Objectives
How do I join two strings together?
Example 1
How do I search and count strings?
Example 2
How do I make a whole string lower or upper case?
Example 3
Can I split a string into its component pieces?
Example 4
Example 5
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.4 – Inputs and Outputs
Learning Objectives
How can I get input from the user?
How do I display notes in the Command Window?
Example 1
Example 2
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.5 – Data Types
Learning Objectives
What is a data type?
What are the MATLAB data types?
Why are data types important?
How do I check the data type of a variable?
Example 1
Example 2
Can I convert between data types?
Example 3
Example 4
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.6 – Vectors and Matrices
Learning Objectives
Why is the program called MATLAB?
What is a matrix?
Do I need to know any special types of matrices?
What is a vector?
How do I define a vector or a matrix in MATLAB?
Example 1
What are some basic functions and commands for matrix manipulation?
Example 2
Example 3
How can I reference strings as vectors?
Example 4
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 2: BASIC PROGRAMMING FUNDAMENTALS
Lesson 2.7 – How to Debug Code
Learning Objectives
What is an error?
What is a warning?
How can I solve the problem with my code?
Can I pause my program part of the way through?
What if I cannot find the exact place of the error?
Multiple Choice Quiz
Problem Set
Module 3: PLOTTING
Lesson 3.1 – Plots and Figures
Learning Objectives
How can I visualize (plot) data in MATLAB?
How do I plot data pairs (points) in MATLAB?
Example 1
How do I plot a function in MATLAB?
Example 2
What is the difference between a figure and a plot?
How can I enter nonlinear functions for plotting?
What are some possible errors with plotting?
How do I show multiple data sets on the same plot?
Example 3
What are some other types of plots that MATLAB can generate?
Example 4
How do I plot on more than one figure in the same m-file?
What is the close command?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 3: PLOTTING
Lesson 3.2 – Plot Formatting
Learning Objectives
How can my MATLAB graph look nicer?
What are some terms I should know for plots?
How can I change the color and style of lines and markers on a plot?
How can I make the function and points on the graph look nicer?
Example 1
How can I put a title and axis labels on my plot?
How can I add a legend to my plot?
How can I add a grid to my graph?
Example 2
How can I add special characters in my axis labels and title?
How can I change axis limits and tick labels?
Example 3
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 3: PLOTTING
Lesson 3.3 – Advanced Plotting
Learning Objectives
Does MATLAB have more plotting capabilities?
How can I create a bar graph?
Example 1
How can I create a 3D line plot?
Example 2
How can I create a 3D surface plot?
Example 3
How can I create a polar plot?
Example 4
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.1: Basic Algebra, Logarithms, and Trigonometry
Learning Objectives
What kind of mathematical functions and operations are available in MATLAB?
How do I use logarithmic functions in MATLAB?
Example 1
What about a logarithm that is not natural?
Example 2
How can MATLAB evaluate trigonometric functions?
Example 3
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.2: Symbolic Variables
Learning Objectives
What is a symbolic variable?
What is a MATLAB toolbox?
How do I use symbolic variables?
Example 1
How do I clear specific variables?
How can I convert from syms data type to other data types?
Example 2
Can I replace a symbolic variable with a value?
Example 3
How can I change the output format of syms?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.3: Solution of Linear and Nonlinear Equations
Learning Objectives
How do I solve for roots of a linear equation?
Example 1
What is a nonlinear equation?
How can I use MATLAB to solve nonlinear equations?
Example 2
Example 3
Is there a faster way to work with polynomial equations in MATLAB?
Example 4
Can I plot with symbolic variables?
Example 5
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.4: Differential Calculus
Learning Objectives
What is a derivative?
How do I take the derivative of a function in MATLAB?
Example 1
Example 2
Where are derivatives used in engineering?
Example 3
How do I find the derivative of a discrete function in MATLAB?
Example 4
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.5: Integral Calculus
Learning Objectives
What is integration?
How does MATLAB conduct symbolic integration?
Example 1
Can MATLAB do numerical integration of discrete functions?
Example 2
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.6 – Linear Algebra
Learning Objectives
What is linear algebra?
How do I add and subtract matrices?
Can I use math functions like
sin()
on matrices?
Example 1
How do I perform matrix multiplication?
What is the difference between matrix and array operations?
Example 2
How do I take the inverse of a matrix?
Example 3
Can MATLAB do advanced matrix and vector operations?
Example 4
How can I solve systems of equations with MATLAB?
Example 5
Example 6
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.7 – Curve Fitting
Learning Objectives
What is curve fitting?
What is interpolation?
How can I interpolate data in MATLAB?
Example 1
What is spline interpolation?
How do I conduct spline interpolation?
Example 2
What is regression?
How do I do regression in MATLAB?
Example 3
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.8 – Curve Fitting: Plotting the Results
Learning Objectives
How can I plot the results of curve fitting?
Example 1
Example 2
What are some common mistakes when plotting curve fitting results?
Multiple Choice Quiz
Problem Set
Module 4: MATH AND DATA ANALYSIS
Lesson 4.9 – Ordinary Differential Equations
Learning Objectives
What is a differential equation?
How do I set up and solve a differential equation?
Example 1
How do I solve a higher order ODE?
Example 2
What are the limitations of using the
dsolve()
function?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 5: CONDITIONAL STATEMENTS
Lesson 5.1 – Conditions and Boolean Logic
Learning Objectives
What are conditions?
Example 1
What is Boolean logic?
Example 2
Can different data types be identified in MATLAB?
Example 3
How can I round numbers in MATLAB?
Example 4
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 5: CONDITIONAL STATEMENTS
Lesson 5.2 – Conditional Statements: if and if-else
Learning Objectives
What is a conditional statement?
What is the if statement?
Example 1
What is the if-else statement?
Example 2
Can I use multiple conditions in a single expression?
Example 3
Example 4
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 5: CONDITIONAL STATEMENTS
Lesson 5.3 – Conditional Statements: if-elseif
Learning Objectives
What is the if-elseif statement?
Example 1
Example 2
Independent vs. Dependent Cases
Example 3
Example 4
What is the if-elseif-else statement?
Example 5
What is the difference between the else and elseif conditional clauses?
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
References:
Module 6: PROGRAM DESIGN AND COMMUNICATION
Lesson 6.1 – Flowcharts
Learning Objectives
What is a flowchart?
Example 1
Example 2
Example 3
Multiple Choice Quiz
Problem Set
Module 6: PROGRAM DESIGN AND COMMUNICATION
Lesson 6.2 – Pseudocode
Learning Objectives
What is a pseudocode?
How are pseudocodes used?
Example 1
How can I convert a pseudocode for a problem into a program?
Example 2
Multiple Choice Quiz
Problem Set
Module 6: PROGRAM DESIGN AND COMMUNICATION
Lesson 6.3 – Writing Better Code
Learning Objectives
How can I improve my code for computational efficiency?
How does hardcoding impact a program?
What are some tips for good comments and spacing?
Why does proper code indenting matter?
What are some tips for choosing inputs and outputs?
What are some tips for thinking ahead in when designing my program?
Multiple Choice Quiz
Problem Set
Module 7: FUNCTIONS
Lesson 7.1 – User-Defined Functions
Learning Objectives
What is a function?
What are the naming rules for functions in MATLAB?
How can I create functions in MATLAB?
Example 1a
Example 1b
Example 2a
Example 2b
Can I define functions in the program m-file?
Example 3
Example 4a
Example 4b
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 7: FUNCTIONS
Lesson 7.2 – Function Design and Communication
Learning Objectives
How can I add a description for my function?
Example 1
How can I define errors and warnings inside my function?
Example 2
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 8: Loops
Lesson 8.1 – while Loops
Learning Objectives
What is a loop?
What is a while loop?
Example 1
Example 2
Example 3
Example 4
Example 5
Lesson Summary
Multiple Choice Quiz
Problem Set
Module 8: Loops
Lesson 8.2 – for Loops
Learning Objectives
What is a for loop?
Example 1
How can I reference vectors inside of a loop?
Example 3
Example 4
Do I have to use the loop counter variable in the body of the loop?
Example 5
When do I use a for loop vs. a while loop?
Example 6
Example 7
Lesson Summary
Multiple Choice Quiz
Problem Set
Module 8: Loops
Lesson 8.3 – break and continue Commands
What are the
break
and
continue
commands?
How does the
break
command work in MATLAB?
Example 1
How does the continue command work in MATLAB?
Example 2
Lesson Summary
Multiple Choice Quiz
Problem Set
Module 8: Loops
Lesson 8.4 – Nested Loops
Lesson Objectives
What is a nested loop?
How do nested loops work?
Example 1
How do loop mechanics apply to nested loops?
Example 2
What is a “flag”?
When should I use programming flags?
Example 3
Example 4
How can I use
break
and
continue
in nested loops?
Example 5
Multiple Choice Quiz
Problem Set
Module 8: Loops
Lesson 8.5 – Working with Matrices and Loops
Learning Objectives
How can I reference matrices in a loop?
Example 1
Example 2
How do I store values in a matrix using a loop?
Example 3
How can I access specific areas of a matrix?
Example 4
Example 5
Example 6
What is vectorization?
How can I vectorize matrix operations in MATLAB
Example 7
Example 8
What are some tips I can use for vectorization?
Multiple Choice Quiz
Problem Set
Module 8: Loops
Lesson 8.6 – Applied Loops
Learning Objectives
Why is this lesson important?
How can I sort an array?
Example 1
Example 2
How can I find the sum of a vector?
Example 3
How can I plot different variations of a function using loops?
Example 4
Multiple Choice Quiz
Problem Set
Module 9: READING FROM AND WRITING TO FILES
Lesson 9.1 – Reading from Files
Learning Objectives
Why read data from a file?
How do I read numeric-only data from files?
Example 1
What is a delimiter?
How can I read numeric and character data from files?
Example 2
Example 3
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 9: READING FROM AND WRITING TO FILES
Lesson 9.2 – Writing to Files
Lesson Objectives
How can I write numeric data to files with MATLAB?
Example 1
How can I write non-numeric data to files with MATLAB?
Example 2
Example 3
Lesson Summary of New Syntax and Programming Tools
Multiple Choice Quiz
Problem Set
Module 9: READING FROM AND WRITING TO FILES
Lesson 9.3 – Navigating Directories in MATLAB
Lesson Objectives
How do I set the current working directory for MATLAB?
Example 1
How can I loop through the contents of a directory?
Example 2
How can I create a new folder (directory) with MATLAB?
Example 3
Lesson Summary
Multiple Choice Quiz
Problem Set
Published with bookdown
Introduction to Programming Concepts with MATLAB
Information
Authors: Autar Kaw, Benajmin Rigsby, Daniel Miller, Ismet Handzic