Lab 2
Lab 2
if condition
block
end
The if construct executes the block of statements if the condition is true. If the condition is false, the block skipped. The
if conditional can be followed by any number of elseif constructs:
if condition
block
elseif condition
block
.
.
.
end
The else statement also works by the following manner:
.
.
.
else
block
end
2.1.2. Switch
The syntax of switch statement is as follows:
switch expression
case value1
block
case value2
block
.
.
.
otherwise
block
end
1
The expression is evaluated and the control is passed to the case that matches the value. For instance, if the value of
expression is equal to value2, the block of statements following case value2 is executed. If the value of expression does
not match any of the case values, the control passes to the optional otherwise block.
2.2. Loops
2.2.1. While loops:
The syntax of while loop is as follows:
while condition:
block
end
The while construct executes a block of statements if the condition is true. After execution of the block, condition is
evaluated again. If it is still true, the block is executed again. This process is continued until the condition becomes false
2.2.2. for loops:
The for loop requires a target and a sequence over which the target loops. The form of the construct is
for target = sequence
block
end
The sequences may have one of the following forms:
1. initval:endval
• increments the index variable from initval to endval by 1, and repeats execution of program statements
until index is greater than endval.
2. initval:step:endval
• increments index by the value step on each iteration, or decrements when step is negative.
For instance the following code executes the value of cos(x) from x=0 to pi/2 at increments of pi/10.
for n = 0:5
y(n+1) = cos(n*pi/10);
end
Note: Loops should be avoided whenever possible in favor of vectorized expressions, which execute much faster.
A vectorized solution to the above for loop is:
>> n = 0:5;
>> y = cos(n*pi/10)
‘ break’ Statement
Any loop can be terminated by the break statement. Upon encountering a break statement, the control is passed to the
first statement outside the loop.
‘Continue’ Statement
When the continue statement is encountered in a loop, the control is passed to the next iteration without executing the
statements in the current iteration.
‘return’ statement
A function normally returns to the calling program when it runs out of statements. However, the function can be forced
to exit with the return command.
2
Errors in Numerical Computations
It is known that in numerical calculations errors are inevitable. So, for instance, if we evaluate the value of the function
g(t)=e−t(sin(2πt)+2) between 0 and 1 with increment of 0.002, using the usual double precision as well as single precision
representation and plotting the differences, the round of error is observed clearly.
The following MATLAB script plots the roundoff error which is incurred by representing of a number by the less precise
single precision representation:
t = 0:.002:1;
tt = exp(-t) .* (sin(2*pi*t)+2);
rt = single(tt);
round_err = (tt - rt) ./tt ;
plot (t,round_err, ‘b-‘);
title ( ‘error in sampling exp(-t)(sin(2\pi t)+2) single precision’)
xlabel(’t’)
ylabel(’roundoff error’)
% relative error should be about eta = eps(single)/2
rel_round_err = max(abs(round_err)) / (eps(’single’)/2)
Exercises:
1. Write a MATLAB script that evaluates the value of sin(2πt) at 200 equidistant points between 0 and 1. Then
calculates the roundoff error by approximating the values of the results which are evaluated at the interval given
above. Plot the roundoff error generated by approximating the values to the five decimal digit.
2. The expression (x − 1)6 can be represented by the following there forms:
p(x) =(x – 1)6
=x6− 6x5+ 15x4− 20x3+ 15x2− 6x + 1
=1 + x(−6 + x(15 + x(−20 + x(15 + x(−6 + x)))))
Write a MATLAB script that plots the three functions within the interval 0.996 and 1.004 at 81 equidistant points.
In order to observe the cancelation error plot each graphs with different colors.
3. Show that
ln − √ ଶ − 1=−ln + √ ଶ − 1
Among the above two expressions which one is better for numerical calculation. Write a MATLAB script that plots
the two equations within a selected interval and show the better equation for numerical calculation.
4. Write a MATLAB program that
a. sums up 1/n for n = 1,2,...,10,000;
b. rounds each number 1/n to 5 decimal digits and then sums them up in 5-digit decimal arithmetic for n =
1,2,...,10,000;
c. sums up the same rounded numbers (in 5-digit decimal arithmetic) in reverse order, i.e.,for n =
10,000,...,2,1.
Compare the three results and explain your observations.