Lecture 6
Multiple Input Functions
You can define a function to have more than one input. This is the syntax:
function r = func1(x,y)
Remember that the name of the function must match the name of the file (with a .m added at the end). Therefore, no spaces, operators etc. in the name of the function.
Exercise:
- Make a function for which the input is x and y and the output is x^y. From the command line, check the following inputs:
- >powerfunc(2,3); powerfunc(3,2);
- x=2; y=3; powerfunc(x,y); powerfunc(y,x)
You can define more than one function inside a function file:
func2.m:
function y = func2(x)
y = helper(x)
function r = helper(q)
r = q^2;
In this way, the functions inside the file are visible to all the other functions in the file. However, only the first can be called from outside the file.
More on Logic
Warm Up Exercises:
-
x = [1 5 2 8 9 0 1], y = [5 2 2 6 0 0 2].Evaluate and understand the following:
- y>x
- x==y
- x<=y
- x|y
- x&y
- x&~y
- ((x>y)|(y>x))
- x<5
- x(y<5)
-
x = [1:10], y = [3 1 5 6 8 2 9 4 7 0]. Evaluate and understand the following:
- (x>3)&(y<5)
- x(x>5)
- x(y>5)
- y(x<=4)
- x( (x<2) | (x>=8) )
- y( (x<2) | (x>=8) )
(remember, all nonzero values are true)
Exercises:
- For x = [3 15 9 12 -1 0 -12 9 6 1]:
- Set to 0 the positive elements of x.
- Set to 3 the multiples of 3.
- Multiply by 5 the even elements of x
- Create y from the elements of x that are > 10.
- Set to 0 the elements less than the average.
- Set to the distance from the average the elements greater than the average.
- Create r and c such that:
( 1 1 1 1 1 )
( 2 2 2 2 2 )
r = ( 3 3 3 3 3 ) c = r'
( 4 4 4 4 4 )
( 5 5 5 5 5 )
- Using r and c, create
( 1 0 0 0 0 ) ( 1 0 1 0 1 )
( 1 1 0 0 0 ) ( 0 1 0 1 0 )
( 1 1 1 0 0 ) and ( 1 0 1 0 1 )
( 1 1 1 1 0 ) ( 0 1 0 1 0 )
( 1 1 1 1 1 ) ( 1 0 1 0 1 )
Think about ideas for the final project.
Ideas mentioned in class:
- Chaos
- Logistic equation
- Mandelbrot Set
- Combinatorics
- Dynamics
- Visual Illusion
- Interploation
- Use a spline to recreate your name in cursive.