Week10_notes

advertisement
CS 103, J. Michael Fitzpatrick
CS 103, Week 10, p. 1/6
CS 103 Week 10
Chapter 6: Complex Data, Character Data, and Additional Plot Types

Strings (continued)
o To convert a number to a string: sprintf(‘%1.1f’, string);
 Very useful in callbacks
o Using sprintf to build a string as the value for a String property
 Use the programming assignment 2 as the running example.
 (Callbacks from the solution are given below)
 GUIv2 [on line] is what I built for the class
o I made the slider actually do something (alter g!)
o Note that some properties, such as the Value property of a slider object,
are not strings. It is not necessary to use str2num to convert them.
o More plotting
 Use x = 1:10; y = [3 7 4 2 9 9 0 3 2.5 6]
03/21/01


bar(x,y)
barh(x,y)

pie(y)

compass(x,y)
OUCH! pie can’t have a zero!
o goes COUNTERclockwise
o colors from the spectrum---violet to red
o for fun try pie([1:100);
o Note that x and y are rectangular coordinates, NOT
polar coordinates
 histograms
o hist(y) plots 10 bins
o hist(y,n) plots n bins
o There are other versions; help hist
o y = rand(1,1000) % roughly uniform
o y = rand(1,10000) % more uniform
o y = randn(1,10000) % normally distributed
o Other string functions:






ischar(‘asdf’)  1; ischar([2 3 4])  0
strcat(‘Hello ‘, ‘world!’)  ‘Helloworld’.
lower(‘Hello’)  ‘hello’
upper(‘Hello’)  ‘HELLO’
strcmp(‘Hello’,’Hello’)  1; strcmp(‘Hello’,’hEllo’)
 0
isspace(‘AB C’)  [0 0 1 0]; isspace(‘a’)  0;
isletter(‘A1 b’)  [1 0 0 1]


Complex numbers
o Examples:





x
y
z
z
z
=
=
=
=
=
10 + 20i % note the “missing” *
12 + 25i
x – y
x*y
x/y % see formula on page 209 of textbook
p. 1/6
CS 103, J. Michael Fitzpatrick
CS 103, Week 10, p. 2/6

Functions:
 real(z)
 imag(z)
 conj(z);
o The complex plane: Plot real horizontally. Plot imag. vertically
 magnitude, angle representation: “polar coordinates”
 Functions: abs(z); angle(z)
 angle is in radians, range = [-pi, pi]
 tan(angle(x)) equals imag(x)/real(x)
o Two (and more) angles give the same tangent!
o angle(-4+4i) is 2.35 (135 degrees), while
atan(-1) is –0.79 (-45 degrees).
o angle(x) == atan2( imag(x), real(x) )
3/23/01


x = abs(x)*(cos(angle(x)) + sin(angle(x))*i)
 must have *i here (can’t omit it as above)
o Plotting in the complex plane:
 plot(x) plots in the complex plane for you!
 Example: x = [1+.5i,-2+3i,-4-i,6+i];
o plot(x); plot(x,’r*’);
o polar(angle(x),abs(x),’r*’);
o compass(angle(x),abs(x));
 Don’t try ‘r*’, etc. with compass: it tries to
draw its arrows with stars!
Plotting in 3D
o plot3(x,,y,z) is for plotting points in 3-space
 Example from textbook: p. 243,
t = 0:.1:10;
x = exp(-.2*t).*cos(2*t); y = exp(-.2*t).*sin(2*t);
plot3(x,y,t)
 Now move it around with the mouse!
Handy for looking at two related functions of one variable: x(t),
y(t), or for arbitrary 3d points.
o Mesh plots are for plotting a surface in 3-space such that the surface
represents a function f(x,y) of two independent variables:
o meshgrid to define the pairs (x,y) at which f is to be plotted
 Example:

x = [-4:.2:4]; y = [-8:.2:8];
[xg,yg] = meshgrid(x,y);
zg = exp(-0.5*(xg.^2+yg.^2));
mesh(xg,yg,zg)
 Now rotate it with the mouse.

 Handy for looking at one function of two variables: z(x,y).
Programming exercise:
o Write a function that takes one argument and returns the value gotten by
swapping its real and imaginary components.
p. 2/6
CS 103, J. Michael Fitzpatrick
CS 103, Week 10, p. 3/6
o Write a function that takes one vector of complex numbers and loops
through it looking for the one with the largest absolute value and returns
that value.
o Now change the previous function so that it also returns the index of that
largest value.
o Matlab provides this function: [biggest, index] = max(x), which, for
complex numbers automatically looks for the largest absolute value.
o
For reference here are some functions from the solution to assignment 2:
function initialize()
clear;
global degrees_to_radians g b target m s0 angle;
degrees_to_radians = pi/180;
g = 9.8;
b = 0.1;
H = findobj(gcbf,'Tag','Air Resistance Editor');
set(H,'String',sprintf('%g',b));
target= 800;
m = 10;
s0 = 100;
fprintf('inside initialize: target = %d\n',target);
plot_target(target);
angle = 45;
function quit_ballistics()
close(gcbf);
function fire()
%
%
%
%
Author: J. M. Fitzpatrick
Purpose: This is a callback function for the Fire button.
Its purpose is to plot and print information about the trajectory
of a shell with or without air resistance.
% Determine type of problem (b small or not)
%
%
%
%
%
Calculste initial velocity (i.e., x and y components)
Calculate values
Plot
Print table
If target hit, give congratulations!
global degrees_to_radians g b target m s0 angle;
fprintf('inside fire: target = %d\n',target);
if angle < 0, break, end
p. 3/6
CS 103, J. Michael Fitzpatrick
CS 103, Week 10, p. 4/6
vx0 = s0*cos(degrees_to_radians*angle);
vz0 = s0*sin(degrees_to_radians*angle);
if b > 1e-10
[t,vx,vz,x,z] = with_air_resistance(g,b,m,vx0,vz0);
else
[t,vx,vz,x,z] = no_air_resistance(g,m,vx0,vz0);
end
hold on;
plot(x,z);
legend('target position','trajectory');
hold off;
print_results(t,vx,vz,x,z);
% Find x value for last positive z:
k = 1;
while k <= length(z) & z(k) >= 0
k = k+1;
end
kp = k - 1;
% Check on and handle target hit:
H = gcbf;
H = findobj(H,'Tag','Message');
if abs(x(kp)-target) < 10
set(H,'String','Congratulations! You hit the target!');
else
set(H,'String','You missed the target. Change the parameters and
try again!');
end
function parameters(name)
global degrees_to_radians g b target m s0 angle;
str = get(gcbo,'String');
switch name
case 'resistance'
value = str2num(str); % returns [] if can’t convert
if size(value) ~= [1 1]
fprintf('Air resistance must be a number\n');
elseif value < 0
fprintf('Air resistance must be a positive number\n');
else
b = value;
end
case 'mass'
value = str2num(str);
if size(value) ~= [1 1]
fprintf('Mass must be a number\n');
p. 4/6
CS 103, J. Michael Fitzpatrick
CS 103, Week 10, p. 5/6
elseif value < 0
fprintf('Mass must be a positive number\n');
else
m = value
end
case 'speed'
value = str2num(str);
if size(value) ~= [1 1]
fprintf('Speed must be a number\n');
elseif value < 0
fprintf('Speed must be a positive number\n');
else
s0 = value;
end
end
function initial_angle()
global angle;
angle = get(gcbo,'Value');
H_AngleText = findobj(gcbf,'Tag','Angle Text');
set(H_AngleText,'String',sprintf('%1.1f degrees',angle));
o Note that some properties, such as the Value property of a slider object,
are not strings. It is not necessary to use str2num to convert them.
o Other string functions:






ischar(‘asdf’)  1; ischar([2 3 4])  0
strcat(‘Hello ‘, ‘world!’)  ‘Helloworld’.
lower(‘Hello’)  ‘hello’
upper(‘Hello’)  ‘HELLO’
strcmp(‘Hello’,’Hello’)  1; strcmp(‘Hello’,’hEllo’)
 0
isspace(‘AB C’)  [0 0 1 0]; isspace(‘a’)  0;
isletter(‘A1 b’)  [1 0 0 1]


More plotting
o Use x = 1:10; y = [3 7 4 2 9 9 0 3 2.5 6]




bar(x,y)
barh(x,y)
pie(y)
OUCH! pie
compass(x,y)


can’t have a zero!
Change x to be numbers in the range [0 359]
Looping example!
o Write a function that takes on argument
p. 5/6
CS 103, J. Michael Fitzpatrick
CS 103, Week 10, p. 6/6
Chapter 6: Complex Data, Character Data, and Additional Plot Types

Strings
o (As we’ve learned) a string is a sequence of characters.
o Example: x = ‘Hello’;

o
03/19/01
o
o
o
size(x)  1 5
 double(x)  72
101
108
108
111
Example: x = ‘10’;
 size(x) 1 2
 double(x)  48 49
 x + 1  49 50
To convert a string to a number: str2num(x)  10
 Very useful in callbacks
To convert a number to a string: sprintf(‘%1.1f’, string);
 Very useful in callbacks
Using sprintf to build a string as the value for a String property
 Use the programming assignment 2 as the running example.
 GUIv2 is what I built for the class
 I made the slider actually do something (alter g!)
o Note that some properties, such as the Value property of a slider object,
are not strings. It is not necessary to use str2num to convert them.
o Other string functions:






ischar(‘asdf’)  1; ischar([2 3 4])  0
strcat(‘Hello ‘, ‘world!’)  ‘Helloworld’.
lower(‘Hello’)  ‘hello’
upper(‘Hello’)  ‘HELLO’
strcmp(‘Hello’,’Hello’)  1; strcmp(‘Hello’,’hEllo’)
 0
isspace(‘AB C’)  [0 0 1 0]; isspace(‘a’)  0;
isletter(‘A1 b’)  [1 0 0 1]


More plotting
o Use x = 1:10; y = [3 7 4 2 9 9 0 3 2.5 6]




bar(x,y)
barh(x,y)
pie(y)
OUCH! pie
compass(x,y)


can’t have a zero!
Change x to be numbers in the range [0 359]
Looping example!
o Write a function that takes on argument
p. 6/6
Download