for ...
i=1:10, ...
i, end
i =
1
i =
2
i =
3
i =
4
i =
5
i =
6
i =
7
i =
8
i =
9
i =
10
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
Al= reshape(1:6,3,2)
Al =
1 4
2 5
3 6
Ar= reshape(1:9,3,3);
A = [Al, Ar]
A =
1 4 1 4 7
2 5 2 5 8
3 6 3 6 9
B = rand(5);
C = A*B;
[Al*B + Ar*B]
{??? Error using ==> mtimes
Inner matrix dimensions must agree.
}
Bt = B(1:2, :)
Bt =
0.8147 0.0975 0.1576 0.1419 0.6557
0.9058 0.2785 0.9706 0.4218 0.0357
Bb = B(3:end, :)
Bb =
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787
[Al*Bt + Ar*Bb]
ans =
12.6449 12.3427 12.5406 12.6299 10.1348
16.0381 15.1880 15.9116 15.8610 13.2881
19.4314 18.0333 19.2827 19.0921 16.4415
[Al*Bt + Ar*Bb] - C
ans =
1.0e-14 *
0 0 0 0 0
0 -0.1776 0.1776 -0.1776 0.1776
0 0 0 0 0
norm([Al*Bt + Ar*Bb] - C)
ans =
3.5527e-15
A
A =
1 4 1 4 7
2 5 2 5 8
3 6 3 6 9
Al = A(:, 2:1);
whos('Al')
Name Size Bytes Class Attributes
Al 3x0 0 double
size(Al)
ans =
3 0
Ar = A(:, 1:5);
Bt = B( 2:1, : )
Bt =
Empty matrix: 0-by-5
Bb = B( :, : );
C = Al*Bt + Ar*Bb
C =
12.6449 12.3427 12.5406 12.6299 10.1348
16.0381 15.1880 15.9116 15.8610 13.2881
19.4314 18.0333 19.2827 19.0921 16.4415
A*B
ans =
12.6449 12.3427 12.5406 12.6299 10.1348
16.0381 15.1880 15.9116 15.8610 13.2881
19.4314 18.0333 19.2827 19.0921 16.4415
Al*Bt
ans =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
n=5; M=rand(5); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.014046 seconds.
ans =
2.1831e-15
n=10; M=rand(5); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.000224 seconds.
ans =
2.3431e-15
n=10; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.000320 seconds.
ans =
1.0564e-14
n=10; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.000295 seconds.
ans =
6.0197e-15
n=10; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.000267 seconds.
ans =
7.6818e-15
n=10; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.000327 seconds.
ans =
9.8228e-15
n=10; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.000329 seconds.
ans =
4.5688e-15
n=50; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.002375 seconds.
ans =
4.3799e-14
n=100; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.010382 seconds.
ans =
1.1309e-13
n=100; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.009085 seconds.
ans =
1.4792e-13
n=200; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.061233 seconds.
ans =
3.1386e-13
n=400; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.292278 seconds.
ans =
1.0353e-12
n=400; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 0.313916 seconds.
ans =
8.1630e-13
n=800; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 2.645006 seconds.
ans =
4.7524e-12
n=800; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 2.553044 seconds.
ans =
2.2558e-12
n=1600; M=rand(n); M=M'+M; tic; [V,E]=eig(M); toc, norm(V*E - M*V)
Elapsed time is 21.999047 seconds.
ans =
6.4731e-12
cond(M)
ans =
9.7276e+04
ls
diary diary-2.txt files1.tgz my_LU2.m my_pi_up.m sqsqrt.m
diary-1.txt diary-3.txt my_LU.m my_pi_down.m sqrtsq.m
eps
ans =
2.2204e-16
eps = 10^-5;
[L,U]=my_LU( eps )
L =
1 0
100000 1
U =
1.0e+05 *
0.0000 -0.0000
0 1.0000
L*U
ans =
0.0000 -1.0000
1.0000 1.0000
format long
L*U
ans =
0.0000100 -1.0000000
1.0000000 1.0000000
norm(L*U- [eps -1; 1 1])
ans =
2.5262125e-13
whos('L')
Name Size Bytes Class Attributes
L 2x2 16 single
eps = 10^-7; [L,U]=my_LU( eps )
L =
1 0
10000000 1
U =
1.0e+07 *
0.0000000 -0.0000001
0 1.0000001
norm(L*U- [eps -1; 1 1])
ans =
1.1686097e-15
eps = 10^-8; [L,U]=my_LU( eps )
L =
1 0
100000000 1
U =
1.0e+08 *
0.0000000 -0.0000000
0 1.0000000
norm(L*U- [eps -1; 1 1])
ans =
1
for i=-10:5, eps = 10^i; [L,U]=my_LU( eps ); err(i+11)=norm(L*U- [eps -1; 1 1]), end
err =
1
err =
1 1
err =
1 1 1
err =
1.0000000 1.0000000 1.0000000 0.0000000
err =
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000
err =
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000
err =
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000
err =
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Column 9
0.0000000
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 10
0.0000000 0.0000000
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 11
0.0000000 0.0000000 0
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 12
0.0000000 0.0000000 0 0
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 13
0.0000000 0.0000000 0 0 0
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 14
0.0000000 0.0000000 0 0 0 0
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 15
0.0000000 0.0000000 0 0 0 0 0
err =
Columns 1 through 8
1.0000000 1.0000000 1.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
Columns 9 through 16
0.0000000 0.0000000 0 0 0 0 0 0
plot(err)
semilogx(err)
semilogx(-10:5, err)
{Warning: Negative data ignored}
semilogx(eps.^[-10:5], err)
semilogx(10.^[-10:5], err)
help semilogx
SEMILOGX Semi-log scale plot.
SEMILOGX(...) is the same as PLOT(...), except a
logarithmic (base 10) scale is used for the X-axis.
See also plot, semilogy, loglog.
Overloaded methods:
rfckt.semilogx
frd/semilogx
Reference page in Help browser
doc semilogx
loglog(10.^[-10:5], err)
eps=10^-8; M = [eps,-1;1 1];
[L,U,p]=lu(M)
L =
1.000000000000000 0
0.000000010000000 1.000000000000000
U =
1.000000000000000 1.000000000000000
0 -1.000000010000000
p =
0 1
1 0
eps=1; M = [eps,-1;1 1]; [L,U,p]=lu(M)
L =
1 0
1 1
U =
1 -1
0 2
p =
1 0
0 1
eps=10^-3; M = [eps,-1;1 1]; [L,U,p]=lu(M)
L =
1.000000000000000 0
0.001000000000000 1.000000000000000
U =
1.000000000000000 1.000000000000000
0 -1.001000000000000
p =
0 1
1 0
n = 5; M=rand(n); M = M*M'; eig(M)
ans =
0.002122042094936
0.036503072369417
0.424289661192421
0.730223994945836
6.616968134040453
n = 50; M=rand(n); M = M*M'; eig(M)
ans =
1.0e+02 *
0.000003052781306
0.000034862219834
0.000105954504473
0.000553129968032
0.000916233464170
0.001270620720536
0.001327127127376
0.002361638456497
0.002413864947737
0.003054527648606
0.004563683715413
0.005056594459612
0.006988412039191
0.007805351947375
0.008747411679026
0.009390165286812
0.011909388017863
0.012165406815620
0.013448599141512
0.015858928012830
0.016393843615531
0.019587932908484
0.020789251276507
0.022528694543295
0.024239817479549
0.025083576726190
0.029504787290914
0.030220505095467
0.035096681286575
0.038493022103322
0.039880511306966
0.041629280343113
0.042908737335537
0.045927693064895
0.049331572856452
0.058623851719545
0.065028683917715
0.067250970656745
0.072782877230926
0.075802833117740
0.081801688719732
0.086853197320759
0.091936265179025
0.099971066946734
0.113055479926636
0.115407962478305
0.124296854304360
0.138043513142896
0.169226659018650
6.042071328304185
n = 5; M=rand(n); M = M*M'; L = chol(M, 'lower')
L =
Columns 1 through 4
1.095018269701212 0 0 0
1.067618470417210 0.658909592061036 0 0
0.665674787006735 0.509959820010103 0.491155614453192 0
1.117510065496527 0.625365499480571 0.029535228058170 0.533744008979963
0.493592535995366 0.485196488487182 -0.542018160749440 0.125949455075882
Column 5
0
0
0
0
0.136298668670772
n = 4; M=rand(n); M = M*M'; L = chol(M, 'lower')
L =
1.548704887778185 0 0 0
0.836134619988236 0.574536847220415 0 0
0.498773155142443 0.171670781878044 0.218459762333100 0
0.754080908769234 0.406258779357970 -0.309968060205766 0.370512854154482
L*L'-M
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
n = 10; M=rand(n); M = M*M'; L = chol(M, 'lower'); L*L'-M
ans =
1.0e-15 *
Columns 1 through 4
-0.444089209850063 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0.444089209850063 0 0 0
0.444089209850063 0 0 0
0 0 0.444089209850063 0
0.444089209850063 0 0 0.444089209850063
0 0 0 0
Columns 5 through 8
0 0.444089209850063 0.444089209850063 0
0 0 0 0
0 0 0 0.444089209850063
0 0 0 0
0 0 0 0
0 -0.444089209850063 0 0
0 0 -0.888178419700125 0
0 0 0 -0.888178419700125
0 0 0 -0.444089209850063
0 0 0 0
Columns 9 through 10
0.444089209850063 0
0 0
0 0
0.444089209850063 0
0 0
0 0
0 0
-0.444089209850063 0
0 0
0 0
n = 10; M=rand(n); M = M*M'; L = chol(M, 'lower'); norm(L*L'-M)
ans =
1.254886840792266e-15
n = 100; M=rand(n); M = M*M'; L = chol(M, 'lower'); norm(L*L'-M)
ans =
7.912490720112384e-14
n = 1000; M=rand(n); M = M*M'; L = chol(M, 'lower'); norm(L*L'-M)
ans =
2.657923300839143e-12