cnbc matlab mini course inf and nan
play

CNBC Matlab Mini-Course Inf and NaN 3/0 returns Inf 0/0 returns - PDF document

CNBC Matlab Mini-Course Inf and NaN 3/0 returns Inf 0/0 returns NaN David S. Touretzky September 2019 3+Inf Day 2: More Stuff Inf/Inf -Inf, -NaN 1 4 Scientific Functions Complex Numbers Trig: sin, cos, tan, asin, acos, atan


  1. CNBC Matlab Mini-Course Inf and NaN 3/0 returns Inf 0/0 returns NaN David S. Touretzky September 2019 3+Inf Day 2: More Stuff Inf/Inf -Inf, -NaN 1 4 Scientific Functions Complex Numbers Trig: sin, cos, tan, asin, acos, atan sqrt(-16) sinh, cosh, tanh, asinh, acosh, ... 3.5i Rounding: floor, ceil, round, fix Modular: rem, mod 2 - 3.5i Exponential: exp, log, log2, log10, sqrt (2+3i) * (4+5i) Primes: factor, primes Polynomials: roots, polyfit, polyval 2 5 Matrix Functions Predicates isreal(3) Determinant: det isprime(1 : 13) isnumeric([2 3 5]) Inverse: inv, pinv isempty([ ]) isinf(Inf) Eigenvalues: eig, svd isnan(NaN) islogical(1 == 1) Fourrier: fft ischar('a') isequal('foo', 'aardvark') And many, many more... What percentage of the first 1000 integers is prime? mean(isprime(1:1000)) 3 6

  2. Return Values Variable In and Out Functions can return multiple values: hist(randn(2000,1)) A = rand(5, 3); hist(randn(2000,1), 50) s = size(A) counts = hist(randn(2000,1), 5) [rows, cols] = size(A) [counts, centers] = hist(randn(2000,1), 5) 7 10 Optional Return Values nargin and nargout Functions can choose whether to return values, Inside a function, nargin is the number of input depending on if the user is asking for values. arguments supplied with the call. plot([1 2 3], [3 1 2]) no return value nargout is the number of output arguments requested with the call. h = plot([1 2 3], [3 1 2]) single return value set(h, 'LineStyle', '--') set(h, 'LineWidth', 8) 8 11 Variable Number of Arguments Testing nargin/nargout Some functions accept a variable number of function [x,y,z] = nargtest(p,q,r,s,t) arguments: if nargout >= 1 x = 50; if nargout >= 2 peaks y = 'foo'; Try: if nargout >= 3 a = nargtest(5,6,7) z = 3:7; [a, b] = nargtest(3) peaks(10) end [a, ~, c] = nargtest(9,8) end end whos % show the local workspace end 9 12

  3. Name Spaces Scripts Called By Functions ● Base workspace: variables created outside of ● Scripts do not have their own workspaces. any function exist in the base workspace. ● A script called from the keyboard executes in ● Local workspaces: each function executes in the base workspace. a separate local workspace holding the arguments, return variables, and any local ● A script called from within a function executes in variables created by the function. the function's local workspace. Functions cannot access variables of the base workspace. 13 16 Name Spaces (cont.) Resetting Variables ● Global workspace: variables declared global clear x removes variable x and by a function are accessed in the global undoes any global declaration workspace. You can also click on a variable in the workspace It's a good idea to also declare the variable pane and hit the Delete key, or right-click on the global in the base workspace. variable and choose from the menu. clear all clears everything clear global clears global declarations whos global shows all global variables 14 17 Global Variables Handle Graphics global pts Root = 0 pts = 0 : pi/20 : 2*pi ; Figure = 1, 2, ... function h = circ(x,y) % draws a circle centered on (x,y) global pts Axes hh = plot(x+cos(pts), y+sin(pts)); if nargout > 0 h = hh; % return h only if requested Line Text Image Surface end end 15 18

  4. Taking Apart A Figure 3D Graphics clf, plot(rand(5, 3)) peaks rotate3d on ax = get(gcf, 'Children') or click on the rotation arrow in the toolbar get(ax) set(gca, 'CameraViewAngleMode', 'manual') or right-click in the figure, lines = get(gca, 'Children') select Rotate Options, then get(lines(1)) select Fixed Aspect Ratio Axes 19 22 Multiple Axes: Subplot Plotting Surfaces clf [x, y, z] = peaks; subplot(2,2,1), plot(rand(5, 5)) surf(x, y, z, z) subplot(2,2,2), bar3(rand(5, 3)) subplot(2,2,3), a=rand(15, 1); pie(a, a > 0.7) surf(x, y, z, x) subplot(2,2,4), polar(cos(0:150)) surf(x, y, z, rand(length(x))) set(gca, 'Position', [0.32 0.1 0.4 0.4]) 20 23 Plotting in 3D Exploring Graphics Objects Don't type all this in! Download this file: www.cs.cmu.edu/~dst/Tutorials/Matlab/helix.m set(gca,'Units') or cd /afs/andrew/usr/dst/matlab set(gca) function helix pts = 0 : pi/20 : 4*pi; propedit(gca) click on “More Properties” x1 = cos(pts); y1 = sin(pts); x2 = cos(pts+pi); y2 = sin(pts+pi); z = pts/(2*pi); Matlab online documentation: Help pulldown menu or '?' icon: clf, whitebg(gcf, [0 0 0]), hold on plot3(x1, y1, z, 'y') > Documentation plot3(x2, y2, z, 'w') > MATLAB axis([ -3 3 -3 3 0 2]) > Graphics view(95, 9) > Graphics Objects end 21 24

  5. Helix (cont.) Surface Objects colors = 'rgbm'; sphere for i = 4 : 4 : length(pts)-4 plot3([x1(i) x2(i)], [y1(i) y2(i)], z([i i]), ... [x,y,z] = sphere(20); colors(ceil(rand(1)*length(colors))), 'LineWidth', 3) x(1 : 5 : 21*21) = NaN; end surf(x, y, z) axis off alpha(0.7) set(gcf, 'Color', 'k') Use the rotate tool to rotate the sphere; set set(gca, 'CameraViewAngleMode', 'manual') Fixed Aspect Ratio Axes first. az = -180 ; while true surf(x, y, z, rand(size(x))) view(az, 9), pause(0.05) shading interp, grid off, axis off az = az + 5 ; set(gcf, 'Color', 'w') end 25 28 Color Maps Data From Files Create a file temps.txt: clf reset, peaks, colorbar Use the “New Script” button. m = colormap; Enter this data: whos m 38 50 colormap(spring) 42 53 33 57 brighten(0.5) 45 56 colormap(jet) 44 46 41 40 colormap(bone) Save the file as temps.txt colormap(hot) load temps.txt plot(temps) 26 29 2D Data Importing Data From Files ● You can import data from Excel (and many [x, y] = meshgrid(-2 : 0.05 : 2) ; other file formats) using the Import Data button. z = sin(x) .* cos(y); contour(z, 20) Select the file you want to import; the wizard will guide you through the rest. imagesc(z) colormap(hot) ● There are also built-in functions specifically for imagesc(x(:), y(:), z) dealing with Excel files: surf(z), colormap(jet) doc xlsread doc xlswrite surfc(z) 27 30

  6. Curve Fitting for Extrapolation Debugging Poor man's debugger: x = randn(1, 2000); Remove semicolons from assignments. y = sin(x) + 0.2 * randn(1, 2000) ; Add 'quoted strings' in appropriate places. clf, hold on, plot(x, y, '.') Add a call to keyboard . (Use return to c = polyfit(x, y, 3) return from keyboard input mode.) Example polynomial representation: c = [ 5 -1 4 3] function y = buggy(vec) 5x 3 – x 2 + 4x + 3 p = vec > 5 Try: buggy([4 6]) 'got this far' Type 'return' to exit pts = min(x) : range(x)/100 : max(x); keyboard keboard mode and plot(pts, polyval(c, pts), 'r', 'LineWidth', 3) z = p * vec continue. y = sin(z) ; end 31 34 Saving Variables The Matlab Debugger clear all dbtype helix a = 'aardvark' dbstop helix 5 [x, y, z] = sphere(5); helix dbstep save stuff.mat dbstep 7 clear all whos whos -file stuff.mat Look at the Stack pulldown menu in the toolbar. load stuff.mat dbstep 30 dbquit save junk.dat x y -ascii dbclear helix type junk.dat doc debug 32 35 General Operating System Stuff Formatted Output pwd for i = 1 : 10 fprintf('The square root of %2d is %f \n', ... cd i, sqrt(i)) dir end ls *.m delete stuff.mat doc fprintf !ps -a title(sprintf('f(x) over range %g to %g', ... -3.5, 5.125)) 33 36

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend