← Home · Community College · Engineering Dept

Programming & Problem Solving
in MATLAB

You'll use MATLAB the way engineers actually use it — to model real systems, run real experiments, and check your answers against real data. Along the way, you'll uncover why computers sometimes get arithmetic "wrong," and what that means for the systems we trust with our lives.

ENGR 11 18 Lessons 3 Units ELSP Project No Exams · Portfolio
MATLAB R2024b
% What does this course teach you?
% Let's ask MATLAB itself.

course = struct();
course.unit1 = 'MATLAB basics';
course.unit2 = 'Build a circuit model';
course.unit3 = 'How numbers work inside';

% The big question we answer:
disp('Can computers do perfect math?')

>> ans = 'No. Here is why.'
UNIT 1 OF 3

Introduction to MATLAB

You'll learn the MATLAB environment by actually using it — not by reading about it. Each lesson starts with a problem that you genuinely can't solve yet. Then we build the tool you need to solve it.

What You Already Know (Before Lesson 1)

You don't need to know any MATLAB. But pause for a moment and think about what you do know:

Research on learning shows that connecting new ideas to what you already know is the fastest path to deep understanding (Ambrose et al., 2010). That's why we start here.

LESSON 1
The MATLAB Desktop
The Problem First

You need to solve 500 equations. Writing them out by hand will take hours. What if there were a workspace that did the bookkeeping for you?

You'll orient yourself in MATLAB's four-panel workspace. You'll run your first commands in the Command Window, save variables in the Workspace, and write reusable scripts in the Editor. You'll learn why MATLAB uses a semicolon to suppress output — and what happens when you forget it.
Try in Command Window
3 + 4 * 2 % what do you get?
x = 10; % the ; hides output
x % now show it
Check your understanding
  • What is the Command Window for? What is the Editor for?
  • Why would you ever want to suppress output with `;`?
  • Where does MATLAB store the value of a variable between commands?
  • What happens if you close MATLAB? Do your variables survive?
Used In
  • Circuit simulation
  • Signal processing
  • Robotics control
LESSON 2
Create Arrays
The Problem First

You need to store the voltage measurements from 1,000 sensors. Making 1,000 individual variables is absurd. What's the better way?

Arrays are MATLAB's core data structure — everything is a matrix. You'll create row vectors, column vectors, and 2D matrices. You'll use the colon operator (1:10) to generate sequences and linspace to make evenly-spaced grids. You'll see why element-wise operations (.*) are different from matrix operations (*).
Try It
t = 0:0.1:2; % time values
v = t.^2; % voltage squared
plot(t, v) % visualize it
Check your understanding
  • What's the difference between A * B and A .* B?
  • How would you create a list of 100 evenly spaced values between 0 and 1?
  • Why does MATLAB treat everything as a matrix, even a single number?
  • Can you have a matrix where each row has a different length? Why or why not?
Assumption to Notice

When you use linspace(0,1,100), you're assuming the quantity you're modeling is continuous. Is that always true? What if it's discrete?

LESSON 3
Logical Data
The Problem First

You have sensor readings from a circuit. You only want the ones above a safety threshold. How do you filter 10,000 numbers down to just the ones that matter?

Computers think in true/false. In MATLAB, every comparison returns a logical array of 1s and 0s. You'll use logical indexing — one of MATLAB's most powerful features — to select, filter, and replace data without writing a single loop. This is not a shortcut; it's the professional approach.
Filtering Without a Loop
readings = [2.1, 5.3, 1.8, 6.0, 4.4];
safe = readings(readings > 3.0);
% safe = [5.3, 6.0, 4.4]
Check your understanding
  • What does readings > 3.0 return on its own?
  • Why is logical indexing faster than a for-loop for large datasets?
  • How would you replace all values below 0 with 0 (clipping)?
  • What is the difference between & (element-wise AND) and && (short-circuit AND)?
Used In
  • Data cleaning
  • Fault detection
  • Image processing
LESSON 4
Control Flow
The Problem First

Your code needs to behave differently depending on the situation — apply one formula for a resistor, a different one for a capacitor. How do you teach a program to make decisions?

You'll write if/elseif/else blocks that branch on conditions, for loops that repeat over arrays, and while loops that keep going until a condition changes. The key insight: loops are powerful but slow. By the end of Unit 1, you'll know when to use a loop — and when to avoid one entirely.
Decision Making
if voltage > 5.0
component = 'capacitor';
elseif voltage > 2.0
component = 'resistor';
else
component = 'check wiring';
end
Check your understanding
  • When does a while loop make more sense than a for loop?
  • What happens if a while loop condition is never false? What do you do?
  • Why is a vectorized solution (using arrays) usually faster than a loop?
  • Write a loop that computes the sum of 1 + 2 + 3 + ... + 100. Then do it in one line without a loop.
LESSON 5
Function Files
The Problem First

You've written the same resistance calculation three times in three different scripts. When you find a mistake, you'll have to fix it in three places. There's a better way.

Functions let you write code once and reuse it everywhere. You'll write your own .m function files, pass in inputs, and get outputs back. You'll learn about local scope — why a variable inside a function can't accidentally overwrite one outside. Functions are the building blocks of every large engineering project.
Your First Function (resistance.m)
function R = resistance(V, I)
% Ohm's Law: V = IR, so R = V/I
R = V / I;
end

% Call it from Command Window:
R = resistance(12, 0.5);
Check your understanding
  • What does it mean for a variable to be "local" to a function?
  • How do you make a function return more than one output?
  • What's the difference between a script and a function?
  • Why is it good practice to add a comment explaining what your function does?
Used In
  • Every serious codebase
  • Libraries & toolboxes
  • Team collaboration
LESSON 6
Program Design
The Problem First

You've got a big project coming. How do you break a complex problem into pieces you can actually code — and then put them back together?

Good programs don't happen by accident. You'll practice top-down decomposition — starting from the big goal and breaking it into smaller functions. You'll learn to test each piece separately before combining them. This is how professional engineers avoid the nightmare of debugging a 500-line script that doesn't work for an unknown reason.
📋Pseudocode
📊Flowchart
💻MATLAB code
🧪Test cases
📝Written explanation

For each solution you build, try representing it in at least 3 of these 5 forms. This deepens your understanding significantly.

Check your understanding
  • What does "decomposition" mean in software design?
  • How do you know if a function is small enough to test easily?
  • What would a good test case for your resistance() function look like?
  • Describe a real problem you care about. How would you break it into functions?
UNIT 2 OF 3 · PROJECT

Build a Real Circuit Model

The Electrify Linear-Systems Problem (ELSP) is the centerpiece of the course. You'll build a physical resistor circuit, measure voltages and currents yourself, model it as a linear system, solve it with MATLAB, and compare your computed answers against your measured data.

THE ELSP PROJECT

From Ohm's Law to Linear Algebra

A circuit is just a system of equations. Kirchhoff's laws give you the equations; MATLAB solves them. But here's what makes this project real: you'll collect the measured data yourself, and you'll find out whether your model is right.

This is the verification principle in action — you can check your own work without asking the instructor if your answer is correct. The circuit tells you.

01Build a resistor network using components you can hold in your hand
02Measure voltage and current at each branch with a multimeter
03Write down Kirchhoff's laws as a matrix equation Ax = b
04Solve with MATLAB using x = A\b
05Compare computed values to measured values — how close are they?
06Investigate the discrepancy. Where did error come from?
Assumptions We're Making — Discuss These

Resistors follow Ohm's Law exactly. Temperature doesn't change resistance. Wires have zero resistance. Measurement instruments are perfect. Which of these is most likely to be wrong? How could you test that?

This Project Uses Iteration — Not Submission
BuildMake the circuit
MeasureCollect data
ModelWrite equations
SolveUse MATLAB
CompareModel vs. data
ReviseFix & repeat

Modeling is not a one-shot activity. Engineers revise their models many times. Your first attempt is supposed to be imperfect. That's the point.

What You'll Need from Unit 1
  • Arrays — to store multiple voltages and currents
  • Matrix operations — to set up Ax = b
  • Functions — to organize your solution code
  • Plotting — to visualize measured vs. computed
PROJECT LESSON 1
Build the Circuit
Assemble a resistor network on a breadboard. Label every node. Draw a schematic by hand — not as a formality, but because drawing forces you to understand the topology.
Think about this
  • What is Kirchhoff's Current Law in plain English?
  • How many independent equations does an n-node circuit produce?
PROJECT LESSON 2
Collect Measurements
Use a multimeter to measure voltage at every node and current through every branch. Record everything in a MATLAB script. This is your ground truth — everything else gets compared to this.
Think about this
  • How precise is your multimeter? What is its uncertainty?
  • If you measure the same voltage twice and get different results, what does that tell you?
PROJECT LESSON 3
Set Up & Solve the System
Translate Kirchhoff's laws into a matrix equation Ax = b. Solve with MATLAB's backslash operator. Then write a function that automates this for any circuit of the same topology.
Think about this
  • What does it mean for a matrix to be singular? What does that mean physically for the circuit?
  • Why is A\b preferred over inv(A)*b in MATLAB?
PROJECT LESSON 4
Compare & Reflect
Plot measured vs. computed values on the same graph. Calculate the error for each branch. Write a paragraph in your Live Script explaining what you see. Where is the model good? Where does it break down?
Model Validity

Your model will not be perfect. That's expected. The question to answer: is the error small enough to be useful? For what applications would this model be "good enough"?

