0%

MATLAB 期末复习

本来说第十六周期末考,结果突然通知提前到第十周考试,这学期这门课我基本都翘了…..

1
2
3
4
5
6
clear %从工作空间清除所有变量
clear all %清理所有的变量和函数
who
whos
clc //清理命令窗口

1
string = num2str(23333) //将数字转换成字符串

矩阵

1
2
3
matrix = rand(6, 9) // 随机生成6*9的矩阵,数值在01之间
rows = size(matrix, 1)
cols = size(matrix, 2)
1
2
3
4
5
6
7
8
9
A = [3 6 2; 6 5 6; 8 7 9]
A(1, :) %得到第一行的元素
B = A' %转置矩阵
C = A(:) %变成一列
D = inv(A) %逆矩阵
rank(A) %矩阵的秩
A([1:3], [2:4])
A(:, 3) = [] %删除第三列
A*D

元胞数组

1
2
3
4
5
A = cell(1, 6)
A{3} = eye(3)
A{3}
A{5} = magic(3)
A{5}

数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
a = 1:0.5:5 %开始为1,结束为5,间隔0.5
linspace(a, b, n) %开始为a,结束为b,共n个数
c = {'哈哈哈哈', 'dax'; [1 2 3 4 5], 7}
c{1, 1} % 查询
c = cell(3, 3)
c{1, 1} = {1:3; 3:5}
c{1,1}{2}
c{2, 2} = 'sdfsdf'
celldisp(c) %单独显示出来

%图形化显示数组
c = {'你好', 'hello'; [1:6], 77}
figure;
c{2, 1}
out = cellplot(c, 'legend');
%将矩阵转换成单元数组
clear
A = [4 5 6; 6 6 7]
c = num2cell(A)
iscell(A)
iscell(c)

结构体

1
2
3
4
5
6
7
8
books = struct ('name', {'daxian', 'dashen'}, 'price', [30,40])
books.name
%结构体
s1 = struct('fenge', {'big', 'little'}, 'yanse', {'blue'}, 'shuju', {[8 8 9; 7 6 5] [1:9]})
diyi = s1(1)
dier = s1(2)

s1shan = rmfield(s1, 'yanse')

取整函数

1
2
3
4
5
6
7
8
9
10
round1 = round(5.5) %四舍五入
round2 = round(5.4)
fix1 = fix(-1.6) %向零取整

floor1 = floor(1.5) //向下取整
floor2 = floor(-1.5)

ceil1 = ceil(1.5) //向上取整
ceil2 = ceil(-1.5)

复数

绘制函数
$ y = sin(x^2) $ $ x \subset [0,5] $

1
2
3
4
x = 0:0.01:5
y = sin(x.^2)
plot(x, y)

按位运算

1
2
3
4
5
6
a = 12
dec2bin(a)
b = 8
dec2bin(b)
bitand(a, b)
dec2bin(bitand(a, b))

求积分

1
2
3
4
syms x
f = inline ('1./(sin(x)+exp(-x.^2))')
y = quad(f, 0, 1.1)

绘图

$ sin(x) $ 和 $ cos(x) $ 和 $ \frac{1}{1+e^x} $

1
2
3
4
5
x = 0:0.5:4*pi
y = sin(x); h = cos(x); w = 1./(1+exp(x));

plot(x, y, 'bd-', x, h, 'gp:', x, w, 'c^-');
legend('sinxx', 'cosxx', 'siggg');

1
2
3
4
5
6

t = 0:0.001:3*pi
y = exp(-t).*sin(2.*t)
subplot(3, 3, 1); plot(t, y)
title('函数图像')

1
2
3
4
5
t1 = 0:0.001:2*pi
r = sin(3*t1)
subplot(3, 3, 2);
polar(t1, r)
title('三叶玫瑰线')

1
2
3
f = 'u.^3 + v.^3 - 9*u*v';
subplot(3, 3, 3); ezplot(f);
title('叶形线');

1
2
3
4
5
6
7
t = 0:0.001:2*pi
x = 3*cos(t)
y = 3*sin(t)
z = 3*t
subplot(3, 3, 4);
plot3(x, y, z)
title('摆线图形')

1
2
3
4
5
6
x = -3:0.01:3;
y = x;
[x,y] = meshgrid(x, y);
z = x.^2/3 + y.^2/4;
subplot(3, 3, 5); mesh(x, y, z);
title('旋转抛物面');

求导

1
2
3
syms x;
y = (x+2)*log(x)/2*sqrt(x);
diff(y, x)

1
2
3
4
5
syms x y;
z = atan(y./x);
diff(z, y)
diff(z, x)

1
2
3
4
5
6
7
syms x y
f = log(x) + exp(-y./x) - exp(1);
a1 = diff(f, x);
a2 = diff(f, y);
dxdy = -a1./a2;
dxdy

1
2
3
4
5
6
syms t
x = 3*(t-sin(t));
y = 3*(1-cos(t));
a1 = diff(x, t)
a2 = diff(y, t)
dxdy = a2/a1

多项式

多项式求值

1
2
3
4
5
6
7
8
9
p = [1 2 -12 -1 7];
z = polyval(p, 3);

x = linspace(-1, 4, 5) %在[-1,4]区间产生5个离散点
p = [1 4 7 -8];
v = polyval(p, x)

%求根
roots([1 3 -12 -2 8])

数值微积分

计算多项式 $ y = x^5-3x^4-8x^3+7x^2+3x-5 $ 在 $[-4,5] $区间的微分

