Lab 6 gave a basic introduction to MATLAB, but it did not cover some of the more powerful features of MATLAB.
Objectives for this lab:
MATLAB stores equations as character strings. For example, let's look at the following
polynomial:
You can enter this equation into MATLAB by assigning it to a variable. You can call it S1:
S1 = 'X^2 + 5*X + 3';
diff(S1,'X')
2X + 5
int(S1,'X',1,100)
717255/2
Let us enter another equation:
S2 = 'X^2-X-6=0';
Note that this time we have set the function equal to a value.
solve(S2)
sympow(S1,2)
(X^2+5*X+3)^2
expand(sympow(S1,2))
X^4+10*X^3+31*X^2+30*X+9
S3 = expand(sympow(S1,2));
simple(S3)
ans = (X^2+5*X+3)^2
S4 = 'X^3-1'; factor(S4)
ans = (X-1)*(X^2+X+1)
Again, MATLAB offers many more symbolic functions. Use help to see what each function does. You can also type help on a line by itself, and MATLAB will give you list of places you can look for new commands to try.
In the last MATLAB lab, you learned about scripting, but the functions that were created could not do very much. Like scheme, MATLAB functions can contain conditionals, iteration, and use recursion.
Conditionals in MATLAB are closer to the use of cond in scheme. A conditional begins with an 'if' and ends with 'end'. Below is an example of the use of if:
d=20;
if d > 100
disp('Greater than 100');
elseif d > 10
disp('Greater than 10');
else
disp('Less than 10');
end
for k=100:-1:1
disp(k);
end
<starting value>:<increment by>:<ending value>
while (x>0)
disp(x);
x=x-1;
end
x>0 is your test expression. end, as in the example. x = 10;
| Less than | < |
| Less than or equal to | <= |
| Greater than | > |
| Greater than or equal to | >= |
| Equal | == |
| Not equal | ~= |
| And | & |
| Or | | |
| Not | ~ |
Recursion is very easy in MATLAB. All you have to do is call your function somewhere in your script. You call your function as you would any other MATLAB function. If you are still confused, here is a very simple example that will print out a text string a certain number of times (based on an input parameter):
function [] = example(x)
% example(x)
% Prints out the string "Listen to Billy and call the doctor." x times
if x > 0
disp('Listen to Billy and call the doctor.');
example(x-1);
end
For this script to run, save it in a file called "example.m". As for the script itself, it takes in one input variable and returns nothing. As the script starts, it checks the value of x. If x is greater than zero, it prints out the string, and calls itself again (recursion). Notice that when the function occurs, the value of x is reduced. This is to ensure that we approach a terminating condition (in this case, when x is no longer greater than 0). You should always make sure your recursive scripts approach termination; infinite recursion will crash your computer in record time!
Listed next are two example functions, one recursive and one iterative, each of which accepts two values and computes the greatest common denominator (GCD). Before you look at the functions, an explanation of GCD is in order.
The greatest common denominator (GCD) of two numbers is the largest integer that can be evenly divided (leaving no remainder) by both numbers. For example, the GCD of 15 and 5 (written as gcd (15, 5)) is 5 because 5 is the largest number that is divided evenly by 15 and 5. Some more examples:
gcd (24, 12) = 12 gcd (13, 2) = 1 gcd (110, 85) = 5
| X | Y | Remainder |
| 110 | 85 | 25 |
| 85 | 25 | 10 |
| 25 | 10 | 5 |
| 10 | 5 | 0 |
This process is rather tedious, and can be greatly aided by a computer.
function [ result ] = rGCD(x, y)
% rGCD(x,y) returns the greatest common denominator of x and % y
temp = mod(x, y);
if ( temp == 0)
result = y;
else
result = rGCD(y, temp);
end
function [ result ] = itGCD(x, y)
% itGCD(x,y) returns the greatest common denomonator of x and % y
temp = mod(x, y);
while ( temp > 0)
x = y;
y = temp;
temp = mod(x, y);
end
result = y;
function [<any variables you want to be returned, separated by commas>] = <functionName>(<parameter list>) % Percent signs are comment marks. <All of your statments for this function>
Functions just end. There's no special way to make it end. If you want something printed out to the screen, don't but a semicolon after it. If you do not want something printed out to the screen, make sure there's a semicolon after that statement.
What you need to turnin:
The mod function if similar to the remainder function, but not exactly the same. Try the mod function in MATLAB to see the differences. Web pages also help.
Turnin all of these files by 8 a.m., Monday, Mar. 26,2001. Turn in the following files:
rfib.m itfib.m rmod.m itmod.m