# 2014F001/User talk:KatieMay/homework 4

8.75/12: See comments in italic. Chris 19:02, 22 October 2014 (UTC)

## 1. Variables are programmer-designated names that can represent a specific value of data. Like other programming languages, MATLAB also has a set of rules for naming variables. If the rules are violated, the variables can not be treated as valid variables, causing programming errors.

Create four "invalid" variable names with different reasons, and state the reason why the variable names can not be used in MATLAB.

K$variable cannot include$ symbol

## 2. Without running Matlab, what output results from running the following Matlab code?


%% A matlab code to test arithmetic functions.

a = 3;

b = 4;

c = a*b;

a = a + 2

b = b + 2;

a = mod (c, a);

b = mod (c, b);

a

b



a=2

b=0

## 3. Use Matlab to evaluate the following expressions.

1. $\sqrt { 2 } \pi ^ 3$
1. $\frac {\pi } {\sin (1.2) * \cos (6.0)}$
1. $(0.0000123 + 5.678 \times 10 ^{-4} ) \times 0.1234 \times 10 ^ {-4}$
1. $\mod (3, 4)$
1. $\mod (4, 3)$
1. floor(7.6)
1. ceil( − 3.4)
1. round(5.5)

sqrt(2)*pi^3

ans =43.8495

pi/(sin(1.2)*cos(6.0))

ans =

   3.5105


(.0000123+5.678*10^-4)*0.1234*10^-4

ans =

  7.1584e-09


mod(3,4)

ans =

    3


mod(4,3)

ans =

    1


floor(7.6)

ans =

    7


ceil(-3.4)

ans =

   -3


round(5.5)

ans =

    6


## 4. Anonymous function in Matlab

An anonymous function defined in matlab is a function that is not stored in a program file, but associated with a variable. Provide a matlab script in which a mathematical function f(x,y) is defined as

f(x,y) = cos(x3) + sin(y3) + ex+y

Calculate the values of f(0.3,0.2), f(3,2), f(π, π / 2) using the defined anonymous function in Matlab.

f(0.3,0.2)

ans =

   2.6564


f(3,2)

ans =

 149.1104


f(pi, pi/ 2)

ans =

 111.5651


## 5. Suppose that you have defined the function foo as follows


function y = foo(x)

z = x + 2;

y = x * z^2;

end



what are the values from the following calls?

>> foo(2)=32

>> foo(foo(2))=36992

## 6. Write a Matlab function (m file) to evaluate the function logfunc(x) = log10(x2), and (1) show your m-file for this function; (2) calculate logfunc(3.2) and logfunc(-5.0)




You have the wrong function. See solution below. -1 Chris

## 7. Sign-and-magnitude

Use the sign-and-magnitude method to represent the signed number (-101)10 in 8 bits.

1 1 0 0 1 0 1

See solution below. -1 Chris

The magnitude (101)10 can be represented by seven bits. The eighth bit will be used for the sign.

 1 1 0 0 1 0 1 26 25 24 23 22 21 20 64 + 32 + 0 + 0 + 4 + 0 + 1 = 101
The last bit will be 1 to denote the negative sign. Thus, the answer is (1110 0101)2.


## 8. One's complement

Convert One's Complement numbers to base ten numbers.

1. 10001010=117
1. 01110110=-118

1. 10001010=117
1. 01110110=-118

Opposite signs. -0.5 Chris

## 9. Two's Complement

Form the negative or positive equivalent of the following 8-bit Two's Complement numbers

1. 00011001
1. 10101010

11100111

01010111

## 10. One's complement

For the computer architecture using two's complement to represent negative numbers, what is the minimum number of bits needed to represent (-255)10.

9

## 11. Representing Negative Numbers

For an 8-bit group, work out the representation for − 8710 in

a) Sign & Magnitude

11010111

b) One's Complement

10101000

c) Two's Complement

10101001

d) Excess-127
00101000


## 12. Suppose signed integers are represented by 10 bits in a computer architecture. Show respectively the range of the integers (in base 10) in the following methods:

a) Sign & Magnitude

(-511 to 511)

b) One's Complement

(-1023 to 1023)

c) Two's Complement (-1023 to 1023)

d) Excess-127

-1150 to 1150