%% 一、特殊矩阵的建立
% zeros
% ones
% eye
I = eye(10, 9);
I = eye(10);
% rand randn
S = rand(10, 1);
Sn = randn(1, 10);
% 从0到10之间均匀分布
S = 0 + (10 - 0)*rand(10, 1);
% 从a到b之间均匀分布的随机数
% S = a + (b - a)*rand
% 产生均值为u,方差为s的正态分布的随机矩阵
% y = u + sqrt(s)*randn
y = 0.6 + sqrt(5)*randn(5, 1);
% 魔方矩阵
magic(5)
% Hilbert矩阵和Toeplitz矩阵
hilb(4)
toeplitz(1:6)
%% 矩阵和向量的运算
A = [1 0 0 0; 3 1 0 0; -5 2 1 0; 7 03 2 1];
B = [1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7];
k = 3;
A + B;
k*A;
A\'
det(A)
inv(A)
a = [1 + 5i, 2, 3 + 6i, 7 - 2i];
b = [2 - i, 4 + 3i, 3 - i, 6];
s = sum(conj(b).*a)
s = a*b\'
s = dot(b, a)
%% 线性方程组的求解
A = [1, 2, 3; 1, 4, 9; 1 8 27];
b = [5, -2, 6]\';
x = inv(A)*b
x = A\b
%% 矩阵的相似化简和分解
A = [0 3 3; -1 8 6; 2 -14 -10];
jordan(A)
[V J] = jordan(A)
A = [1 0 i; 0 2 0; -i 0 1];
eig(A)
[E D] = eig(A)
%% 范数
A = [0 3 3; -1 8 6; 2 -14 -10];
norm(A, 1)
norm(A, 2)
norm(A, inf)
norm(A, \'fro\')
%% 矩阵分析
syms x
A = [sin(x) exp(x) 1; cos(x) x^2 + 1 log(x)];
diff(A)
diff(A, 2)
A = [0 1; 0 -2];
expA = funm(A, @exp)
expA = expm(A)
sinA = funm(A, @sin)
cosA = funm(A, @cos)
% 单分支条件
A = input(\'input A\');
if A > 10
disp(A);
end
% 双分支if语句
x = input(\'x\');
if x > 10
y = log(x);
else
y = cos(x);
end
% 多分支if语句
c = input(\'input a character\', \'s\');
if c >= \'A\' & c <= \'Z\'
disp(char(abs(c) + abs(\'a\') - abs(\'A\')));
elseif c >= \'a\' & c <= \'z\'
disp(char(abs(c) - abs(\'a\') + abs(\'A\')));
elseif c >= \'0\' & c <= \'9\'
disp(c);
end
% switch分支
price = input(\'input price\');
switch fix(price/100)
case {0, 1}
rate = 0;
case {2, 3, 4}
rate = 0.03;
case {5, 6, 7, 8, 9}
rate = 0.05;
otherwise
rate = 0.1;
end
% for循环
A = 1:100;
sumA = 0;
for k = 1:100
sumA = sumA + A(k);
end
% while循环
while 1
c = input(\'input a character\', \'s\');
if isempty(abs(c))
break;
end
end
%% 基本绘图操作
x = 0:0.01:2*pi;
y = sin(x);
plot(x, y);
% 第二个参数为矩阵
y1 = sin(x);
y2 = cos(x);
y3 = 0.002*exp(x);
y4 = x;
y5 = 0.002*tan(x);
z = [y1; y2; y3; y4; y5];
plot(x, z)
% 两个参数都是矩阵
x1 = 0:0.01:2*pi;
x2 = -pi:0.01:pi;
x = [x1; x2];
y1 = cos(x1);
y2 = sin(x2);
y = [y1; y2];
plot(x, y)
% plot只有一个参数
x = linspace(0, 2*pi, 200);
y = sin(x);
plot(y)
y2 = cos(x);
y3 = y + i*y2;
plot(y3)
axis equal % 下面讲解的坐标轴控制
% plot有多个参数
x1 = linspace(0, 2*pi, 200);
x2 = linspace(0, 2*pi, 100);
y1 = cos(x1);
y2 = sin(x2);
plot(x1, y1, x2, y2)
% plot含有的曲线选项
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y, \'b\') % r g y m k b
plot(x, y, \'p\') % * . p < >
plot(x, y, \':\') % - -- -. :
plot(x, y, \'r*:\')
%% 图形标注
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y)
xlabel(\'x\')
ylabel(\'y\')
title(\'正弦\');
text(2, 0.3, \'y = xin(x)\');
text(5, 0.5, \'x_2\')
x1 = linspace(0, 2*pi, 200);
x2 = linspace(0, 2*pi, 100);
y1 = cos(x1);
y2 = sin(x2);
plot(x1, y1, x2, y2)
legend(\'cos\', \'sin\');
xlim([0 10])
%% 图形保持
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
hold on
plot(x, y1, \'r\');
plot(x, y2, \'g\');
hold off
%% 窗口分割
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);
y4 = exp(x);
subplot(2, 2, 1)
plot(x, y1);
subplot(222)
plot(x, y2)
subplot(223)
plot(x, y3)
subplot(224)
plot(x, y4)
% line对象
% h = line([-pi:0.01:pi], sin([-pi:0.01:pi]));
% hp = plot([-pi:0.01:pi], sin([-pi:0.01:pi]));
hl = line(\'XData\', [-pi:0.1:pi], \'YData\', sin([-pi:0.1:pi]), ...
\'LineWidth\', 1, \'LineStyle\', \':\', \'Color\', \'r\');
set(hl, \'LineWidth\', 2, \'Marker\', \'p\', \'MarkerSize\', 15);
ht = text(0, 0, \'sin\');
set(ht, \'String\', \'cos\');
set(ht, \'FontSize\', 20);
hf = figure;
ha = axes(\'Parent\', hf, \'Units\', \'normalized\', \'Position\', [.1 .1 .6 .5]);
% 符号变量
a = sym(\'a\');
syms b;
% 符号常量
c = sym(\'3\');
f1 = \'3 * x + 4\';
syms x
f2 = 3 * x + 4;
f3 = sym(\'3*y + 4\');
% 符号四则运算
fadd1 = f1 + f2
fmul = f1 * f2
% 符号表达式化简
syms x y
s = (x^2 + y^2)^2 + (x^2 - y^2)^2;
simplify(s)
simple(s)
% 符号和数值的转换
eval(c)
numeric(c)
% 因式分解,展开和合并同类项
syms a b x y;
f1 = a^3 - b^3;
factor(f1)
f2 = (3*x^2 + 8*y^2)*(-x^2 + 3*y);
expand(f2)
f3 = 3*x^2 + 4*x^2 + 5*x^2*y;
collect(f3)
% 符号矩阵
a1 = [x x + y; y y^2]
% 符号函数值的求解
syms x
f1 = x^3 - 9;
subs(f1, 3)
% 符号极限,符号微分,符号积分
syms x a
y = (sin(x + a) - sin(x - a))/x;
limit(y, 0)
y2 = sqrt(1 + exp(x));
diff(y2)
diff(y2, 2)
diff(y2, 3)
y3 = (3 - x^2)^3;
int(y3)
y4 = abs(1 - x);
eval(int(y4, 1, 2))
% 符号级数求和
syms n
f = 1/n^2;
s1 = symsum(f, n, 1, inf)
% 泰勒展开
syms x
y = (1 + x + x^2)/(1 - x + x^2);
taylor(y, 6, 1)
% 代数方程
clear
syms x
solve(x + x*exp(x) - 10)
clear
eval(solve(\'x + x*exp(x) - 10\'))
% 方程组
[x y] = solve(\'x + y - 98\', \'x^(1/3) + y^(1/3) - 2\', \'x, y\')
[x y] = solve(\'1/x^3 + 1/y^3 - 28\', \'1/x + 1/y - 4\', \'x, y\')
% 微分方程
dsolve(\'Dy - (x^2 + y^2)/x^2/2\', \'x\')
请发表评论