Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

% Define matrix A one element at a time.

%% Lab 2 – Your Name – MAT 275 Lab

%% Example code

% Example 1

% NOTE: Delete examples before submission.

A = [1 0; 0 -1]

A = [1, 0; 0, -1]

% NOTE: The two matrices above are the same. We can separate columns with

% commas OR spaces, and separate rows with semi-colons.

%%

% The following code changes the element in the 1st row and 2nd column of

% matrix A to 9. In general, we can extract or change elements in a matrix A

% with the syntax A(row,column).

A(1,2) = 9

%%

% We can create the original matrix A above one element at a time as

% follows:

% Initiate A as empty vector (optional but necessary in other cases).

A = [];

% Define matrix A one element at a time.

A(1,1) = 1;

A(1,2) = 0;

A(2,1) = 0;

A(2,2) = -1;

% Display vector A.

A

%%

% This example shows how we can extract elements, rows, or columns from a

% matrix. A colon indicates “all elements.” So A(2,:) extracts elements in

% all columns of A that are also in the 2nd row of A. Note: To run this

% section, a matrix A must be saved in the workspace.

disp(‘Extract second row’) % disp command displays a specified string

A(2,:)

disp(‘Extract first column’)

A(:,1)

disp(‘Extract last element’)

A(end,end)

%%

% Let’s declare a vector b and sove Ax = b where A is a known matrix, b is

% a known vector, and x is an unkown vector with same dimensions as b. This

% is the most fundamental problem in linear algebra.

b = [1;2];

x = A\b  % solve Ax = b using backslash command

% NOTE: These examples are not comprehensive. Make sure you also go through

% the protocol examples.

%% Exercise 1

% Part (a)

%%

% Part (b)

%%

% Part (c)

% NOTE: Use the “backslash” command. NOT the division operator “/”.

%%

% Part (d)

%%

% Part (e)

%%

% Part (f)

%% Exercise 2

% Part (a)

% NOTE: We must create separate function M-files here. The function will

% require input variables. Therefore, you cannot use “run” to execute the

% function. You must invoke it by providing values for the input variables.

% Suppose we name our function geom_sum. Then complete the following.

% NOTE: You MUST comment each line of code in you M-file similar to how I

% comment each line of code in this script file. You may choose your own

% style of documentation, but make sure it is consistent throughout the

% semester and thoroughly explains what the code is doing. Further, you

% should have a comment at the beginning of each function file explaining

% what the function does. I WILL MARK OFF POINTS otherwise.

% Display contents of geom_sum M-file.

type ‘geom_sum.m’

% Assign values to input variables.

r = ??;

a = ??;

n = ??;

% NOTE: Do NOT define r,a, or n inside the function file. This defeats the

% purpose of requiring input arguments.

% Compute geometric sum for specified values of r,a, and n.

geom_sum(??)

%%

% Part (b)

% NOTE: MATLAB has several built in functions. Here, we want to use the

% built-in function “sum” to compute the same geometric sum as computed in

% part (a). If x is a vector, then “sum(x)” sums all the elements of x.

% Therefore, the “sum” function takes a vector as input and sums its

% elements. Hence, we must create a vector containing all the terms of the

% sum we wish to compute and pass that vector as input to the sum function.

%% Example for 2b

x = [1, 1, 1];

sum(x)

% NOTE: Answer should be 1 + 1 + 1 = 3. Type “help sum” in command window

% for more info.

%% Exercise 3

% Part (a)

% NOTE: When the protocol says to write a script file, you do not need to

% create a separate M-file. This main file is a script file and you can

% simply write the list of commands here. Please refer to the second

% example on page 5 before attempting this problem.

% Initiate product P.

P = ??;

% Define starting iteration index.

m = ??;

% Define stepsize of iteration.

k = ??;

% Define ending iteration index.

n = ??;

% Compute product.

for i = m:k:n

    <code>    % muliply P by next element at each iteration (suppress output)

end

% Display product.

<code>

% NOTE: i = m:k:n is also a vector! A for-loop essentially iterates through

% each element of a vector. Keep this in mind for part (b).

% NOTE: Type “help for” in the command window for more info.

%%

% Part (b)

% NOTE: Emphasis on the part where it says “SINGLE COMMAND.” That means ONE

% line of code. The “prod” function is another built-in function that,

% similar to “sum”, takes in a vector as input, yet computes the product of

% all the elements of the input vector. So, we need to create a vector

% containing all the numbers between 1 and 15 with a stepsize of 2. Which

% vector declaration method is best when we know the stepsize? Also, in

% order to compute the product in a single command, we must declare the

% vector INSIDE the prod command as an input argument, as opposed to x =

% vector, prod(x). Instead, we do prod(vector). Type “help prod” in the

% command window for more info.

%% Exercise 4

% NOTE: When the protocol says to write a script file, you do not need to

% create a separate M-file. This main file is a script file and you can

% simply write the list of commands here. Refer to second example on page 6

% of protocol before attempting this exercise.

% Initiate variables.

power = ??;

k = ??; % initiate counter

% Initiate vector V of powers of 3.

V = ??;

% Compute powers of 3 and store in V.

while ?? < ??   % specify condition of while-loop: stop iterating once

                % condition is no longer satisfied

    power = ??; % compute next value of power at each iteration

    V = ??;     % concatenate vector V with new element at each iteration

    k = ??;     % increment counter k

end

% Display vector V.

<code>

%% Exercise 5

% NOTE: Here, we must create a separate function M-file f that accepts one

% input argument, a value of the variable x. You may want to start creating

% a new folder for each lab as we will be using f multiple times to define

% different functions. Please refer to the example on the last page of the

% protocol before attempting this problem.

% Display contents of function f M-file.

type ‘f.m’

% Evaluate f at x = 1.

f(??)

% Evaluate f at x = 2.

<code>

% Evaluate f at x = 3.

<code>

% Evaluate f at x = 4.

<code>

% Evaluate f at x = 7.

<code>

% Evaluate f at x = 10.

<code>

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"