x_pts = rand(10,1); y_pts = x_pts.^2 + .5 * x_pts + randn(10,1) y_pts = -0.278750411789841 4.308278468140866 0.805023284605219 1.227888509457540 1.430800743225569 -0.146681725192621 0.092666019129331 2.062217763435908 2.804607247421031 2.830646566388258 '+' ans = + plot(x_pts, y_pts, '+') y_pts = x_pts.^2 + .5 * x_pts + .1 * randn(10,1) y_pts = 1.138286241727489 1.152606309540508 0.151342924791998 1.453966911563669 0.764947216430652 0.161753634098940 0.289498880683966 0.542176063171842 1.424959904330218 1.334725872582780 plot(x_pts, y_pts, '+') y_pts = 3 * x_pts.^2 + .5 * x_pts + .1 * randn(10,1) y_pts = 2.487525461882627 2.799566057654503 0.004983316867032 2.878504422360082 1.221385855773519 0.221150623603561 0.404451937115499 1.095186115829375 3.366241291436880 3.104322281791285 plot(x_pts, y_pts, '+') close all; plot(x_pts, y_pts, '+') n = 25; x_pts = rand(n,1); y_pts = 3 * x_pts.^2 + .5 * x_pts + .1 * randn(10,1) {Error using + Matrix dimensions must agree. } y_pts = 3 * x_pts.^2 + .5 * x_pts + .1 * randn(n,1); close all; plot(x_pts, y_pts, '+') y_pts = 3 * x_pts.^3 + .5 * x_pts + .1 * randn(n,1); close all; plot(x_pts, y_pts, '+') y_pts = x_pts.^5 + .5 * x_pts + .1 * randn(n,1); close all; plot(x_pts, y_pts, '+') y_pts = 3 * x_pts.^3 + .5 * x_pts + .1 * randn(n,1); M(:,1) = x_pts .^ 2 M = 0.192496613130303 0.145586856179197 0.586015952937965 0.632342882768395 0.034921370332937 0.239869163381811 0.198547062263974 0.417720507039084 0.503198463258302 0.569551987961545 0.076189843132071 0.461995728922051 0.429153394810510 0.026442576423009 0.014160448216269 0.248366728308060 0.921108465908117 0.115862442918032 0.342538340336927 0.050091784258784 0.564402194397763 0.065073517931178 0.255992538129684 0.488708264160413 0.793708605378865 M(:,2) = x_pts M = 0.192496613130303 0.438744359656398 0.145586856179197 0.381558457093008 0.586015952937965 0.765516788149002 0.632342882768395 0.795199901137063 0.034921370332937 0.186872604554379 0.239869163381811 0.489764395788231 0.198547062263974 0.445586200710899 0.417720507039084 0.646313010111265 0.503198463258302 0.709364830858073 0.569551987961545 0.754686681982361 0.076189843132071 0.276025076998578 0.461995728922051 0.679702676853675 0.429153394810510 0.655098003973841 0.026442576423009 0.162611735194631 0.014160448216269 0.118997681558377 0.248366728308060 0.498364051982143 0.921108465908117 0.959743958516081 0.115862442918032 0.340385726666133 0.342538340336927 0.585267750979777 0.050091784258784 0.223811939491137 0.564402194397763 0.751267059305653 0.065073517931178 0.255095115459269 0.255992538129684 0.505957051665142 0.488708264160413 0.699076722656686 0.793708605378865 0.890903252535798 M(:,3) = 1 M = 0.192496613130303 0.438744359656398 1.000000000000000 0.145586856179197 0.381558457093008 1.000000000000000 0.586015952937965 0.765516788149002 1.000000000000000 0.632342882768395 0.795199901137063 1.000000000000000 0.034921370332937 0.186872604554379 1.000000000000000 0.239869163381811 0.489764395788231 1.000000000000000 0.198547062263974 0.445586200710899 1.000000000000000 0.417720507039084 0.646313010111265 1.000000000000000 0.503198463258302 0.709364830858073 1.000000000000000 0.569551987961545 0.754686681982361 1.000000000000000 0.076189843132071 0.276025076998578 1.000000000000000 0.461995728922051 0.679702676853675 1.000000000000000 0.429153394810510 0.655098003973841 1.000000000000000 0.026442576423009 0.162611735194631 1.000000000000000 0.014160448216269 0.118997681558377 1.000000000000000 0.248366728308060 0.498364051982143 1.000000000000000 0.921108465908117 0.959743958516081 1.000000000000000 0.115862442918032 0.340385726666133 1.000000000000000 0.342538340336927 0.585267750979777 1.000000000000000 0.050091784258784 0.223811939491137 1.000000000000000 0.564402194397763 0.751267059305653 1.000000000000000 0.065073517931178 0.255095115459269 1.000000000000000 0.255992538129684 0.505957051665142 1.000000000000000 0.488708264160413 0.699076722656686 1.000000000000000 0.793708605378865 0.890903252535798 1.000000000000000 y_pts y_pts = 0.569165531883002 0.409434927342318 1.726570759201690 1.902639835552143 0.033197486106947 0.699188853722737 0.474880845814220 1.061638083545818 1.560674870603504 1.644366135419390 0.142200557378328 1.252535179583144 1.086168974640015 -0.017807342821571 0.317153991513710 0.786062932571265 3.162710351061859 0.162794792875764 0.807517004654424 0.127885976485014 1.726825466368285 0.044146925308094 0.408555499773177 1.229562346977968 2.600155443867529 pars = M \ y_pts pars = 5.206010527547564 -2.134257823427847 0.341161009710272 px = 0:.01:1; py = pars(1).*px.^2 + pars(2).*px + pars(3); hold on; plot( px, py ) close all plot(x_pts, y_pts, '+') hold on; plot( px, py ) my_fft n=4; M=rand(n); b=rand(4,1); x = inv(M)*b; n=4; M=rand(n); b=rand(4,1); x = inv(M)*b; A*x-b {Undefined function or variable 'A'. } n=4; M=rand(n); b=rand(4,1); x = inv(M)*b; M*x-b ans = 1.0e-15 * 0 -0.138777878078145 -0.055511151231258 0 n=4; M=rand(n); b=rand(4,1); x = inv(M)*b; norm(M*x-b) ans = 1.712768662514187e-15 n=4; M=rand(n); b=rand(4,1); x = inv(M)*b; norm(M*x-b)/norm(A) {Undefined function or variable 'A'. } n=4; M=rand(n); b=rand(4,1); x = inv(M)*b; norm(M*x-b)/norm(M) ans = 1.110835517688788e-15 n=100; M=rand(n); b=rand(4,1); x = inv(M)*b; norm(M*x-b)/norm(M) {Error using * Inner matrix dimensions must agree. } n=100; M=rand(n); b=rand(n,1); x = inv(M)*b; norm(M*x-b)/norm(M) ans = 2.237702545066385e-14 n=1000; M=rand(n); b=rand(n,1); x = inv(M)*b; norm(M*x-b)/norm(M) ans = 2.972238791179215e-14 n=2000; M=rand(n); b=rand(n,1); x = inv(M)*b; norm(M*x-b)/norm(M) ans = 4.191171590432875e-13 n=4; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U]=lu(M) L = 0.130549421184416 0.446516563570319 0.235428370633934 1.000000000000000 1.000000000000000 0 0 0 0.156954455961112 1.000000000000000 0 0 0.992163300346774 0.313685792969415 1.000000000000000 0 U = 0.668324682774067 0.415568350533194 0.830908622729425 0.739330955694736 0 0.817246075860006 0.374953772284727 0.214439588377251 0 0 -0.476349081699036 -0.411887928222916 0 0 0 0.565202234361948 n=4; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U,p]=lu(M) L = 1.000000000000000 0 0 0 0.068426848744128 1.000000000000000 0 0 0.865339881816867 -0.213053507188489 1.000000000000000 0 0.980253936589449 -0.625832061950171 -0.177860674964585 1.000000000000000 U = 0.660717701198427 0.924636162754205 0.556681097743108 0.841338986405045 0 0.897668112086984 0.652611129963731 0.931619651517016 0 0 -0.295235889766219 -0.072326419210734 0 0 0 0.404373065108938 p = 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 norm(L*U-A) {Undefined function or variable 'A'. } norm(L*U-M) ans = 1.460320210811606 norm(L*U-p*M) ans = 1.621375048933499e-16 n=4; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; x-x2 ans = 1.0e-15 * -0.222044604925031 0 0 0 n=4; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U,p]=lu(M); y=L\(p*b); norm(M*x2-b) ans = 0.583123793114304 n=4; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U,p]=lu(M); y=L\(p*b); norm(M*x2-p*b) ans = 1.221778202502620 n=4; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; norm(M*x2-b) ans = 1.509827417430662e-15 n=400; M=rand(n); b=rand(n,1); x=inv(M)*b; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; norm(M*x2-b) ans = 8.075595472720247e-13 n=50; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 0.000183 seconds. n=50; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 0.000168 seconds. n=500; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 0.006120 seconds. n=500; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 0.007582 seconds. n=5000; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 2.196707 seconds. n=5000; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 2.225997 seconds. n=500; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.004642 seconds. n=50; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.002376 seconds. n=50; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.000123 seconds. n=50; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.000105 seconds. n=500; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.003161 seconds. n=500; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.004288 seconds. n=5000; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 0.955545 seconds. n=5000; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 1.062510 seconds. n=10000; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 17.713188 seconds. n=10000; M=rand(n); b=rand(n,1); tic; x=inv(M)*b; toc Elapsed time is 17.783285 seconds. n=10000; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 6.098289 seconds. n=10000; M=rand(n); b=rand(n,1); tic; [L,U,p]=lu(M); y=L\(p*b); x2=U\y; toc Elapsed time is 6.139872 seconds. n=10000; tic; M=rand(n); toc Elapsed time is 1.159052 seconds. C = reshape(1:35,5,7) C = 1 6 11 16 21 26 31 2 7 12 17 22 27 32 3 8 13 18 23 28 33 4 9 14 19 24 29 34 5 10 15 20 25 30 35 C(2,:) = [] C = 1 6 11 16 21 26 31 3 8 13 18 23 28 33 4 9 14 19 24 29 34 5 10 15 20 25 30 35 a = ones(5,0) a = Empty matrix: 5-by-0 a = ones(5,[]) a = Empty matrix: 5-by-0 a' * a ans = [] a * a' ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 help for for Repeat statements a specific number of times. The general form of a for statement is: for variable = expr, statement, ..., statement END The columns of the expression are stored one at a time in the variable and then the following statements, up to the END, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars. Some examples (assume N has already been assigned a value). for R = 1:N for C = 1:N A(R,C) = 1/(R+C-1); end end Step S with increments of -0.1 for S = 1.0: -0.1: 0.0, do_some_task(S), end Set E to the unit N-vectors for E = eye(N), do_some_task(E), end Long loops are more memory efficient when the colon expression appears in the for statement since the index vector is never created. The BREAK statement can be used to terminate the loop prematurely. See also parfor, if, while, switch, break, continue, end, colon. Reference page in Help browser doc for for i = 1:5, i^2 end for i = 1:5, i^2 end | {Error: Illegal use of reserved keyword "end". } for i = 1:5, i^2, end ans = 1 ans = 4 ans = 9 ans = 16 ans = 25 for i = [1 2 3 4 5], i^2, end ans = 1 ans = 4 ans = 9 ans = 16 ans = 25 for i = [1 2 3 2 1], i^2, end ans = 1 ans = 4 ans = 9 ans = 4 ans = 1 help while while Repeat statements an indefinite number of times. The general form of a while statement is: while expression statements END The statements are executed while the real part of the expression has all non-zero elements. The expression is usually the result of expr rop expr where rop is ==, <, >, <=, >=, or ~=. The BREAK statement can be used to terminate the loop prematurely. For example (assuming A already defined): E = 0*A; F = E + eye(size(E)); N = 1; while norm(E+F-E,1) > 0, E = E + F; F = A*F/N; N = N + 1; end See also for, if, switch, break, continue, end. Reference page in Help browser doc while help if if Conditionally execute statements. The general form of the if statement is if expression statements ELSEIF expression statements ELSE statements END The statements are executed if the real part of the expression has all non-zero elements. The ELSE and ELSEIF parts are optional. Zero or more ELSEIF parts can be used as well as nested if's. The expression is usually of the form expr rop expr where rop is ==, <, >, <=, >=, or ~=. Example if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end See also relop, else, elseif, end, for, while, switch. Reference page in Help browser doc if fun(10) ans = 1.5497677 fun(10) {Undefined function or variable "pi6". Error in fun (line 6) pi6 = pi6 + 1/(single(i)^2); } fun(10) ans = 0.0100000 fun(10) fun(10) out = fun(10) Error in fun (line 5) for i = 1:n, {Output argument "pi6" (and maybe others) not assigned during call to "/Users/pauldj/works/courses/LSC/LSC-14/matlab/fun.m>fun". } out = fun(10) out = 0.0100000 out = fun(10); fun(10); fun(10) ans = 0.0100000 fun(10) ans = 0.0100000 out1 = fun(10) out1 = 0.0100000 [out1,out2] = fun(10) out1 = 0.0100000 out2 = Columns 1 through 4 0.619489037062918 0.904903972865043 0.584558922638576 0.629318022752374 0.085686192046266 0.769838252864667 0.553827130053070 0.991735379974794 0.486720264896526 0.733331599320730 0.345886189664853 0.330930299636738 0.241540507702473 0.222553783541366 0.558614895540223 0.813159930162063 0.344962719554239 0.422670130692573 0.617284582720012 0.168454258693608 0.280567538148654 0.533228653494366 0.031075200557722 0.132614162357398 0.901709549706691 0.781693879965064 0.737912879225141 0.141596286418039 0.576239608370405 0.282683462505017 0.890375742498582 0.270996389141598 0.191734574980332 0.525815156235904 0.662786244085994 0.650839933247640 0.205008913689200 0.863115483106720 0.138794025949360 0.348429867990226 Columns 5 through 8 0.272751001730310 0.001301870003024 0.558386694972771 0.199108963070011 0.372783307254474 0.620685704275907 0.977504677639645 0.555609994544617 0.479146472964448 0.693187593786768 0.996003354201565 0.535430058539208 0.407228454568137 0.395478209712309 0.907015570417146 0.387118298290959 0.616584369867613 0.545091828927819 0.055991714855854 0.718332940928266 0.326801412548592 0.524557096749591 0.036029808678857 0.889375689165753 0.865859229116291 0.673196887904239 0.274115532929493 0.062451720894690 0.668846860880698 0.295274404820520 0.480341941726175 0.780458972886156 0.614038509781014 0.931224494542981 0.723113057810490 0.489835475947501 0.392436314870910 0.358123189970843 0.028690232365092 0.949042084829540 Columns 9 through 10 0.852603173922383 0.429466874728425 0.148429829114078 0.809575137809497 0.569709605451495 0.086916218418680 0.603644020981215 0.237908518724660 0.288920606680620 0.624171345326556 0.152255818548895 0.718842433031686 0.143632105971649 0.685200278539959 0.446475659180735 0.570963992801686 0.740068966541718 0.886786719529678 0.922711964037460 0.269795008999380 [out1,out2] {Error using horzcat Dimensions of matrices being concatenated are not consistent. } [[1:10]',out2] ans = Columns 1 through 4 1.000000000000000 0.619489037062918 0.904903972865043 0.584558922638576 2.000000000000000 0.085686192046266 0.769838252864667 0.553827130053070 3.000000000000000 0.486720264896526 0.733331599320730 0.345886189664853 4.000000000000000 0.241540507702473 0.222553783541366 0.558614895540223 5.000000000000000 0.344962719554239 0.422670130692573 0.617284582720012 6.000000000000000 0.280567538148654 0.533228653494366 0.031075200557722 7.000000000000000 0.901709549706691 0.781693879965064 0.737912879225141 8.000000000000000 0.576239608370405 0.282683462505017 0.890375742498582 9.000000000000000 0.191734574980332 0.525815156235904 0.662786244085994 10.000000000000000 0.205008913689200 0.863115483106720 0.138794025949360 Columns 5 through 8 0.629318022752374 0.272751001730310 0.001301870003024 0.558386694972771 0.991735379974794 0.372783307254474 0.620685704275907 0.977504677639645 0.330930299636738 0.479146472964448 0.693187593786768 0.996003354201565 0.813159930162063 0.407228454568137 0.395478209712309 0.907015570417146 0.168454258693608 0.616584369867613 0.545091828927819 0.055991714855854 0.132614162357398 0.326801412548592 0.524557096749591 0.036029808678857 0.141596286418039 0.865859229116291 0.673196887904239 0.274115532929493 0.270996389141598 0.668846860880698 0.295274404820520 0.480341941726175 0.650839933247640 0.614038509781014 0.931224494542981 0.723113057810490 0.348429867990226 0.392436314870910 0.358123189970843 0.028690232365092 Columns 9 through 11 0.199108963070011 0.852603173922383 0.429466874728425 0.555609994544617 0.148429829114078 0.809575137809497 0.535430058539208 0.569709605451495 0.086916218418680 0.387118298290959 0.603644020981215 0.237908518724660 0.718332940928266 0.288920606680620 0.624171345326556 0.889375689165753 0.152255818548895 0.718842433031686 0.062451720894690 0.143632105971649 0.685200278539959 0.780458972886156 0.446475659180735 0.570963992801686 0.489835475947501 0.740068966541718 0.886786719529678 0.949042084829540 0.922711964037460 0.269795008999380 fun(10) fun(10) ans = -1 p = 10; fun(10) ans = -1 p p = 10 p = 10; fun(p) ans = -1 p p = 10 out2 out2 = Columns 1 through 4 0.619489037062918 0.904903972865043 0.584558922638576 0.629318022752374 0.085686192046266 0.769838252864667 0.553827130053070 0.991735379974794 0.486720264896526 0.733331599320730 0.345886189664853 0.330930299636738 0.241540507702473 0.222553783541366 0.558614895540223 0.813159930162063 0.344962719554239 0.422670130692573 0.617284582720012 0.168454258693608 0.280567538148654 0.533228653494366 0.031075200557722 0.132614162357398 0.901709549706691 0.781693879965064 0.737912879225141 0.141596286418039 0.576239608370405 0.282683462505017 0.890375742498582 0.270996389141598 0.191734574980332 0.525815156235904 0.662786244085994 0.650839933247640 0.205008913689200 0.863115483106720 0.138794025949360 0.348429867990226 Columns 5 through 8 0.272751001730310 0.001301870003024 0.558386694972771 0.199108963070011 0.372783307254474 0.620685704275907 0.977504677639645 0.555609994544617 0.479146472964448 0.693187593786768 0.996003354201565 0.535430058539208 0.407228454568137 0.395478209712309 0.907015570417146 0.387118298290959 0.616584369867613 0.545091828927819 0.055991714855854 0.718332940928266 0.326801412548592 0.524557096749591 0.036029808678857 0.889375689165753 0.865859229116291 0.673196887904239 0.274115532929493 0.062451720894690 0.668846860880698 0.295274404820520 0.480341941726175 0.780458972886156 0.614038509781014 0.931224494542981 0.723113057810490 0.489835475947501 0.392436314870910 0.358123189970843 0.028690232365092 0.949042084829540 Columns 9 through 10 0.852603173922383 0.429466874728425 0.148429829114078 0.809575137809497 0.569709605451495 0.086916218418680 0.603644020981215 0.237908518724660 0.288920606680620 0.624171345326556 0.152255818548895 0.718842433031686 0.143632105971649 0.685200278539959 0.446475659180735 0.570963992801686 0.740068966541718 0.886786719529678 0.922711964037460 0.269795008999380 BR = out2(5:end, 5:end) BR = Columns 1 through 4 0.616584369867613 0.545091828927819 0.055991714855854 0.718332940928266 0.326801412548592 0.524557096749591 0.036029808678857 0.889375689165753 0.865859229116291 0.673196887904239 0.274115532929493 0.062451720894690 0.668846860880698 0.295274404820520 0.480341941726175 0.780458972886156 0.614038509781014 0.931224494542981 0.723113057810490 0.489835475947501 0.392436314870910 0.358123189970843 0.028690232365092 0.949042084829540 Columns 5 through 6 0.288920606680620 0.624171345326556 0.152255818548895 0.718842433031686 0.143632105971649 0.685200278539959 0.446475659180735 0.570963992801686 0.740068966541718 0.886786719529678 0.922711964037460 0.269795008999380 whos(BR) {Error using whos Argument must contain a string. } whos('BR') Name Size Bytes Class Attributes BR 6x6 288 double diary off