Introduction to Computer Programming with MATLAB

read the review

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

do only the coding part

CSC 113:
Introduction to
Computer
Programming with
MATLAB
Main Hardware Component Categories
Software: What is Programming?
Programming is the process of:
• Analyzing a problem
• Designing an algorithmic solution (i.e., producing a recipe or list of
rules)
• Coding the design in a programming language
• Testing the correctness of the solution
• Maintaining the solution (receiving user feedback and modifying the
design and code)
Translate and Run a Program
• Interpreted languages translate a program during execution, or
“on the fly.”
What is MATLAB?
• MATLAB was originally developed to be a “MATrix LABoratory”
• Developed primarily by Cleve Moler in the 1970’s
• Moler wanted his students to use the matrix packages LINPACK and EISPACK
without requiring knowledge of Fortran
• Developed MATLAB as an interactive system to access LINPACK and EISPACK
• MATLAB gained popularity primarily through word of mouth
because it was not officially distributed
• In 1980’s, MATLAB was rewritten in C with more functionality
• The Mathworks, Inc. was created in 1984
• The Mathworks is now responsible for development, sale, and
support for MATLAB
• MATLAB has now evolved into an interactive system and
programming language for general scientific and technical
computation and visualization
Problem Solving in Engineering and Science
1. State the problem
• You must understand the problem to solve it
2. Describe inputs and outputs
• Input: What do I know?
• Output: What am I trying to find out?
3. Develop an Algorithm
• Given my input, how can I find the output?
4. Solve the problem
• Write MATLAB code!
5. Test the solution
• Does the code work?
• Does it solve the problem I started with?
MATLAB Prep
• Before we can start coding, we need access to MATLAB:
1. Install copy on personal computer
• Download MATLAB to personal computer via Catholic University of America
School Wide License: Portal Page
• ~30 day student free trial available
• Discounted student editions are available for purchase; can be used over your
whole student career (undergrad, graduate, and PhD).
2. Access in Campus Labs
3. Remote Access to Computer Lab Resources
• https://technology.catholic.edu/programs/labremoteaccess/
• Use Single App option for MATLAB
MATLAB as a calculator









Calculations can be performed similar to a scientific calculator
Just type them into the command window and press enter
Some examples to try out (press enter after each):
1+1
3 ∗7
4/2 + 1
3^2
10 − 5 ∗ 4 + 2^4
4/2^2
• Use parentheses to disambiguate order of operations
• 3 – (6^2)/3
• 3 – 6^(2/3)
• Screen a little messy? Type clc to clear it.
Common Mathematical Operations
• Raising to a power:
• 5^2 (5 raised to the 2nd power, or 5 squared)
• Square root:
• sqrt(25)
• Trigometric operations: Sine, Cosine, Tangent
• sin(3.14)
• cos(3.14)
• tan(3.14)
• π: Use ‘pi’ as a stand in for π
• cos(pi)
Order of Operations
MATLAB follows the standard algebraic rules:
1. Parenthesis from the innermost set to the outermost
2. Exponentiation “^”
3. Multiplication and division “*, /” from left to right
4. Addition and subtraction “+,-” from left to right
Variables
• What if you want to save a number for later use?
• E.g. your bank’s interest rate is 0.031256842; do you really want to have to
re-type that in every time?
• A MATLAB variable is a name that you assign to a value
• The variable name allows you to re-use it in other places or calculations
• Try typing:
x=5
• Assigns the value 5 to the variable name x.
Now type:
x+x
• What is the result?
Variables name rules
• Names must start with a letter
• The other characters can be letters, numbers, or the underscore
character ( _ )
• Names can be any length but only the first 63 characters are used
• Names are case sensitive:
e.g., x is not the same as X
• Names cannot be a reserved keyword (run iskeyword for list)
• You can use the command isvarname to test if a name is valid
• isvarname Abc13 returns 1 (valid)
• isvarname Abc-13 returns 0 (invalid)
Variables: what are they good for?
• Variables improve clarity, by letting you name a value
something to remind you of its meaning, and allowing you to
reuse the value
• Lets say you want to calculate the area of a circle:
(𝐴= 𝜋𝑟^2); you could do the following:
r=3
circle_Area = pi * r * r
• Now you want to calculate the circumference of the same
circle (𝐶=2𝜋𝑟); use r again!
Circle_Circumf = 2 * pi * r
• Naming the radius variable r reminds you it is a radius and
allows you to re-use it in multiple calculations.
Matrices in MATLAB
• Matrices are MATLAB’s namesake, and its basic datatype
• Single values are called scalars, and are stored as a 1×1 matrix
• A one-dimensional list of vales, in either a row or column, are called vectors,
and are stored as a 1xn or nx1 matrix
• A two-dimensional table of values is called a matrix. It contains both rows and
column, and are stored as a nxm matrix
• In math, we represent matrixes with square brackets:
𝐴= 5
𝐵= 2
5
1
𝐶=
5
1
D=
5
2
7
Matrix Definition
• In MATLAB, we can enter simple matrixes as explicit lists using
• square brackets at start and end,
• commas or spaces to separate columns,
• and semicolons to separate rows:
1
1 2
𝐴= 5
𝐵= 2 5 𝐶=
D=
5
5 7
• A=[5]
• B = [2,5]
OR B = [2 5]
• C = [1;5]
• D = [1,2;5,7] OR D = [1 2;5 7]
Advanced Matrix Definition
MATLAB provide methods of automatically creating larger
matrixes according to a patten.
• The Colon operator (:) can be used to define evenly spaced
matrixes
A = [ 1 : 5 ] returns A = 1 2 3 4 5
• MATLAB starts at one, then increments by one until it reaches five
• If you would like a different increment, use:
A = [ 1 : 2 : 9 ] returns A = 1 3 5 7 9
• MATLAB starts at one, then increments in steps of two, until it
reaches nine
Advanced Matrix Definition
• In general, A = x : y : z indicates to form a matrix that
• value 1 is x
• value 2 is x + y
•…
• value i is x + (i −1)y
• if x + (i −1)y > z , then stop and do not add the element
• Note: if z does not appear in the increment patter, it will not be
included
• Numbers beyond z will not be included
Advanced Matrix Definition
• The linspace command lets you specify linearly spaced
matrixes:
• Provide the start number, end number , and number of elements:
• linspace(1, 10, 3) returns 1 5.5 10
• Start at one, end at ten, include 3 elements
• WARNING: students often confuse linspace and the : operator
• The logspace command is similar, but uses the first two
numbers as powers of 10:
logspace( 1, 3, 3) returns 10 100 1000
Arithmetic operators
• Scalar operators can be applied to
• Scalars
• A matrix and a scalar: will perform the operation on each member of
the matrix separately
• Two matrixes of equal size: will perform operation on each pair of
elements
Arithmetic operators
• A=2
• X=[1,2,3,4,5]
• A-1
•=1
•X+5
• = [6,7,8,9,10]
• X.*[5,4,3,2,1]
• = [5, 8, 9, 8, 5]
The Transpose operation
• The transpose operator (‘) changes rows to columns, and vice-versa
• A=[1;2;3]
A=
1
2
3
• A’
ans = 1 2 3
• B=[1,2;3,4]
B=
1 2
3 4
B’
ans =
1 3
2 4
Number Display
Saving Your Work:
Script files
• You can write commands to a script file just like in MATLAB’s
command window, BUT they will not be executed immediately.
• Save the file as a .m file (e.g. myScript.m).
• When the script is RUN, the commands will be executed, one at a
time, in the order they are listed in the file.
• You can run a script file either by:
1.Pressing Run in MATLAB’s script file editor
2.Typing the filename in the command window
• Scrips allow you to save your code in a more permanent and reusable way.
• You can also make small changes to your commands and calculations without
re-typing each one
Saving Your Work:
Script files
• So now, we can type our formula once, and run.
CircleScript.m:
r=4
circle area = pi * r * r
circle circum = 2 * pi * r
• In command window:
CircleScript
• Now if we want, we can go back to the editor and change the variables
value, save, and re-run the program to get the new answers!
Saving Your Work:
Script files
• You can also COMMENT your code – that is, make notes in your code to
document things for later reference.
• Use “%” to start a comment. Any text after this symbol will be ignored by
MATLAB
• CircleScript.m:
%Script to calculate circle geometry
r=4 %Set the radius value
circle area = pi * r * r
% calc circle area
circle circum = 2 * pi * r % calc circle circumferance
Saving Your Work:
Script files
▪ Comments are useful for:
▪ Planning out your steps in advance:
%Step 1: calculate radius
%Step 2: calcuate circle area
▪ Reminding yourself what your code does and why
%I’m dividing the diamater by 2 to calculate the radius, so I can…
▪ Explainning to your professor what your code does and why (or what it should
do)
%This should calcuate the diamater of the circle for use in…
▪ Explain to your team what your code does and why
%I’m using this equation because…
Saving Your Work:
Script files
▪ Comments can also be used to organize your code into sections
▪ the pattern:
%% comment
will break up the code into sections
▪ Sections can be run altogether, like normal scripts, or individually (usefull if you
need to test one portion repetedly
▪ Sections are optional, but usefull for organizing large files (Or labeling mutliple
HW problems submited in one script file for your professor 😉
More about MATLAB Environment
• Command Window
• Type in mathematical operations and lines
of code for immediate execution
• Answers are displayed
• Create and access variables
• Useful commands:
• whos: displays current variables
• clc: clears window (but does not erase
variables)
• clear all: erases all variables
• To remove a single variables, type clear
varname
• help: display help text
• Type help topic to get help on a specific
topic
• NOTE: variables and commands are lost
when MATLAB is closed!
More about MATLAB Environment
• Workspace window
• Provides a summary of the variables
currently defined and available in the
command window.
• You might note an entry called “ans”
• Matlab automatically stores the result of
the last calculation in a variable called
“ans”
• You can use ans in calculations, but
remember it may change after executing a
new command.
More about MATLAB Environment
• Current folder window
• Displays files currently available to MATLAB
• We will discuss creating and running files in a few slides
More about MATLAB Environment
• Graphics Windows
• MATLAB is often used to visualize
data via graphics windows, such as
the graph to the right.
• Windows will automatically be
created when you plot some data.
• Plotting data requires two
vectors of equal length and the
plot command:
• x=[1 2 3 4 5]
y=[10 20 30 40 50]
plot(x,y)
More about MATLAB Environment
• Editor Window:
• This is the window where we
program our script files
• Can be opened with the “New
Script” button, or the edit
command
• Multiple files can be opened as
tabs
• The Editor bar (above) has tools to
save, edit, debug, and run the
program.
• We will explore these more
throughout the semester
Introduction
• MATLAB provides many pre-built commands to accomplish a variety
of common mathematical calculations quickly.
• These are referred to as “Functions”
• MATLAB functions usually require some input and produce output.
• Format:
output = function(inputs)
• If you don’t provide a variable to receive the output, the output is
printed to the command window
Input arguments
• Function Inputs are often called arguments
• Some functions require one input argument
• x = sqrt(16)
(calculates square root of 16, stores results in x)
• Others require two or more input arguments:
arguments are separated by commas, and order is important
• x = rem(14, 5)
(calculate the remainder of 14 divided by 5, stores results in x)
Input arguments
• Functions can be nested: the result of one function can be used as
the argument of another function
• x = sqrt(rem(14, 5))
• Many functions can except vectors or matrices as input
• Many will perform the requested operation on each value in the matrix
• X=[1,4,9,16,25]
y=sqrt(X)
y=
1
2
3
4
5
Function Outputs
• Unlike inputs, outputs are optional: you can store them in a variable,
use them immediately (via nesting), or ignore them
• Most functions have at least one output variable
• x = sqrt(16) % (calculates square root of 16, stores results in x)
• If you do not provide an output variable, the result is displayed to the screen,
and temporarily stored in ans
• Note: if the input is a vector, the output will be a vector too.
Function Outputs
• Some Functions have two or more outputs:
• You can store multiple outputs in two or more variables by
specifying a list of variables in vector format:
• [a,b] = size(x) %(a contains the number of rows, b the number of columns)
• a and b are separate variables you can use independently (they are not
trapped in a vector together)
• Since outputs are optional, you can choose to save any number
you want
• If you provide one variable, only the first will be stored
• If you provide less than the numbers of outputs, only the first N will be
stored; the remaining outputs will be lost
• If you do not store any outputs, only the first output will be
displayed to the screen, and temporarily stored in ans
Getting help
• help generates a list of help topics
• help tan generates help on the function tan
• Can be used with any built-in MATLAB function
• Window interface can be activated by selecting:
Help → MATLAB Help
• Alternatively, from the command window you can run:
doc tan
to open the documentation of the function tan
• Play with the help window to see the various options MATLAB offers
Common Mathematical Functions
-1
Rounding functions
Discrete mathematics functions
Trigonometric functions
Data Analysis Functions:
Maximum and Minimum
• max(x) and min(x) can be used to find the maximum and minimum
value in a matrix, respectively.
• These two functions offer optional additional outputs
• In addition to returning the max/min value, they can (optionally) return the
location of the value in the array
• This is known as an index number; we will come back to their usefulness in Chapter 4.
• Do you remember how to accept two outputs?
• By specifying two variables in vector format:
[a,b] = max(x)
• a will contain the maximum value
• b will contain the values location in x
Data Analysis Functions:
Maximum and Minimum
Data Analysis Functions:
Basic Statistics
Data Analysis Functions:
Sums and Products
Data Analysis Functions:
Sorting Values
Data Analysis Functions:
Matrix Size
Data Analysis Functions:
Advanced statistics
Uniform Random numbers
• rand generate a uniform pseudo-random number in the range [0, 1]
• randi(max) generate a pseudo-random integer in the range [1, max]
1. How would you generate a random number in the interval [a, b] for
any a, b ∈ R with a ≤ b?
𝑎 + 𝑏 − 𝑎 ∗ 𝑟𝑎𝑛𝑑
2. How would you generate a random integer in the interval [a, b] for any
a, b ∈ Z with a ≤ b?
(a − 1) + 𝑟𝑎𝑛𝑑𝑖(𝑏 − (𝑎 − 1))
Uniform Random number Matrices
Complex Numbers
• Complex numbers consist of a real and an imaginary part
• 3 + 2i
• −5 − 2i
• 3/4 + 7/8i
where i is the imaginary unit, which has the property i 2 = −1
• In MATLAB complex numbers can be entered as
• A = 3 + 2i or
• A = 3 + 2 ∗ i or
• A = complex(3, 2),
which all return A = 3.0000 + 2.0000i
Complex Numbers
Computational Limits
Computational Limits & Special Values
• MATLAB is limited in the data it can store, hence the limits
• It does not have infinite memory
• Overflow: When a calculation goes above the maximum value
MATLAB can store
• When this happens, MATLAB returns the special value Inf – short for infinity
• Underflow: when a calculation is so small MATLAB cannot distinguish
it from 0
• When this happens, MATLAB returns 0
Special Values and functions
π
Suppressing output
• When performing complex, multi-step computations (in script files or
the MATLAB Command Window), we may only want to display the
final answer.
• We can suppress the output of any command by placing a semicolon
(;) character at the end of the command.
• EG:
X=5;
Y=X+3;
Z=Y*X
%Output will be suppressed
%Output will be suppressed
%Output will be displayed
• On the Ch 3 homework, try to only display the answers specifically
requested; suppress any intermediate steps using a semicolon.
Be careful not to suppress the answers though!
Combining Matrixes
▪A matrix can also be built from other matrices
A=[1, 2, 3];
B=[4, 5, 6];
C=[A; B]
• returns:
123
456
Note: each row must have the same number of columns
Indexing Matrices
• Once a matrix is defined, you can access elements using
their index number in parenthesis
• Index: numerical location of a number in the array;
first element in always indexed one (1)
• Format: Matrix(index)
• If A = [1, 3, 5, 7, 9, 11]
then A(3) will return 5,
A(5) will return 9
Indexing Matrices
• For 2-dimensional arrays, you can provide row and column
indexes:
• Format: TwoDMatrix(row, col)
1 2
• If A = [1,2;3,4] =
3 4
A(1, 2) will return 2
A(2, 1) will return 4
• Technically you can also use single indexes (but it is less
readable):They are counted down each Column
• A(1) -> 1, A(2) ->3, A(3)->2
Indexing Matrices
• Indexes can also be used to change or write new values into
the array
• A = [1, 2, 3, 4, 5, 6]
A(3) =10
results in A = [1, 2, 10, 4, 5, 6]
• A(10) = 10
results in A = [1, 2, 10, 4, 5, 6, 0, 0, 0, 10]
1 2
• If B = [1,2;3,4]=
3 4
B(1, 2) = 5
1 5
result: B =
3 4
Indexing Matrices with the Colon Operator
• The colon operator (:) can also be used to extract whole
or specific rows and/or columns when indexing:
• A(:, 1) extracts column 1 from matrix A (all rows)
• A(1, 🙂 extracts row 1 from matrix A (all columns)
• A(2:3, 🙂 extracts rows 2 to 3 (all columns)
• A(2:3, 1:3) extracts columns 1 to 3 of rows 2 to 3
• A(:) extracts all elements into a column vector
• Useful for calculating stats on an entire matrix
Changing, Deleting, or Adding rows & columns
• Suppose
1
2
3
4
5
6
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12] =
7
8
9
10 11 12
• Chang row 2 of A to [13 14 15]
• A(2, 🙂 = [13 14 15]
• Deleting column 3 of A
• A(:, 3) = []
• Adding [16 17] as a 5th row to A
• A(5, 🙂 = [16 17]
Indexing with “end”
• MATLAB includes a keyword “end” that can be used to
indicate the last index (row or column) in a vector or
Matrix.
• This is useful if you do not know the exact size of the matrix:
1 2 3
•A=
;
4 5 6
• A(end, end) -> 6
• A(1,end) -> 3
• A(end, 1) -> 4
Matrix Math: scalar operations
• Matrices can be used in mathematical calculations as
well, but they behave differently depending on the
operation and operands
• Addition and subtraction of One matrix and one “scalar”:
• A=[1,2,3,4]
A+5 %Will add 5 to each element of A
ans = 6 7 8 9
A-1 %Will subtract 1 to each element of A
ans = 0 1 2 3
Matrix Math: scalar operations
• For multiplication, division, and exponents, scalar operation should
be explicitly indicated using the “.” operand notation:
• .*
• ./
• .^
• A=[1,2,3,4]
A.*5 %Will multiply each element of A by 5.
ans = 5 10 15 20
A./2 %Will divide each element of A by 2
ans = 0.5000 1.0000 1.5000 2.0000
A.^2 % will square each value
ans = 1 4 9 16
Matrix addition
▪The sum of two n x m matrices A and B is an n x m matrix C where
• C (r , c ) = A(r , c ) + B(r, c )
for every element in row r and column c such that 1 ≤ r ≤ n, 1 ≤ c ≤ m
▪The subtraction from an n x m matrices A of an n x m matrix B is an
n x m matrix C where
C (r , c ) = A(r , c ) − B(r, c )
for every element in row r and column c such that 1 ≤ r ≤ n, 1 ≤ c ≤ m
• In MATLAB, matrix addition and subtraction can be computed as:
A + B and A − B
Matrix Scalar Multiplication
▪Indicated with “.” operand notation:
A .* B and A . / B and A.^B
▪The scalar multiplication of two n x m matrices A and B is an n x m matrix
C where
C (r , c ) = A(r , c ) * B(r, c )
for every element in row r and column c such that 1 ≤ r ≤ n, 1 ≤ c ≤ m
▪The scalar division of an n x m matrices A by an n x m matrix B is an
n x m matrix C where
C (r , c ) = A(r , c ) / B(r, c )
• for every element in row r and column c such that 1 ≤ r ≤ n, 1 ≤ c ≤ m
Matrix Scalar Multiplication
▪The scalar exponents of two n x m matrices A and B is an n x m matrix
C where
• C (r , c ) = A(r , c ) ^ B(r, c )
for every element in row r and column c such that
1 ≤ r ≤ n, 1 ≤ c ≤ m
Why Matrix Math?
• Allows easy completion of repetitive calculations
• Calculate the circumference of circles with radius 1
through 100;
Circumference =2𝜋𝑟
• r = [1:100]
• A=2 .* pi .* r
% create vector of desired r values
% compute vector of Areas.
Problems with Two Variables:
Using Meshgrid
• The Meshgrid command automatically creates matrices to allow
calculation of all variable combinations (and the vectors don’t need to be
the same size!)
• Set x = [1:5] and y=[1:3]
• Call meshgrid:
[new_x, new_y] = meshgrid(x,y)
1 2 3
• new_x = 1 2 3
1 2 3
4 5
4 5
4 5
1 2
• Now new_x *. new_y = 2 4
3 6
1
new y =2
3
3 4
5
6 8 10
9 16 15
1 1 1
2 2 2
3 3 3
1
2
3
Special Matrixes
Diagonal Matrices
• The diag() function can be used to extract the diagonal of a matrix:
A = [1 2 3; 3 4 5; 1 2 3];
diag(A) –> 1.0
4.0
3.0
• A second input can specify alternate diagonal:
• diag(A,1)
Diagonal Matrices
• The diag() function can also be used to build a matrix from a given
diagonal
• If the input of diag is a vector, a matrix will be built with the vector as
the diagonal:
B = [1 2 3];
diag(B) -> 1 0 0
0 2 0
0 0 3
• Again, a second argument can be used to place the vector in a specific
diagonal.
Diagonal Matrixes
Magic Matrices
• The Magic function
generates a “magic” matrix
• All rows, columns, and
diagonals sum to the same
number
• This is more interesting
than it is useful…
• But it is an easy way to get
large matrixes of values.
Spring 2021
Midterm
CSC 113 Intro. To Programming with MATLAB
3/04/2021
Name:________________________
Written portion: Complete the written portion in a Microsoft Windows compatible format.
1. Indicate whether the following variable names are valid in MATLAB; if invalid indicate
why. (5 points)
a. The4thQuarter
ANSWER:
b. 1998_RC
ANSWER:
c. The Quick Brown Fox
ANSWER:
d. Thunderbird_2
ANSWER:
e. It’s#codingTime!
ANSWER:
2. Give a brief definition/description (one or two sentences) of the following parts of a
computer. (5 points)
a. Central Processing Unit (CPU)
ANSWER:
b. Main Memory
ANSWER:
c. Secondary Memory / Storage
ANSWER:
d. Input Devices
ANSWER:
e. Output Devices
ANSWER:
3. Give a brief description (one or two sentences) of what the following MATLAB built-in
functions do. (5 points)
a. realmax
ANSWER:
b. rand(m,n)
ANSWER:
c. mode(x)
ANSWER:
d. [a,b] = size(x) ANSWER:
e. factorial(x)
ANSWER:
Coding portion: Complete the coding portions in one or more MATLAB script files; please
clearly indicate via comments which parts belong to each question.
4. Write MATLAB code to perform each of the following calculations. (10 points)
a.
3+𝑒2 ∗5
2∗3
7
b. 6 + 8 ∗ 𝑡𝑎𝑛−1 (23°)
c. 𝑒 6+2 + √19
d. log 20 (5 + 106 )
e. 1+2-3*4/56
5. The formula for converting the temperature from Fahrenheit to Celsius is as follows:
𝐶=
5
(𝐹 − 32)
9
Write MATLAB code to generate a vector of 25 linearly spaced temperatures in
Fahrenheit between 0.0 and 212.0. Then, your code should convert and display the
temperatures in Celsius. Plot the values, putting Fahrenheit on the x-axis and Celsius on
the y-axis (5 points)
6. Write MATLAB code to: (10 points)
a. Set a variable y equal to a random integer between 5 and 20
b. Check if y is prime
c. Set a variable x equal to 27+yi
d. Display the imaginary part of x
7. The equation of a straight line can be expressed as
𝑦 = 𝑚𝑥 + 𝑏
where m is the slope and b is the y-intercept. Write MATLAB code to set the slope to 16
and the intercept to -5. Then plot y over the range x = -10 to 10. (10 point)
8. The abs() function is used to calculate the absolute value of a number.
Use this function to write MATLAB code to: (10 points)
a. Create a 100 by 100 matrix of random numbers from the gaussian distribution.
b. Calculate and display the absolute difference between the mean of the matrix’s
values and the expected mean of 0.
c. Calculate and display the absolute difference between the variance of the matrix’s
values and the expected variance of 1.
d. Repeat parts b. and c. for each column of the matrix. Display the column which is
the closest to the expected values of mean and which is closest to the expected
value of variance.
9. Write MATLAB code to create the following matrices and perform the operations below:
(12 points)
7
4
𝐴=[
1
0
8
5
2
0
9
1
6
] 𝐵 = [5
3
9
0
2
3
4
6
7
8]
10 11 12
a. Assign to x the element in the second row and third column of A.
b. Assign to y the fourth column of B.
c. Assign to z the submatrix of B consisting of rows 2 through 3 and columns 1
through 3.
d. Delete the last column of A
10. Add two lines at the top of your script file(s) to delete any existing variables and erase
the contents of the MATLAB command window. Ensure your .m file(s) runs without
error before submission. (3 points)
Extra Credit: Write MATLAB code to: (10 points)
a. Define a vector of all even numbers between 1 and 100.
b. Define a vector of all odd numbers between 1 and 100.
c. Plot the two vectors against each other.
d. Create a vector of 100 elements where all the odd indexed elements are 1, and all
the even indexed elements are 0.
e. Plot the point value of each question on this exam (excluding extra credit) in a
new window. Label the x-axis “Question #” and the y-axis “Point value.”

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER