Matlab PDF
Matlab PDF
looping constructs
ans =
0
Two theoretically equal numbers can differ slightly due to round off
errors during computer calculations. This causes equality or
inequality test to fail.
ans =
1
Logical operators: symbols that are used to combine or negate the
expressions containing relational operators (conditions)
& AND
| OR
~ NOT
xor Exclusive OR
if statement
Syntax:
if e
statements
end
Ex: if x >= 0
y = sqrt(x)
end
if e
statements
else
statements
end
if e1
statements
elseif e2
statements
else
statements
end
Example revisted: Write a program to find area of a triangle if 3
sides are given.
%data validation
if (x(1)+x(2)<x(3)|x(2)+x(3)<x(1)|x(1)+x(3)<x(2))
disp('error in input data... try again');
else
s=sum(x)/2;%semiperimeter
area=sqrt(s*(s-x(1))*(s-x(2))*(s-x(3)));
fprintf('area = %f\n',area);
end
%area of a triangle given 3 sides
disp('Program to find area of a triangle when 3 sides are given');
a=input('Enter 1st side of a triangle :');
b=input('Enter 2nd side of a triangle :');
c=input('Enter 3rd side of a triangle :');
%data validation
if ~(a+b>c & a+c>b & b+c>a)
disp('error in input data... try again');
else
s=a+b+c/2;%semiperimeter
area=sqrt(s*(s-a)*(s-b)*(s-c));
fprintf('area = %f\n',area);
end
The syntax is:
scalar or string
switch expr
case v1
statement
...
case {v2,v3,...}
statement
...
otherwise
statement
...
end
Example 1:
%program to display transmission line fault statistics
switch lower(fault)
case 'lg'
disp('probability of occurrence of LG fault is 85%');
case 'll'
disp('probability of occurrence of LL fault is 8%');
case 'llg'
disp('probability of occurrence of LLG fault is 5%');
case ‘lll’
disp('probability of occurrence of LLL fault is 2%');
otherwise
disp(‘invalid input’);
end
Generate a random integer number x between 1 to 10. If x = 1 to
3, then display the message Result = FF to the screen. If x = 4 to
10 then display message result =„Pass‟.
Ex:
x = 5;
while x <25
disp (x)
x = 2*x-1;
end
function[f]=fact2(n)
f=1;
ii=1;
while(ii<=n)
f=f*ii;
ii=ii+1;
end
>> fact2(3)
ans =
6
Write a program to reverse digits of an integer number
11111*
1111*1
111*11
11*111
1*1111
*11111
Normally when program encounters error while running, the program aborts. The try-
catch constructs modifies this default behavior.
the statements between try and catch are executed until an error occurs.
If error occurs, instead of aborting, the statements between catch and end are
executed.
The code within catch and end can make use of the function lasterr
to access information about the error and act accordingly.
x = ones(4,2); x = ones(4,2);
y = 4*eye(3); % now wrong size y = 4*eye(3); % now wrong size
try try
z = x*y; z = x*y;
catch catch
z = nan; lasterr
disp('X and Y are not conformable.') end
end
z
ans =
NaN
Recursion is a kind of tricky and smart construct which allows a
function to call itself. or it is a type of programming technique
which involves the use of function that call itself one or more
times until a specified condition is met.
The Matlab programming language supports it, so a statement
within the function can call the same function during its
execution.
Two requirements to solve problem recursively:
1. to express the problem in recursive form
2. to include the stopping condition
Factorial of a number
It is possible for the function to call itself forever and never
return an answer. That happens in the code above if we enter a
negative argument. How to avoid this?
Try-catch construct
function y = ten_exp(n)
% This is a recursive function for computing y = 10^n.
% The program works only if n is a nonnegative integer.
% If n is negative, the algorithm won't stop.
if n == 0
y=1
else
n %<< this line is not needed but for inspection
y = 10 * ten_exp(n-1)
end
>> ten_exp(3)
n=
3
n=
2
n=
1
y=
1
y=
10
y=
100
y=
1000
ans =
1000
The advantages and disadvantages of the two approaches (recursion and iteration) are
not always obvious, and you should really take it on a case-by-case basis. When in
doubt: test it. But generally:
1. Recursion may be slower, and use greater resources, because of the extra function
calls.
function y = ourmean(x)
% (OURMEAN) Calculates average
[m,n] = size(x);
if m == 1
m = n;
Sub- end
Function y = sum(x)/m;
s=
15
a=
ans =
15
Types of errors in MATLAB programs
• Syntax errors
– Check spelling and punctuation
• Run-time errors
– Check input data
– Can remove “;” or add “disp” statements
• Logical errors
– Use shorter statements
– Check algorithm
– Check units