Professional Documents
Culture Documents
: ..
Matlab
a=2
2 a.
b = 3;
3 b, .
c=a+b
a b.
x = 1, y = -2 , (
).
b
: 3.
clear b
b .
, b.
clear
v = [7 -2 5]
, .
v = [7,-2, 5]
w = [1; 2; 3] , .
= [4 -2 5; -1 7 -6; 2 3 5]
= [ 4 -2 5
-1 7 -6
2 3 5]
3x3.
( Enter ).
va = abs(v)
va
v.
sq = sqrt(v)
sq
v.
, sq .
e4, e .
v'
v.
C1 = eye(4)
4.
C2 = eye(5,3) 5 3 .
C3 = ones(3) 3 .
C4 = eye(2,3) 5 3 .
C5 = zeros(2) 2 .
1 = diag(v) ()
v, v .
A2 = diag(v,k) 0,
k- v.
k = 0 v , k = 1
, k = -1
.
v.
w = diag(A,k) k-
-. To k .
diag ,
.
: ( ),
, ,
. .. sqrt, abs, sin .
u1 = [4:10]
4,5,6,7,8,9,10 ( 1).
v1 = [3:2:8]
3,5,7 ( 2).
t1 = [1:-1/2:-1]
1,1/2,0,-1/2,-1 ( -).
u2 = linspace(4,10,7) 7 4 10 -
, u1.
t1 = logspace(0,3,4) 4 100 103.
n = length(t1)
t1
.
[nr,nc] = size(A)
nr nc .
(1,2)
(4,5) = 3
4,5 3 (
4 5,
).
= [1:5, 2:6] [1 2 3 4 5 2 3 4 5 6] .
1 2 3 4 5
= [1:5; 2:6]
.
2 3 4 5 6
(2,:)
2 .
(:,5)
5 .
(1:2, end-1:end)
4 2
4 5
2 ,
.
5 6
(:, 2:4) = [] 2, 3 4 , .
v = rand(1,3) 1 3
0 1.
= rand(3)
3 .
= rand(3)
3x3 ( ).
S = +
, .
P = *
(m x n) (n x t)
P(m x t).
1.*2
1 2, ,
1, 2.
X1./X2
1 2.
^3
/ ** ( ).
3 = 1.^3
3 1
3 1.
4 = 1.^2 1 2, ,
4 1
2.
A^(-1)
inv(A)
A\B
: inv(A)*B,
,
.
Ax = b .
max(A)
, -
.
( ), .
max(A,[],1)
, -
.
max(A,[],2)
, -
.
min(A)
max, .
sum(A)
, -
.
( ),
.
sum(A,1)
, -
.
sum(A,2)
, -
.
prod(A)
sum, .
C = reshape([1 2 3;
4 5 6;
7 8 9;
10 11 12], 2,6) 4x3 2 6
1 7 2 8 3 9
C =
,
4 10 5 11 6 12
.
= C(:)
C -.
z1 = 1-2*i
1 -2.
z2 = 1-2j
z1 ( i j ).
real(z1)
imag(z1)
angle(z1)
s = 'A string example' .
s = num2str(pi,3)
s '3.142'.
2
.
n = str2num('2.34')
n 2.3400...
.
x 3 2 x + 12 x = 1.5.
roots([1 0 -2 12])
poly([0 -1 2])
(
).
2
t = [0: pi/20: 2*pi];
plot(t, cos(t))
t [0,2 ] /20
.
grid on
xlabel('t'); ylabel('Cos')
x y .
t [0,2 ]
y [ 1,1] .
plot(t,cos(t),'k-', t,sin(t),'ro')
.
legend('Cos', 'Sin')
semilogy(t, abs(cos(t)))
,
.
semilogx(t, abs(cos(t)))
, .
loglog(t, abs(cos(t)))
subplot(2,2,1)
4 (2 x 2 )
1,
, plot, loglog .
3
x = linspace(-20, 20, 80);
y = linspace(-2*pi, 2*pi, 160);
[,] = meshgrid(x,y)
x y X Y
length(y) x length(x)
X ,
Y.
- x 160
- y'
80 .
surf(X, Y, X.^2.*cos(Y))
x 2 cos ( y ) ,
x [ 20, 20] y [ 2 , 2 ] .
. (.* ./ .^ .\ .),
.
clc
(command window),
.
help exp
exp.
lookfor exponential
exponential,
help.
Ctrl+C Ctrl+Break
File > New > M-file
m-file
F5 m-file editor
(command window), m-file.
out.
: m-file
, namefun.m
, % .
m-file
help.
%This function finds the sum and the product of two numbers
function [sum1,prod1] = praxeis(a1,a2)
sum1 = sum([a1,a2]);
prod1 = prod([a1,a2]);
praxeis.m
[s,p] = praxeis(2,3) s
5 p 6.
x = input('Enter a value for x')
( )
eps
Matlab (.. 2.22044604925031e-016)
format long
15
.
format short
, 4 .
save('xyfile','x','y') xyfile.mat x y.
load xyfile
workspace (x
y) xyfile.mat.
.
D = load('xyfile.txt') x y,
xyfile.txt D. x y
( ).
-ascii.
fnum = fopen('results.dat','wt')
results.dat (
, )
( , -1
) fnum,
.
'wt', 'rt',
'at', ,
, .
.
%7.4f () 4
,
7 . %11.3e
a 10b . \t
tab \n .
.
, %
. , .
:
Exapmle One:
-314.1593 3.142e+000
...
a = fgetl(fnum)
fnum
a .
: a = Example One:
2
fnum - b.
3 3 (
).
2 ,
.. b =
fscanf(fnum,'%f',2).
format:
b = fscanf(fnum,'%f %f',3)
b = -314.1593
3.1420
fgetl fscanf,
fgetl
fscanf.
fclose(fnum)
fnum .
type results.dat
results.dat
command window.
x=3<4, y=4>=5
x=1 y=0. To 1 0
. 0 1 logical double.
== (), ~ (), & (), | (). ..
a = 3==3 & (4~=4 | ~1) a=0.
if x>0
disp('Positive')
elseif x==0
disp('Zero')
elseif x<0
, , ,
(
, .. NaN Not a Number) .
To disp command window.
disp('Negative')
else
disp('Error')
end
switch sign(x)
case 1
disp('Positive')
case 0
disp('Zero')
.
,
, 0 1 .
sign 1,0,-1 ,
.
case -1
disp('Negative')
otherwise
disp('Error')
end
k = 1 2 3 4 5 6 7 8 9 10
for k=1:10
fprintf('%3.0f',k)
end
k=1;
while k<=10
fprintf('%3.0f',k)
k=k+1;
end
break
return
y = [3 6 9 12 15];
x = [3 4 1
2];
z = y(x)
z = 9 12 3 15 6. x .
zb = z>10
zb = 0 1 0 1 0. To zb logical array.
z(zb)
12 15. To zb ().
zb = [0 1 0 1 0]
z(zb), , 0 1
0.
zi = find(z>10)
2 4, ()
.
z(zi)
12 15. z(zb).
, 2 4 1 length(z)
5.
z(z>10)
all(zb)
0 (false). 1,
0.
any(zb)
1 (true). 1,
0.
nargin, nargout
global V
V
( workspace).
feval('cos',0)
1, , cos(0).
trig_fun
workspace ,
.
trig_fun = inline('cos(x)*sin(x)')