# basic matlab HW,

*label*Other

*timer*Asked: Mar 23rd, 2013

**Question description**

It would be great if you could sent me the homework through email zengjr1992@gmail.com

COMPUTER SCIENCE 2A

INTRODUCTION TO COMPUTERS

ASSIGNMENT 4

Overview:

In this assignment, you’ll write matlab functions implementing various simple algorithms. You

will have to deal with for-loops, while-loops, and if-then-else constructs in addition to vector data

structures.

1. Write a script as follows. Use colon notation to create a vector from -2π to 2π with about

20 increments per number or 250 total. The plot sin(v) in red and using “hold on” plot

cos(v) in blue. Add a title. (10%)

2. The reverse function for vector [1 2 5 4] returns [4 5 2 1]. Write a function reverse which

takes one input and returns one result. It should first find the length of the input, then

using a for loop assign the “i

th” element of the result to the n+1- i

th element of the input

vector.(15%)

3. Bouncing numbers. This function requires a while loop around an if-then-else construct.

The input is an integer and the output is a vector composed as follows. While the number

is not equal 1, then if it is odd, multiply by 3 and add 1; else if it is even, divide by 2. For

example 3 should return the vector [3 10 5 16 8 4 2 1]. You will have to use vector

concatenation v = [v n] to build the output vector because you won’t know the length in

advance. (15%)

4. Plotting bouncing numbers. Write a script which runs bouncing numbers from 1 to 1000,

takes the length of each output, and plots the length of each output. Also plot the

histogram of lengths(10%)

5. Binary Padding. Given a vector of 1’s and 0’s and a length like 16, adds 0’s to the left of

the vector until it is the correct length. Make sure it does something reasonable with a

input vector which is too long. (10%)

6. Decimal to Binary: write a function dec2bin which converts a decimal number to a vector

of 1’s and 0’s/ you know that mod(n 2) returns 1 or 0 if a number is odd or even. And you

already wrote reverse. Now its just a question of while the number is ~= 0 to keep

dividing by 2 after saving the remainder in a list.(15%)

7. Binary to decimal. Now you have to write a for loop with a sum gathering the powers of

2 which are selected by the 1’s in the vector. Are you going to calculate two to the

highest power and divide by 2, or reverse the vector and start from 1 by powers of

2?(10%)

8. Binary Addition. This function takes 2 vectors of 1’s and 0’s representing binary numbers

of the same length (which you can get using padding), add the numbers together (without

converting to decimal). Demonstrate with 150 Plus 90 (15%)

What to submit and how:

Submit your “.m” files individually and save your plots in a doc file and then as a pdf named

firstlast-hw4.pdf (replace “firstlast” with your first and last name and submit via Latte

.

Due date: Sun, Mar 24 at 11:55 PM. Late policy is 5% of the assignment value per day.