PROJECT LESSON 5
Extend the Model
Add more resistors to make your circuit more complex. Does the model still hold? Try adding a non-resistive element (a capacitor or LED). What breaks? Why? This is the "high ceiling" of the project — you can keep going indefinitely.
Think about this
  • What physical assumptions does the linear model make that would break with non-linear components?
  • How would you model a circuit with both DC and AC components?
PROJECT LESSON 6
Portfolio Write-Up
Write a clear, honest account of your project in your Learning Portfolio. Include: what you built, what worked, what didn't, what surprised you, and what you'd do differently. The goal is reflection, not polish.
This Is Real Engineering
  • Model → Test → Revise is how products get built
  • Documenting failures is valued in industry
UNIT 3 OF 3

How Computers Think About Numbers

This unit answers a question most programmers never ask: how does a computer actually store the number 0.1? The answer is stranger — and more important — than you think. If you write code that involves money, navigation, or medical data, you need to know this.

What You Already Know

In this unit, you'll understand exactly why that last one happens — and how to protect your code from it.

UNIT 3 · LESSON 1
Unsigned Integers & Binary
The Problem First

You have 8 light switches. Each is either ON (1) or OFF (0). How many different combinations can you make? And what numbers can you represent that way?

Everything in a computer is binary — 0s and 1s. You'll learn to convert between decimal and binary by hand, understand what "8-bit" means, and see why a uint8 variable can only hold values 0–255. You'll also see what happens when you go one higher than 255 — overflow — and why that matters.
Overflow in Action
x = uint8(255); % maximum for uint8
y = x + uint8(1);
disp(y) % What do you expect?
ans = 255 % Not 256. Why?
Check your understanding
  • Convert 42 to binary by hand. Then verify with MATLAB: dec2bin(42)
  • What is the largest number an 8-bit unsigned integer can hold?
  • Name a real situation where overflow in embedded code has caused a safety problem.
Used In
  • Embedded systems
  • Image data (0–255)
  • Network protocols
UNIT 3 · LESSON 2
Signed Integers & Two's Complement
The Problem First

You need to represent negative numbers in binary. You can't just add a minus sign — computers only have 0s and 1s. How do you encode the idea of "negative"?

Two's complement is the clever trick computers use to represent negative numbers without wasting a bit on a sign symbol. You'll learn the conversion rule by hand, understand why int8 goes from −128 to +127 (not −127 to +127), and see how subtraction becomes addition in binary — which is why it's such an elegant solution.
Signed vs Unsigned
int8(127) + int8(1) % what happens?
ans = 127 % Saturates, doesn't wrap
typecast(int8(-1), 'uint8')
ans = 255 % Same bits, different meaning
Check your understanding
  • Convert −5 to 8-bit two's complement by hand.
  • Why does two's complement make addition hardware simpler?
  • Why is the range −128 to +127 asymmetric (127 ≠ 128)?
UNIT 3 · LESSON 3
Fixed-Point Numbers
The Problem First

Integers can't represent 3.14. Floating-point numbers are slow on cheap hardware. What do you do when you need decimals but you're programming a small sensor with limited compute power?

Fixed-point arithmetic places the decimal point at a fixed location in the bit pattern. It's faster than floating-point, uses less memory, and is totally predictable. You'll see how MATLAB's ufixed data type works, understand the trade-off between range and precision, and learn why GPS chips and audio processors often use fixed-point math.
The Key Assumption in Fixed-Point

You're assuming your numbers will always stay within a known range. If they don't, you get overflow. Is that assumption safe for the application you're building?

Check your understanding
  • What is the trade-off between integer bits and fractional bits in a fixed-point format?
  • Why is fixed-point preferred over floating-point in real-time control systems?
  • Give an example of an application where fixed-point arithmetic would cause a serious problem if not designed carefully.
Used In
  • DSP chips
  • Motor controllers
  • IoT sensors
UNIT 3 · LESSON 4
Floating-Point Numbers
The Problem First

Try this in MATLAB: 0.1 + 0.2 == 0.3. You'll get false. Why? And should you be worried?

Floating-point is like scientific notation in binary: a mantissa and an exponent, both stored in binary. Most decimal fractions can't be stored exactly — so the computer rounds to the nearest representable value. You'll learn to measure and account for this rounding error, and why testing with == on floating-point numbers is almost always a mistake.
The Classic Surprise
0.1 + 0.2 == 0.3 % false!
abs((0.1+0.2) - 0.3) < eps % true

% eps is machine epsilon
% the smallest number where 1+eps ≠ 1
Check your understanding
  • Why can't 0.1 be stored exactly in binary floating-point?
  • What is machine epsilon (eps) and what does it measure?
  • How should you test if two floating-point numbers are "equal"?
  • Find a real-world example where floating-point error caused a serious bug.
