Mar 14, 2025 | 422 words | 4 min read
14.1.2. Task 0#
Learning Objectives#
Practice writing simple arithmetic expressions in MATLAB. Practice writing simple vectors and matrices in MATLAB.
Task Instructions#
In this task, you will be writing simple arithmetic expressions and learning how to initialize vectors and matrices in MATLAB.
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, prompt the user to initialize the variables,a,b, andcto \(5\), \(1\), and \(2\) using the built-ininputfunction.Note
In MATLAB, the
inputfunction retrieves user input and assumes it is numeric by default, meaning it directly interprets the input as an integer or float. For example, usingx = input('Enter a number: ')expects a number. In contrast, Python’sinputfunction always returns user input as astring, regardless of the content, requiring explicit conversion to numeric types (likeint()orfloat()) afterward.In the
CALCULATIONsection create three variables that compute the following three equations in MATLAB using appropriate syntax and function calls where needed (Be careful with order of operations! Make sure that all trigonometric functions are computed in radians.):a. \(a*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 to the command window rounded to three decimal places using thefprintfin-built function.Hint
To round to three decimal places in MATLAB within an
fprintf()statement, use the syntax%.3fwhen referencing the variable you want to format.Create a new section called
VECTOR/MATRIX INITIALIZATION, in which,initialize a row vector
v1 = [1, 17, 4]andinitialize a \(3\) x \(3\) matrix
m1 = [4, 6, 19; 3, 52, 6; 7, 9, 14].
Create another section called
VECTOR/MATRIX CALCULATIONsection, in which,replace the second value in
v1with a value of \(8\) andreplace the first row in matrix
m1with the row vectorv1.
Allow both vector
v1and matrixm1to be shown in the command window.Publish your script as ma1_pre_0_pub_username.pdf.
Refer to How to Publish in MATLAB video.
Note
For publishing, all user
inputstatements must be commented out, and the corresponding values should be hardcoded, as shown below. This applies only when preparing for publishing, and it should be done for all remaining assignments in MATLAB. Remember, this only concerns userinputcommands and does not apply to function input arguments.%a = input("Enter a:"); a = 5;
Save your script as ma1_pre_0_username.m.
Save your flowchart as ma1_pre_0_username.pdf.
Submit all three files to Gradescope.
Sample Output#
Use the values in Table 14.1 below to test your program.
Case |
a |
b |
c |
|---|---|---|---|
1 |
5 |
1 |
2 |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
>> ma1_pre_0_username Enter a:5 Enter b:1 Enter c:2 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
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. |