An interactive introduction to MATLAB

(Jeff_L) #1

42 scripts and functions


Listings 3.4 presents an example of a simple function that multiplies two
numbers,xandy, together to calculate anarea. Listing 3.5 demonstrates how
to execute this function in the command window.
Listing 3.4: A simple function
1 function area = calculateArea(x, y)
2 % Function to calculate an area given two lengths (x, y)
3 area = x*y;

Listing 3.5: Execution of a simple function
1 >> x = 5; y = 10;
2 >> area = calculateArea(x, y)
3 area =
4 50
The same function could also be executed using variables with different
names, as shown in Listing 3.6.
Listing 3.6: Execution of a simple function
1 >> length1 = 25; length = 100;
2 >> myArea = calculateArea(length1, length2)
3 myArea =
4 2500

Creating a function


(http://www.eng.ed.ac.uk/teaching/courses/matlab/unit03/simple-
function.shtml)
Free download pdf