Used In
  • All numerical software
  • Finance (be careful!)
  • Scientific computing
UNIT 3 · LESSON 5
The IEEE 754 Standard
The Problem First

Before 1985, every computer manufacturer stored floating-point numbers differently. Code from one machine wouldn't give the same answers on another. Scientists couldn't reproduce each other's results. How did we fix this?

IEEE 754 is the international standard that defines exactly how floating-point numbers are stored and computed. You'll learn the bit layout of a 64-bit double (1 sign bit, 11 exponent bits, 52 mantissa bits), understand special values like Inf, -Inf, and NaN, and use an online IEEE 754 calculator to explore edge cases. This is one of the most important technical standards in computing history.
Special Values
1/0 % Inf (not an error!)
-1/0 % -Inf
0/0 % NaN (Not a Number)
isnan(0/0) % true
isinf(1/0) % true
Check your understanding
  • How many bits does a double-precision float use for the exponent? For the mantissa?
  • What does NaN mean and when does it appear?
  • Why is NaN != NaN evaluating to true not a bug, but a feature?
  • What's the largest finite number a double can represent?
Used In
  • All modern CPUs
  • GPU computation
  • Numerical libraries
UNIT 3 · LESSON 6
Precision, Accuracy & Numerical Analysis
The Problem First

You've built a model that gives an answer. But how wrong could that answer be? And does the error grow or shrink as your computation gets bigger?

Precision and accuracy are different things. A clock that always reads 12:00 is precise (consistent) but not accurate (not right). You'll measure absolute error, relative error, and rounding error in MATLAB computations. You'll also see numerical instability — cases where tiny initial errors grow catastrophically through repeated calculation. This is the foundation of numerical analysis.
Investigate This Yourself
Compute the sum 1/1 + 1/2 + 1/3 + ... + 1/1000000 in two ways:
Forward: start from 1/1 and add small terms last
Backward: start from 1/1000000 and add large terms last
Do you get the same answer? If not, which is more accurate and why?
Check your understanding
  • What is the difference between absolute error and relative error?
  • What makes a numerical algorithm "stable" vs "unstable"?
  • How does Lloyd Trefethen define numerical analysis? Why is that definition interesting?
  • If your ELSP project gave an error of 0.05 volts, is that good or bad? (What does it depend on?)
Used In
  • Finite element analysis
  • Climate modeling
  • Machine learning

How This Course Fits Together

MATLAB Basics
Arrays & Logic
Functions
Circuit Model (ELSP)
Verify vs. Data
Binary
Integers
Fixed-Point
Floating-Point
Error Analysis

How to Get the Most Out of This Course

These strategies come from research on how people actually learn — not from myth or habit.

🤔

Ask "Why?" before "How?"

Every lesson starts with a headache — a problem you can't yet solve. Sit with that discomfort before reaching for the solution. The frustration is productive; it means your brain is building a connection.

🔄

Retrieve, Don't Reread

After you finish a lesson, close your notes and write down everything you remember. This feels harder than rereading, but research consistently shows it produces stronger, longer-lasting memory (Roediger & Karpicke, 2006).

🧪

Break Your Code on Purpose

Take working code and deliberately change something to see what breaks. Ask: "what if I remove the semicolon? What if this array is empty?" Breaking things teaches you the boundaries of your understanding.

✏️

Write Before You Code

Pseudocode and flowcharts aren't busywork — they're thinking on paper. Students who write out their plan first write better code faster, because they've already solved the logic problem before the syntax gets in the way.

🔗

Connect Every Lesson to Something You Care About

Motivation isn't something you wait to feel — it's something you build. After each lesson, write one sentence: "This connects to [something in my life] because..." The more specific, the more powerful.

📋

Use Your Portfolio as a Mirror, Not an Archive

Your Learning Portfolio isn't a scrapbook of completed work. It's a record of your thinking over time. Include what confused you, what you tried that didn't work, and what changed your mind. That's what real learning looks like.

Learning Milestones

These aren't grades — they're descriptions of what you can do. Use them to understand where you are and where you're headed.

🌱

MATLAB User

You can navigate the MATLAB desktop, create arrays, use logical indexing, write control flow, and call functions. You can solve a small problem from scratch in a Live Script.

Circuit Modeler

You built a physical circuit, collected your own data, set up a linear system, solved it with MATLAB, and compared computed to measured. You explained the discrepancy in writing.

🔢

Numerical Thinker

You can explain why 0.1 + 0.2 ≠ 0.3 in binary. You understand overflow, two's complement, fixed-point trade-offs, and IEEE 754. You can measure and reason about floating-point error in your own code.

ENGR 11 · Programming & Problem Solving in MATLAB · All resources free
← Teaching Computing Differently · henry@henryfan.org
ENGR 11