1
2
3
4
5
6
7
8
9
x = linspace(-4, 5);
p = [1 -3 -8 7 3 -5];
f = polyval(p, x);
subplot(2, 1, 1); plot(x, f);
dfb = diff(f) ./ diff(x)
xd = x(2:length(x));
subplot(2, 1, 2);
plot(xd, dfb);

求极限

$$ f(x) = {\lim_{x\to0}}\frac{sinx}{x} $$
$$ g(x) = {\lim_{y\to0}sin(x+2y)} $$

1
2
3
4
5
syms x y
f = sin(x) / x;
g = sin(x+2*y);
fx = limit(f)
gx = limit(g, y, 0)

方程组求解

1
2
3
4
5
6
7
L1 = 'x + y + z = 10';
L2 = '3*x + 2*y + z = 14';
L3 = '2*x + 3*y - z = 1';
g = solve(L1, L2, L3)
g.x
g.y
g.z

循环

求 $ s = \sum_{n=1}^{10}n $

1
2
3
4
5
s = 0;
for n = 1:10
s = s+n;
end
s

求 $ 1! + 2! +…+ 10! $ 的值

1
2
3
4
5
6
7
8
s = 0;
for i = 1:10
p = 1;
for(j = 1:i)
p = p*j;
end
s = s + p
end

模拟试题

求导

1
2
3
syms x;
y = (x+2)*log(x)/2*sqrt(x);
diff(y, x)

1
2
3
4
5
syms x y;
z = atan(y./x);
diff(z, y)
diff(z, x)

1
2
3
4
5
6
7
syms x y
f = log(x) + exp(-y./x) - exp(1);
a1 = diff(f, x);
a2 = diff(f, y);
dxdy = -a1./a2;
dxdy

1
2
3
4
5
6
syms t
x = 3*(t-sin(t));
y = 3*(1-cos(t));
a1 = diff(x, t)
a2 = diff(y, t)
dxdy = a2/a1

绘图

$ sin(x) $ 和 $ cos(x) $ 和 $ \frac{1}{1+e^x} $

1
2
3
4
5
x = 0:0.5:4*pi
y = sin(x); h = cos(x); w = 1./(1+exp(x));

plot(x, y, 'bd-', x, h, 'gp:', x, w, 'c^-');
legend('sinxx', 'cosxx', 'siggg');

1
2
3
4
5
6

t = 0:0.001:3*pi
y = exp(-t).*sin(2.*t)
subplot(3, 3, 1); plot(t, y)
title('函数图像')

1
2
3
4
5
t1 = 0:0.001:2*pi
r = sin(3*t1)
subplot(3, 3, 2);
polar(t1, r)
title('三叶玫瑰线')

1
2
3
f = 'u.^3 + v.^3 - 9*u*v';
subplot(3, 3, 3); ezplot(f);
title('叶形线');

1
2
3
4
5
6
7
t = 0:0.001:2*pi
x = 3*cos(t)
y = 3*sin(t)
z = 3*t
subplot(3, 3, 4);
plot3(x, y, z)
title('摆线图形')

1
2
3
4
5
6
x = -3:0.01:3;
y = x;
[x,y] = meshgrid(x, y);
z = x.^2/3 + y.^2/4;
subplot(3, 3, 5); mesh(x, y, z);
title('旋转抛物面');

1
2
3
4
5
x = -3:0.001:13;
y = 8.*(cos(x-pi/4).^2).*(x>=-3&x<0) + ...
(x/5+4).*(x>=0 & x<5) + ...
(x.*exp(-x)) .* (x>=5&x<13);
plot(x, y)

1
2
3
4
5
syms x;
y = sin(3*x) / x;
limit(y, x, 0);
ezplot('y=sin(3*x)/x')

计算下列积分

1
2
syms x;
f = int(exp(x)*sin(x), x)

1
2
syms x;
f = int(x*log(1+x), x, 0, 1)

1
2
syms t;
f = int(t*exp(-t), t, 0, inf)

1
2
syms x y
f = int(int(x*y, x, y^2, y+2), y, -1, 2)

求下列微分方程(组):

1
y = dsolve('(x^2-1)*Dy + 2*x*y - cos(x) = 0', 'y(0) = 1', 'x')

1
y = dsolve('D2y + 3*Dy + exp(x) = 0', 'x')

1
2
[x1,x2,x3] = dsolve('Dx1=x2','Dx2=-4*x1+4*x2+2*x3',...
'Dx3 = 2*x1-x2-x3', 'x1(0)=1', 'x2(0)=0', 'x3(0)=-1', 't')

矩阵

1
2
3
4
5
A = [4 9 2; 7 6 4; 3 5 7];
B = [37; 26; 28];
X = inv(A) * B
det(A) %求行列式
rank(A)

1
2
A = [4 2 3; 1 1 0; -1 2 3];
B = inv(A-2*eye(3)).*A

数值插值

1
2
3
4
5
x = [0 3 5 7 9 11 12 13 14 15]
y = [0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6]
x1 = 0:0.1:15;
y1 = interp1(x, y, x1, 'spline');
plot(x, y, '+', x1, y1, x, y, 'r:')

曲线拟合

1
2
3
4
5
t = [1 2 3 4 5]
y = [1.1 3 4 8 11];
a = polyfit(t, y, 3);
z = polyval(a, t)
plot(t, y, '*', t, z, 'r:')
求大佬赏个饭