First MATLAB program - Time & Space Complexity
When we write our first MATLAB program, it is helpful to see how the time it takes to run changes as we give it more work.
We want to know how the program's running time grows when the input size gets bigger.
Analyze the time complexity of the following code snippet.
n = 10;
total = 0;
for i = 1:n
total = total + i;
end
disp(total);
This code adds up all numbers from 1 to n and shows the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that adds each number to total.
- How many times: It runs exactly n times, once for each number from 1 to n.
As n gets bigger, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the size of the input.
[X] Wrong: "The loop runs faster because it just adds numbers."
[OK] Correct: Even simple additions take time, and since the loop runs n times, the total time grows with n.
Understanding how loops affect running time is a key skill. It helps you explain your code clearly and shows you can think about efficiency.
"What if we changed the loop to run from 1 to n squared? How would the time complexity change?"
Practice
disp('Hello, world!') do?Solution
Step 1: Understand the disp function
Thedispfunction in MATLAB shows text or values on the screen.Step 2: Analyze the input to disp
The input is a string 'Hello, world!' which means it will print this exact text.Final Answer:
It displays the text Hello, world! on the screen. -> Option BQuick Check:
disp('text') shows text [OK]
- Thinking disp saves text to a file
- Using double quotes instead of single quotes
- Confusing disp with variable creation
Solution
Step 1: Check the correct function name
MATLAB usesdispto display messages, notdisplayorprint.Step 2: Check the string syntax
Strings must be inside single quotes in MATLAB, sodisp('Hello')is correct.Final Answer:
disp('Hello') -> Option AQuick Check:
disp with single quotes is correct [OK]
- Using double quotes instead of single quotes
- Using wrong function names like print or display
- Missing quotes around text
disp('Welcome to MATLAB!')Solution
Step 1: Understand disp function behavior
Thedispfunction prints the string inside the parentheses to the screen.Step 2: Check the string format
The string is correctly enclosed in single quotes, so it will print exactly as given.Final Answer:
Welcome to MATLAB! -> Option AQuick Check:
disp prints string text [OK]
- Expecting disp to print the command itself
- Using double quotes causing errors
- Forgetting to use disp function
disp(Welcome to MATLAB!)
Solution
Step 1: Check the input to disp
The text Welcome to MATLAB! is not inside single quotes, so MATLAB treats it as variables or commands.Step 2: Understand string syntax in MATLAB
Strings must be enclosed in single quotes to be recognized as text.Final Answer:
Text is not inside single quotes -> Option DQuick Check:
Strings need single quotes [OK]
- Omitting quotes around text
- Thinking semicolon is required for disp
- Believing disp is not a function
Helloand
Welcome to MATLABWhich code will do this correctly?
Solution
Step 1: Check how to display multiple lines
Using two separatedispcalls prints each line separately.Step 2: Analyze each option
disp('Hello') disp('Welcome to MATLAB') uses two disp calls with correct strings. disp('Hello\nWelcome to MATLAB') tries to use \n inside single quotes which MATLAB does not interpret as newline. disp('Hello') disp(Welcome to MATLAB) misses quotes in second disp. disp('Hello' + 'Welcome to MATLAB') tries to add strings which is invalid in MATLAB.Final Answer:
disp('Hello') disp('Welcome to MATLAB') -> Option CQuick Check:
Use separate disp calls for multiple lines [OK]
- Using \n inside single quotes expecting newline
- Missing quotes around text
- Trying to add strings with + operator
Ready to prove it?
Pass the test and this pattern moves unknown
