Dec 03, 2024 | 407 words | 4 min read
12.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.m
and rename the file to ma1_pre_1_username.m.Make sure to fill out all header information, including a short description of the code.
In the
INITIALIZATION
section, prompt the user to initialize the variables,a
,b
, andc
to \(5\), \(1\), and \(2\) using the built-ininput
function.Note
In MATLAB, the
input
function 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’sinput
function always returns user input as astring
, regardless of the content, requiring explicit conversion to numeric types (likeint()
orfloat()
) afterward.In the
CALCULATION
section 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
OUTPUTS
section, print the results to the command window rounded to three decimal places using thefprintf
in-built function.Hint
To round to three decimal places in MATLAB within an
fprintf()
statement, use the syntax%.3f
when 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 CALCULATION
section, in which,replace the second value in
v1
with a value of \(8\) andreplace the first row in matrix
m1
with the row vectorv1
.
Allow both vector
v1
and matrixm1
to be shown in the command window.Publish your script as ma1_pre_1_username.pdf.
Refer to How to Publish in MATLAB video.
Note
For publishing, all user
input
statements 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 userinput
commands and does not apply to function input arguments.%a = input("Enter a:"); a = 5;
Save your script as ma1_pre_1_username.m.
Save your flowchart as ma1_pre_username.pdf.
Submit all three files to Gradescope.
Sample Output#
Use the values in Table 12.2 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_1_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