# 2014F001/User talk:Mnoony/homework 9

## 1. If statements and logical operators

Rewrite the following statements to use only one if statement:

 if x < y
if  z > 10 | y > 2
w = x*y*z
end
end


if x < y & z>10 | y>2
w=x*y*z
end


## 2. Relational operators

Suppose x = 6, without using Matlab, find the results of variable z in the following question.


(a) z = (x ~=6)
(b) z = x > 3 + 8
(c) z = 3 > 2 == 1
(d) z = 3 > 2 < 3
(e) z = 2 < 3 + 1 < 5 + 1


a) z= 1

b) z= 0

c) z= 0

d) z= 1

e) z= 1

A should be false, 0, and c should be true, 1.

## 3. Logical Operators

Suppose x =-1 and y = 0, work out the results of the following expression before checking them in Matlab.

(a) x & y
(b) x | y
(c) x > 0 & y > 0
(d) x + (y != 1)


a)

b)

c)

d)

## 4. Does this MATLAB code produce an error when run? If so, why?

    clear, clc;
A = [1:1:4];
B = [10:-2:4];
C(1,4) = 0;
for m = [4,3,2,1]
if A(m) > 2
A(1,m) = A(1,m)*m;
B(1,m) = 2*B(1,m-1);
else if A(m) < 2
A(1,m) = A(1,m)*(-1 *m);
B(1,m) = -2*B(1,m-1);
end
end
C = A + B


## 5. Print out the answer for A

A(10) = 0.0;
for i = [1:10]
if (mod(i, 2)==0)
A(i) = 1;
else
A(i) = 2;
end
end
A


## 6. Predict the answer of the following MATLAB code

   clear, clc;
for  m = [1:2]
for n = [5:-1:2]
if ((m+n <= 5) && (m >= 5))
disp(‘Condition 1 entered’)
elseif ((m+n <= 4) || (m >= 4))
disp(‘Condition 2 entered’)
elseif ((m+n <= 3) && (m >= 3))
disp(‘Condition 3 entered’)
elseif ((m+n >= 2) || (m <= 2))
disp(‘Condition 4 entered’)
end
end
end


## 7. Print out the outcome

  M = [1, 2, 4; 3,1,1; 2,5,6]
for i = [1:3]
for j = [1:3]
if (M (i, j) == 3)
M (i, j) = 0;
elseif (M(i, j) > 3)
M (i, j) = 1;
end
end
end


## 8. What’s the resulting value for counter?

  counter = 0;
for i = [1:3]
for j = [1:3]
M(i, j) = 2*i+j;
if (M(i, j) >3)
M(i, j) = 2;
counter = counter +1;
end
end
end
counter


## 9. Area estimation

Using Matlab to estimate the percentage of the area of the black shape in the box. You need to complete the matlab code provided below to obtain the image matrix.

determine the area of the shape.

In matlab, the follow code can be used to obtain a matrix (denoted as X) to represent the picture. Copy the following code to your matlab editor and continue to complete the code to calculate the percentage of the black area.

clear all; clc;
[A,map] = rgb2ind(M,2)
image(A);
colormap(map);
colorbar;
[m, n] =size(A); % m and n correspond to the size of the image matrix.


## 10. Integration

Consider the function $f(x) = 2.5 \cdot e^{-(x-3.2)^2/4.25}$ . Write a Matlab program which

• asks the user to enter values xmin and xmax;
• calculates the area under this curve between xmin and xmax. (here, xmin = 0.5, xmax =5.0)

Note: Use "input" to request user input in MATLAB. see http://www.mathworks.com/help/techdoc/ref/input.html for the documentation of input. For example,

xmin = input('The minimum x value for integration xmin =');
xmax = input('The maximum x value for integration xmax =');


In this way, the values for xmin and xmax can be provided interactively.

## 11. Integration

Given two mathematical functions $f(x) = 1/x * e^{-(\ln(x) -1)^2}$ and g(x) = sin2(x) − 0.5, calculate the area between the two curves from x=1.5 to x = 6.5 as shown in the following figure.