PRACTICE WITH INDEXING A = 3 1 2 0 7 2 -3 0 0 0 0 0 0 0 0 44 A(1,[1 1 2 2]) ans = 3 3 1 1 A(1,end-1:end) ans = 2 0 B = [3 4 5 6; 0 1 2 3] B = 3 4 5 6 0 1 2 3 [A B] {??? Error using ==> horzcat CAT arguments dimensions are not consistent. } [A B'] ans = 3 1 2 0 3 0 7 2 -3 0 4 1 0 0 0 0 5 2 0 0 0 44 6 3 A A = 3 1 2 0 7 2 -3 0 0 0 0 0 0 0 0 44 B B = 3 4 5 6 0 1 2 3 C = [A B] C = 3 1 2 0 7 2 -3 0 0 0 0 0 0 0 0 44 3 4 5 6 0 1 2 3 C = [A; B] C = 3 1 2 0 7 2 -3 0 0 0 0 0 0 0 0 44 3 4 5 6 0 1 2 3 A A = 3 1 2 0 7 2 -3 0 0 0 0 0 0 0 0 44 A(3,:) = [] A = 3 1 2 0 7 2 -3 0 0 0 0 44 A(4,4) = [] {??? Subscripted assignment dimension mismatch. } A(:,end)=[] A = 3 1 2 7 2 -3 0 0 0 x = 3:3:10 x = 3 6 9 x = 3:4:10 x = 3 7 size(-1:44) ans = 1 46 A A = 3 1 2 7 2 -3 0 0 0 B B = 3 4 5 6 0 1 2 3 B(:,end) = [] B = 3 4 5 0 1 2 A A = 3 1 2 7 2 -3 0 0 0 B*A ans = 37 11 -6 7 2 -3 size(B) ans = 2 3 size(A) ans = 3 3 B B = 3 4 5 0 1 2 A A = 3 1 2 7 2 -3 0 0 0 B*A ans = 37 11 -6 7 2 -3 A A = 3 1 2 7 2 -3 0 0 0 inv(A) {Warning: Matrix is singular to working precision.} BECAUSE OF THE ROW OF ZEROS ans = Inf Inf Inf Inf Inf Inf Inf Inf Inf ELEMENT-BY-ELEMENT ARITHMETIC x = 0:100; y = sin(x); z = exp(-x); size(y*z') ans = 1 1 size(y'*z) ans = 101 101 q = y.*z; THIS IS WHAT WE WANT size(q) ans = 1 101 A A = 3 1 2 7 2 -3 0 0 0 MULTIPLY MATRIX BY SCALAR B=3*A; ELEMENT-BY-ELEMENT DIVIDE: B./A ans = 3 3 3 3 3 3 NaN NaN NaN A A = 3 1 2 7 2 -3 0 0 0 B B = 9 3 6 21 6 -9 0 0 0 f = pi; size(f) ans = 1 1 size(A) ans = 3 3 MULTPLY SCALAR BY MATRIX f*A ans = 9.4248 3.1416 6.2832 21.9911 6.2832 -9.4248 0 0 0 A*f ans = 9.4248 3.1416 6.2832 21.9911 6.2832 -9.4248 0 0 0 OR DIVIDE ... A/f ans = 0.9549 0.3183 0.6366 2.2282 0.6366 -0.9549 0 0 0 f/A {??? Error using ==> mrdivide Matrix dimensions must agree. } NEED ./ TO DIVIDE SCALAR BY MATRIX f./A ans = 1.0472 3.1416 1.5708 0.4488 1.5708 -1.0472 Inf Inf Inf A A = 3 1 2 7 2 -3 0 0 0 B B = 9 3 6 21 6 -9 0 0 0 TAKE LAST COL OUT OF B: B(:,end) = [] B = 9 3 21 6 0 0 A*B WORKS ... A*B ans = 48 15 105 33 0 0 A A = 3 1 2 7 2 -3 0 0 0 B B = 9 3 21 6 0 0 A.*B DOESN'T WORK .............. A.*B {??? Error using ==> times Matrix dimensions must agree. } EYE, ONES, ZEROS ... eye(3) ans = 1 0 0 0 1 0 0 0 1 pi*ones(2,3) ans = 3.1416 3.1416 3.1416 3.1416 3.1416 3.1416 zeros(3,3) ans = 0 0 0 0 0 0 0 0 0 INSERT expo.m HERE ... USES SERIES APPROX TO exp(x) 1 + x + x^2/2! + x^3/3! + ... ================================================================ function y = expo(x,tol) % % Power series for exp(x) % error(nargchk(1,2,nargin)) if nargin == 1 tol = 1e-4; end y = 1; err = 1; n = 0; while err > tol n = n+1; y1 = y +x^n/factorial(n); err = abs(y-y1)/y1; y = y1; end ===================================================================== RUN IT WITH ; LEFT OFF y1 TO SEE CONVERGENCE expo(2) y1 = 3 y1 = 5 y1 = 6.3333 y1 = 7 y1 = 7.2667 y1 = 7.3556 y1 = 7.3810 y1 = 7.3873 y1 = 7.3887 y1 = 7.3890 ans = 7.3890 COMPARE "EXACT" ANSWER ... exp(2) ans = 7.3891 PROBLEM WITH FACTORIAL WHEN n GETS TOO BIG ... factorial(10) ans = 3628800 prod(1:10) ans = 3628800 factorial(0) ans = 1 factorial(30) ans = 2.6525e+32 factorial(60) ans = 8.3210e+81 factorial(80) ans = 7.1569e+118 factorial(120) ans = 6.6895e+198 factorial(180) ans = Inf dtest type dtest.m done = 0; while (done < 0) x = input('Enter a number > 0'); done = x>0; end % Modify expression to add input arguments. % Example: % a = [1 2 3; 4 5 6]; % foo(a); FILE dtest.m INSERTED HERE =============================== done = 0; while ~done x = input('Enter a number > 0: ') done = x>0; end ================================================= dtest Enter a number > 0: -1 Enter a number > 0: -5 Enter a number > 0: 0 Enter a number > 0: 12 dtest Enter a number > 0: -2 Enter a number > 0: 0 Enter a number > 0: -pi Enter a number > 0: pi x x = 3.1416 3*done ans = 3 LOGICAL STUFF .................... y = exp(1) < pi y = 1 TWO CONDITIONS WITH "OR" BETWEEN THEN exp(1)3 ans = 1 exp(1)3 ans = 1 exp(1)==pi & pi>3 ans = 0 ftest2.m ======================= for i=0:2 for j=1:2 i*j end end ================================ ftest2 ans = 0 ans = 0 ans = 1 ans = 2 ans = 2 ans = 4 CHANGE dtest.m ============================================== done = 0; while ~done x = input('Enter a positive even number: '); done = x>0 & mod(x,2)==0; end =========================================== dtest Enter a positive even number: 3 Enter a positive even number: 7 Enter a positive even number: -12 Enter a positive even number: 102 MORE WITH mod FUNCTION y = 0:20; find(mod(y,3)==0) ans = 1 4 7 10 13 16 19 mod(y,3) ans = Columns 1 through 11 0 1 2 0 1 2 0 1 2 0 1 Columns 12 through 21 2 0 1 2 0 1 2 0 1 2 y(find(mod(y,3)==1)) ans = 1 4 7 10 13 16 19 find(mod(y,3)==1) ans = 2 5 8 11 14 17 20 TEST EACH y FOR DIVISIBILITY BY 3 mod(y,3)==1 ans = Columns 1 through 11 0 1 0 0 1 0 0 1 0 0 1 Columns 12 through 21 0 0 1 0 0 1 0 0 1 0 FIND THE LOCATIONS OF y THAT HAVE REMAINDER 1 WHEN DIV BY 3 find(mod(y,3)==1) ans = 2 5 8 11 14 17 20 SHOW THEIR VALUES y(find(mod(y,3)==1)) ans = 1 4 7 10 13 16 19 y y = Columns 1 through 11 0 1 2 3 4 5 6 7 8 9 10 Columns 12 through 21 11 12 13 14 15 16 17 18 19 20 z = (y-10).^2 z = Columns 1 through 11 100 81 64 49 36 25 16 9 4 1 0 Columns 12 through 21 1 4 9 16 25 36 49 64 81 100 PARABOLA ............................ plot(z) grid z' ans = 100 81 64 49 36 25 16 9 4 1 0 1 4 9 16 25 36 49 64 81 100 min(z) ans = 0 INDEX OUTPUT FROM min: 11th value is lowest (0) [zmin,ind]=min(z) zmin = 0 ind = 11 z(ind) ans = 0 [zmax,ind]=max(z) zmax = 100 ind = 1 z(end) ans = 100 zsort = sort(z) zsort = Columns 1 through 11 0 1 1 4 4 9 9 16 16 25 25 Columns 12 through 21 36 36 49 49 64 64 81 81 100 100 zsort(end) ans = 100 zsort(end:-1:1) ans = Columns 1 through 11 100 100 81 81 64 64 49 49 36 36 25 Columns 12 through 21 25 16 16 9 9 4 4 1 1 0 A A = 3 1 2 7 2 -3 0 0 0 A(2,:) = A(2,end:-1:1) A = 3 1 2 -3 2 7 0 0 0 INSERT convert.m ==================================================== function xconv = convert(x,xunits,aunits) % % Convert a length(s) from (ft, in, yards, mi) to (ft, in, yards, mi) % switch lower(xunits) case 'in' xind = 1; case 'ft' xind = 2; case 'yd' xind = 3; case 'mi' xind = 4; otherwise error(['unsupported unit ' xunits]); end switch lower(aunits); case 'in' aind = 1; case 'ft' aind = 2; case 'yd' aind = 3; case 'mi' aind = 4; otherwise error(['unsupported unit ' xunits]); end conv = [1 1/12 1/36 1/(5280*12) %% in to in,ft,yd,mi 12 1 1/3 1/5280 %% ft to in,ft,yd,mi 36 12 1 3/5280 %% yd to in,ft,yd,mi 5280*12 5280 5280/3 1]; %% mi to in,ft,yd,mi xconv = conv(xind,aind)*x; ========================================================================= USING ode45 FOR DIFF. EQ. SOLVING tspan = %% time span y0 = %% initial cond. etc = [a b omega]; [t,y] = ode45(@myfun,0:dt:tspan,y0,[],etc); INSERT myfun.m =============================== function ydot = myfun(t,y,etc) a = etc(1); b = etc(2); omega = etc(3); ydot = %% whatever =========================================== STRING (CHARACTER) ARRAYS size('Warren Gibson') ans = 1 13 name = 'Warren Gibson'; name(7)=[] name = Warren Gibson