SUNY Geneseo Department of Mathematics
Monday, January 30
Math 230 02
Spring 2017
Prof. Doug Baldwin
Motivation: A programmer/digital artist once asked me for advice on writing a program that would distort a digital photograph as if it were being viewed through water with droplets falling in it and making ripples. We decided to design a function that would more or less model the ripples, and he could use the amplitude of that function to control his distortion. In particular, the function we came up with was
Such a function in Matlab can use “local variables” (i.e., variables created within the function) to represent the distance and scale factor:
function [ z ] = wave( x, y )
%WAVE Evaluate a function whose plot looks like a wave rippling away from
% the origin of the Cartesian plane. Specifically, wave(x,y) returns the
% value of the wave at point (x,y).
% The basic wave uses the cosine of distance from the origin to create
% ripples. But I scale the amplitude by a factor of the form s^(-distance)
% to make the magnitude of the ripples decrease as they get farther from
% the origin.
s = 1.1; % Scale factor for damping wave
d = sqrt( x^2 + y^2 );
z = s ^ (-d) * cos( d );
end
Note the syntax for defining a local variable and assigning it a value: name = value
These local variables are helpful for a couple of reasons:
You can also use local variables in the command window, e.g., to save the result of a command for later use. Matlab has some built-in locals, most significantly a constant for the value of π
>> x = 3 x = 3 >> y = 4 y = 4 >> sqrt( x^2 + y^2 ) ans = 5 >> 2 * x + 3 * y ans = 18 >> pi ans = 3.1416 >> format long >> pi ans = 3.141592653589793
Functions for lab can all be written as one-liners
Vectors and how to create them
Read sections 4.2 - 4.5