# 2014F001/User talk:lavina usa/homework 4

11/12: Good work. Chris 15:38, 21 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.

C1%E2 = only numbers, letters or underscores can be used. Percent signs are not permitted.

A2B3-C3= variable name can't be hyphen.

358291 = needs to contain letters.

142B= variable starts with a number rather than a letter. Letters are correct way to contain valid variable than numbers.

## 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=5

b=0



mod(12,5) is 2 since dividing twelve by five yields a remainder of two. -0.5 Chris

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

1. $\sqrt { 2 } \pi ^ 3$ = 43.8495
2. $\frac {\pi } {\sin (1.2) * \cos (6.0)}$ = 3.5105
3. $(0.0000123 + 5.678 \times 10 ^{-4} ) \times 0.1234 \times 10 ^ {-4}$ = 7.1584*10 ^ -9
4. $\mod (3, 4)$ = 3
5. $\mod (4, 3)$ = 1
6. floor(7.6) = 7
7. ceil( − 3.4) = -3
8. round(5.5) = 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.

Answer:


f(0.3,0.2) = 2.6564

f(3,2) = 149.1104

f( pi, pi/2) = 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)

m file =

function z = logfunc(x)
z = log10(x^2);
end

logfunc(3.2)= 1.0103
logfunc(-5.0)=1.3979


## 7. Sign-and-magnitude

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

## 8. One's complement

Convert One's Complement numbers to base ten numbers.

1. 10001010 = ( -117 )10
2. 01110110 = (137)10

## 9. Two's Complement

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

1. 00011001 = 11100111
2. 10101010 = 01010110

## 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.

## 11. Representing Negative Numbers

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

 a) Sign & Magnitude = (11010111)2 b) One's Complement = (00101000)2 c) Two's Complement = (00101001)2 d) Excess-127 = (00101000)2

## 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 = -511 to 511 c) Two's Complement = -1023 to 1023 d) Excess-127 = -127 to 896

Two's Complement has a range of -(29) to +(29 - 1) = -512 to +511. Excess-127 has the same range as One's Complement. -0.5 Chris