Dec 04, 2025 | 421 words | 4 min read
15.1.2. Task 0#
Learning Objectives#
Translate fundamental programming concepts from Python to MATLAB.
Practice writing arithmetic expressions, initializing vectors and matrices, and using loops.
Understand key differences in syntax, such as
inputfunction behavior, array indexing, and loop structure.
Task Instructions#
This assignment is designed to be a quick transition from Python to MATLAB. You’ll work through the following parts: Part 1: Basic Calculations, Part 2: Vectors & Matrices, and Part 3: Loops.
Part 1: Basic Calculations#
Make a copy of the MATLAB template file
ENGR133_MATLAB_Template.mand rename the file to ma1_pre_0_username.m.Make sure to fill out all header information, including a short description of the code.
In the
INITIALIZATIONsection, hardcode three variables:a = 5,b = 1, andc = 2.Note
While MATLAB has an
inputfunction, we’ll hardcode values for this assignment to make publishing your code easier.In the
CALCULATIONSsection, compute the following three expressions, assigning each result to a new variable. Be mindful of the order of operations and remember that trigonometric functions in MATLAB expect radians.a. \(a \cdot b^4 + \tan(c)\)
b. \(\left( \frac{\pi}{c} - b! \right) + \frac{a}{15}\)
c. \(b^5 + \frac{c}{4} + \sin^{-1}(1)\)
In the
OUTPUTSsection, print the results of your calculations to the command window. Use thefprintffunction to display each result rounded to three decimal places.Hint
The format specifier for this is
%.3f.
Part 2: Vectors & Matrices#
Create a new section in your script called
VECTORS & MATRICES.Initialize a row vector
v1 = [1, 17, 4]and a \(3 \times 3\) matrixm1 = [4, 6, 19; 3, 52, 6; 7, 9, 14].Modify the vector
v1by replacing its second element with the value \(8\).Note
Remember that MATLAB uses 1-based indexing, not 0-based like Python.
Modify the matrix
m1by replacing its first row with the updated vectorv1.Ensure both
v1andm1are displayed in the command window.
Part 3: Loops#
Create additional
INITIALIZATIONandCALCULATIONSsections for bothwhileandforloop.In
WHILE LOOP INITIALIZATION, initialize the variablexand set it equal to \(0\). This variable will be used for calculations and as a counter variable in thewhileloop. The purpose of a counter variable is to determine the number of times a loop runs before ending.In
WHILE LOOP CALCULATIONS, create awhileloop that computes the following equation until \(x \le 10\):\[y = (x * 4) + 5\]In the
whileloop, print the value of \(x\) and \(y\) after performing each calculation. MAKE SURE to include a line within thewhileloop that increments the value ofxby one before each calculation to prevent the code from getting stuck in an infinite loop.In
FOR LOOP INITIALIZATION, initialize the following row vectork, which is a 5-element row vector with equally spaced values ranging from \(1\) to \(10\).Hint
Use the
linspacefunction to create this vectorIn
FOR LOOP CALCULATION, begin constructing theforloop. Using appropriate MATLAB syntax, create aforloop that performs the following calculation for every value in vectork. Assign the value in the variable namedelement.\[s = \text{element} * 2\]Print every value of
safter performing the calculation using a singlefprintfstatement within theforloop.
Sample Output#
Sample Output
>> ma1_pre_0_username The result to the first calculation is 2.815 The result to the second calculation is 0.904 The result to the third calculation is 3.071
v1 =
1 8 4
m1 =
1 8 4 3 52 6 7 9 14
The value of x = 1. The value of y = 9. The value of x = 2. The value of y = 13. The value of x = 3. The value of y = 17. The value of x = 4. The value of y = 21. The value of x = 5. The value of y = 25. The value of x = 6. The value of y = 29. The value of x = 7. The value of y = 33. The value of x = 8. The value of y = 37. The value of x = 9. The value of y = 41. The value of x = 10. The value of y = 45. The value of x = 11. The value of y = 49. The value of s = 2.0 The value of s = 6.5 The value of s = 11.0 The value of s = 15.5 The value of s = 20.0
Deliverables |
Description |
|---|---|
ma1_pre_0_username.m |
Your completed MATLAB code. |
ma1_pre_0_pub_username.pdf |
PDF with published MATLAB code |
ma1_pre_0_username.pdf |
Flowchart(s) for this task. |