Docsity

14 Solved Problems on MATLAB Functions - Assignment 2 | CS 257, Assignments of Computer Science

Toggle sidebar
bg1
CS 257 Homework 2
Assigned: January 31, 2005
Due: February 03, 2005 - in class
The purpose of the first assignment is to acquaint the student with the basic MATLAB
environment. Once you have completed the assignment you should be able to perform the
following:
•Enter/exit MATLAB on the EWS machines, run a simple demo program, use the
helpdesk facility, be familiar with search and help.
•Perform arithmetic calculations using operators, commands and functions.
•Create and work with simple arrays.
•Create and display a simple plot.
NOTES:
We will be using MATLAB 7.0 Release 14 for this course (contrary to a previous announce-
ment). The MATLAB environment can be executed on EWS UNIX Workstations by select-
ing ”Matlab” from the EWS Start menu, or by entering ”matlab” at the command prompt
of an xterm session.
We strongly suggest that you use the MATLAB software provided on the EWS workstations.
Otherwise, it is possible that you will not be able to successfully complete the assignments.
Make sure to clearly and explicitly state all your answers.
Use helpdesk to answer all the questions.
ASSIGNMENT:
This assignment covers lectures 1 and 2.
1. Complete the MATLAB lines below to set sequal to the sum of the even-numbered
entries of A.
A=sin(1:40);
s = sum( A(2:2:40) )
2. Complete the MATLAB lines below to set sequal to the sum of the odd-numbered
entries of A.
A=sin(1:40);
s = sum( A(1:2:40) )
3. From “helpdesk” present the replacement for the “short-circuit” logical operator &&
when operating on MATLAB logical arrays.
& (or alternatively, bitand(a,b) where a,b are two logical arrays)
1
pf3

Partial preview of the text

Download 14 Solved Problems on MATLAB Functions - Assignment 2 | CS 257 and more Assignments Computer Science in PDF only on Docsity!

CS 257 Homework 2

Assigned: January 31, 2005

Due: February 03, 2005 - in class

The purpose of the first assignment is to acquaint the student with the basic MATLAB environment. Once you have completed the assignment you should be able to perform the following:

  • Enter/exit MATLAB on the EWS machines, run a simple demo program, use the helpdesk facility, be familiar with search and help.
  • Perform arithmetic calculations using operators, commands and functions.
  • Create and work with simple arrays.
  • Create and display a simple plot.

NOTES:

We will be using MATLAB 7.0 Release 14 for this course (contrary to a previous announce- ment). The MATLAB environment can be executed on EWS UNIX Workstations by select- ing ”Matlab” from the EWS Start menu, or by entering ”matlab” at the command prompt of an xterm session.

We strongly suggest that you use the MATLAB software provided on the EWS workstations. Otherwise, it is possible that you will not be able to successfully complete the assignments.

Make sure to clearly and explicitly state all your answers.

Use helpdesk to answer all the questions.

ASSIGNMENT:

This assignment covers lectures 1 and 2.

  1. Complete the MATLAB lines below to set s equal to the sum of the even-numbered entries of A.

A=sin(1:40); s = sum( A(2:2:40) )

  1. Complete the MATLAB lines below to set s equal to the sum of the odd-numbered entries of A.

A=sin(1:40); s = sum( A(1:2:40) )

  1. From “helpdesk” present the replacement for the “short-circuit” logical operator && when operating on MATLAB logical arrays.

& (or alternatively, bitand(a,b) where a,b are two logical arrays)

  1. Complete the MATLAB lines below to set logical array l equal to 1 for those indices where both A > 2 and B < −1 below:

A=3sin(1:40); B=3cos(1:40); l = A>2 & B<- (we also accepted "l = bitand(A > 2, B < -1)")

  1. Assume that A is a 1x1024 array containing some unknown integress. Present a single MATLAB command to collect all entries that are even.

A(2:2:1024) (we also accepted A(mod(A,2)==0) )

  1. Complete the MATLAB lines below to generate length 100 array B with B(n) = (n^2 + n)/2.

A=1:100; B = (A.^2 + A)./

  1. Complete the MATLAB line below to set s equal to the sum of the components in the n-th row of Pascal’s triangle.

we accepted a couple answers based on the interpretation of n-th row: s = diag(fliplr(pascal(n+1))) s = diag(fliplr(pascal(n)))

  1. Complete the MATLAB line below to set s equal to the sum of the components in row 1 to n of Pascal’s triangle.

(multiple answers) (1) sum(sum(triu(fliplr(pascal(n+1)))))- (2) sum(sum(triu(fliplr(pascal(n+1))))) (3) sum(sum(triu(fliplr(pascal(n))))) Also "tril(rot90(pascal(?),2))" and other variants. If the answer was correct, we awarded credit.

  1. Complete the MATLAB lines below to compute y in Ay = b by Cramer’s rule.

b=ones(4,1); A=1:16; A=A.*sin(A); A=reshape(A,4,4); y = det([b,A(:,2:4)])/det(A) for m=2:4; y=[y;det([A(:,1:(m-1)),b,A(:,(m+1):4)])/det(A)]; end

  1. Complete the MATLAB lines below to compute y in Ay = b by MATLAB’s linear system solver: