Wednesday, 8 June 2016

MATLAB Programming 30 - if...else...end statement

if...else...end Statement

An if statement can be followed by an optional else statement, which executes when the expression is false.

Syntax

The syntax of an if...else statement in MATLAB is:

if <expression>
% statement(s) will execute if the boolean expression is true
<statement(s)>
else
<statement(s)>
% statement(s) will execute if the boolean expression is false
end

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

Example


Create a script file and type the following code:

a = 100;
% check the boolean condition
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
else
% if condition is false then print the following
fprintf('a is not less than 20\n' );
end

fprintf('value of a is : %d\n', a);

When the above code is compiled and executed, it produces the following result:

a is not less than 20

value of a is : 100


MATLAB Programming 29 - if...end Statement

Decision making structures require that the programmer should specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

MATLAB provides following types of decision making statements.

if... end Statement

An if ... end statement consists of an if statement and a boolean expression followed by one or more statements. It is delimited by the end statement.

Syntax

The syntax of an if statement in MATLAB is:

if <expression>
% statement(s) will execute if the boolean expression is true
<statements>
end

If the expression evaluates to true, then the block of code inside the if statement will be executed. If the expression evaluates to false, then the first set of code after the end statement will be executed.


Example

Create a script file and type the following code:

a = 10;
% check the condition using if statement
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
end
fprintf('value of a is : %d\n', a);

When you run the file, it displays the following result:

a is less than 20

value of a is : 10


Tuesday, 7 June 2016

MATLAB Programming - Functions for set operations PROGRAM

Create a script file and type the following code:

a = [7 23 14 15 9 12 8 24 35]

b = [ 2 5 7 8 14 16 25 35 27]

u = union(a, b)

i = intersect(a, b)

s = setdiff(a, b)

When you run the file, it produces the following result:

a = 7 23 14 15 9 12 8 24 35

b = 2 5 7 8 14 16 25 35 27

u = Columns 1 through 11
      2 5 7 8 9 12 14 15 16 23 24
      Columns 12 through 14
      25 27 35

i = 7 8 14 35

s = 9 12 15 23 24

MATLAB Programming 28 - Functions for set operations (4)

setxorSets exclusive OR of two arrays

unionSets union of two arrays

uniqueUnique values in array


MATLAB Programming 27 - Functions for set operations (3)

setdiff(A,B)Sets difference of two arrays; returns the values in A that are not in B. The values in the returned array are in sorted order.

setdiff(A,B,'rows') - Treats each row of A and each row of B as single entities and returns the rows from A that are not in B. The rows of the returned matrix are in sorted order.
The 'rows' option does not support cell arrays.


MATLAB Programming 26 - Functions for set operations (2)

ismember(A,B,'rows') - Treats each row of A and each row of B as single entities and returns a vector containing 1 (true) where the rows of matrix A are also rows of B. Elsewhere, it returns 0 (false).

issorted(A) - Returns logical 1 (true) if the elements of A are in sorted order and logical 0 (false) otherwise. Input A can be a vector or an N-by-1 or 1-by-N cell array of strings. A is considered to be sorted if A and the output of sort(A) are equal.

issorted(A, 'rows') - Returns logical 1 (true) if the rows of two-dimensional matrix A are in sorted order, and logical 0 (false) otherwise. Matrix A is considered to be sorted if A and the output of sortrows(A) are equal.


MATLAB Programming 25 - Functions for set operations (1)

MATLAB provides various functions for set operations, like union, intersection and testing for set membership, etc.

The following table shows some commonly used set operations:

intersect(A,B)Set intersection of two arrays; returns the values common to both A and B. The values returned are in sorted order.

intersect(A,B,'rows') - Treats each row of A and each row of B as single entities and returns the rows common to both A and B. The rows of the returned matrix are in sorted order.

ismember(A,B) - Returns an array the same size as A, containing 1 (true) where the elements of A are found in B. Elsewhere, it returns 0 (false).




Saturday, 4 June 2016

Calculus using MATLAB 2 - finding the limits (2)

The limit function falls in the realm of symbolic computing; you need to use the syms function to tell MATLAB which symbolic variables you are using. You can also compute limit of a function, as the variable tends to some number other than zero.

 To calculate lim x->a(f(x)), we use the limit command with arguments. The first being the expression and the second is the number, that x approaches, here it is a.

For example, let us calculate limit of a function f(x) = (x-3)/(x-1), as x tends to 1.

limit((x - 3)/(x-1),1)

MATLAB will execute the above statement and return the following result:

ans = NaN




Calculus using MATLAB 1 - finding the limits (1)

MATLAB provides various ways for solving problems of differential and integral calculus, solving differential equations of any degree and calculation of limits. Best of all, you can easily plot the graphs of complex functions and check maxima, minima and other stationery points on a graph by solving the original function, as well as its derivative.

This chapter will deal with problems of calculus.. In this chapter, we will discuss pre-calculus concepts i.e., calculating limits of functions and verifying the properties of limits.

In the next chapter Differential, we will compute derivative of an expression and find the local maxima and minima on a graph. We will also discuss solving differential equations.

Calculating Limits

MATLAB provides the limit command for calculating limits. In its most basic form, the limit command takes expression as an argument and finds the limit of the expression as the independent variable goes to zero.

For example, let us calculate the limit of a function f(x) = (x3 + 5)/(x4 + 7), as x tends to zero.

syms x

limit((x^3 + 5)/(x^4 + 7))

MATLAB will execute the above statement and return the following result:

ans = 5/7


Thursday, 2 June 2016

MATLAB Programming 24 - Functions for bitwise operations (2)

bitand(a, b) - Bit-wise AND of integers a and b

bitcmp(a) - Bit-wise complement of a

bitget(a,pos) - Get bit at specified position pos, in the integer array a

bitor(a, b) - Bit-wise OR of integers a and b

bitset(a, pos) - Set bit at specific location pos of a

bitshift(a, k) - Returns a shifted to the left by k bits, equivalent to multiplying by 2k. Negative values of k correspond to shifting bits right or dividing by 2|k| and rounding to the nearest integer towards negative infinite. Any overflow bits are truncated.

bitxor(a, b) - Bit-wise XOR of integers a and b

swapbytes - Swap byte ordering


MATLAB Programming 24 - Functions for bitwise operations (2)

Create a script file and type the following code:

a = 60;      % 60 = 0011 1100

b = 13;      % 13 = 0000 1101

c = bitand(a, b)      % 12 = 0000 1100

c = bitor(a, b)      % 61 = 0011 1101

c = bitxor(a, b)      % 49 = 0011 0001

c = bitshift(a, 2)      % 240 = 1111 0000 */

c = bitshift(a,-2)      % 15 = 0000 1111 */

When you run the file, it displays the following result:

c = 12

c = 61

c = 49

c = 240

c = 15


MATLAB Programming 23 - Functions for Bitwise Operations (1)

Bitwise operators work on bits and perform bit-by-bit operation.

Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100

A|B   = 0011 1101

A^B  = 0011 0001

~A = 1100 0011

MATLAB provides various functions for bit-wise operations like 'bitwise and', 'bitwise or' and 'bitwise not' operations, shift operation, etc.