📖 Scenario: You are an engineering student learning how MATLAB helps solve real-world problems in science and engineering.
🎯 Goal: Build a simple MATLAB script that shows key reasons why MATLAB is popular in engineering and science.
📋 What You'll Learn
Create a cell array called reasons with 4 exact reasons why MATLAB is used
Create a variable called count to store the number of reasons
Use a for loop with variable i to print each reason with its number
Print the total number of reasons at the end
💡 Why This Matters
🌍 Real World
Engineers and scientists use MATLAB to quickly analyze data, perform calculations, and visualize results in many fields like robotics, aerospace, and biology.
💼 Career
Knowing MATLAB helps you solve technical problems efficiently and is a valuable skill for jobs in engineering, research, and data analysis.
Progress0 / 4 steps
1
Create the list of reasons
Create a cell array called reasons with these exact entries: 'Easy matrix operations', 'Built-in scientific functions', 'Visualization tools', 'Wide engineering toolbox'.
MATLAB
Hint
Use curly braces {} to create a cell array of strings in MATLAB.
2
Count the number of reasons
Create a variable called count and set it to the number of elements in reasons using the length function.
MATLAB
Hint
Use length(reasons) to find how many items are in the cell array.
3
Print each reason with a number
Use a for loop with variable i from 1 to count to print each reason preceded by its number and a dot. Use fprintf with format '%d. %s\n'.
MATLAB
Hint
Use for i = 1:count and fprintf to print each reason with its number.
4
Print the total number of reasons
Print the total number of reasons using fprintf with the exact text: 'Total reasons: %d\n' and the variable count.
MATLAB
Hint
Use fprintf('Total reasons: %d\n', count); to print the total count.
Practice
(1/5)
1. Why is MATLAB commonly used by engineers and scientists?
easy
A. Because it simplifies math and data tasks
B. Because it is a web development tool
C. Because it is mainly used for writing novels
D. Because it only works on mobile devices
Solution
Step 1: Understand MATLAB's main purpose
MATLAB is designed to help with math, data analysis, and simulations.
Step 2: Match the purpose with the options
Only Because it simplifies math and data tasks mentions simplifying math and data tasks, which fits MATLAB's use.
Final Answer:
Because it simplifies math and data tasks -> Option A
Quick Check:
MATLAB purpose = Simplify math and data [OK]
Hint: Think about MATLAB's main use in math and data [OK]
Common Mistakes:
Confusing MATLAB with web or mobile tools
Thinking MATLAB is for writing text or novels
2. Which of the following is the correct way to create a vector in MATLAB?
easy
A. v = {1 2 3 4};
B. v = (1 2 3 4);
C. v = [1, 2, 3, 4];
D. v = <1 2 3 4>;
Solution
Step 1: Recall MATLAB vector syntax
Vectors are created using square brackets with spaces or commas between elements.
Step 2: Check each option
v = [1, 2, 3, 4]; uses square brackets and commas correctly; others use invalid symbols.
Final Answer:
v = [1, 2, 3, 4]; -> Option C
Quick Check:
Vector syntax = square brackets [OK]
Hint: Remember vectors use square brackets in MATLAB [OK]
Common Mistakes:
Using parentheses instead of brackets
Using curly braces which create cell arrays
Using angle brackets which are invalid
3. What will be the output of this MATLAB code?
t = 0:0.5:2; y = t.^2; disp(y);
medium
A. [0 0.25 1 2.25 4]
B. [0 0.5 1 1.5 2]
C. [0 0.25 0.5 0.75 1]
D. [0 1 4 9 16]
Solution
Step 1: Understand the vector t
t = 0:0.5:2 creates [0 0.5 1 1.5 2].
Step 2: Calculate y = t.^2 element-wise
Squaring each element: 0^2=0, 0.5^2=0.25, 1^2=1, 1.5^2=2.25, 2^2=4.
Final Answer:
[0 0.25 1 2.25 4] -> Option A
Quick Check:
Element-wise square = correct output [OK]
Hint: Remember .^ squares each element, not matrix power [OK]
Common Mistakes:
Confusing element-wise power with matrix power
Misunderstanding the colon operator range
Incorrectly calculating squares
4. Identify the error in this MATLAB code snippet: