【MATLAB】常用命令快速入门,国赛加油 (7)

image

L = imsubtract(I, J); imshow(L)

image

M = immultiply(I, 2); imshow(M)

image

直方图和直方图均衡化 % imhisteq 直方图均衡化 % imhist 直方图 I = imread('tire.tif'); imshow(I)

image

imhist(I)

image

J = histeq(I); imshow(J);

image

imhist(J)

image

标注连通分量 I = imread('coins.png'); imshow(I)

image

J = imsubtract(I, imopen(I, strel('disk',50))); % 开运算,先腐蚀后膨胀 % bwlabel 先二值化再标注连通分量 bw = imbinarize(J); imshow(bw)

image

L = bwlabel(bw, 8); max(max(L)) ans = 10 方程求解 方程和方程组的解析解(solve) solve(方程1,方程2,……,变量1,变量2……) % 多项式合并: (x + 3x - 5x)x/4 syms x (x+3*x-5*x)*x/4

-x24

方程1:ax^2+bx+c=0

syms a b c x solve(a*x^2 + b*x + c, x)

(-b+b2-4 a c2 a-b-b2-4 a c2 a)

方程2:\(2\mathrm{x}-\mathrm{x}^2=\mathrm{e}^{-\mathrm{x}}\)

syms x; y = 2*x - x^2 -exp(-x); solve(y, x)

0.41640296239270530567959997618172

方程组1:\( \begin{cases} \mathrm{x}+\mathrm{by}=5\\ \mathrm{ax}-\mathrm{y}=\mathrm{x}\\ \end{cases} \)

syms a b x y y1 = x + b*y -5; y2 = a*x -y -x; res = solve(y1, y2, x, y); res.x

5a b-b+1

res.y

5 a-1a b-b+1

方程组2:\( \begin{cases} \mathrm{e}^{-\mathrm{e}^{-\mathrm{x}_1-\mathrm{x}_2}}=\mathrm{x}_2\left( 1+{\mathrm{x}_1}^2 \right)\\ \mathrm{x}_1\cos \left( \mathrm{x}_2 \right) +\mathrm{x}_2\sin \left( \mathrm{x}_1 \right) =\frac{1}{2}\\ \end{cases} \)

syms x1 x2 y1 = exp(-exp(-x1-x2)) - x2*(1+x1^2); y2 = x1*cos(x2) + x2*sin(x1)-1/2; res = solve(y1,y2,x1,x2); res.x1

0.35324661959671746608371888721268

res.x2

0.60608173664146473530299588999127

方程和方程组的数值解(fsolve) fsolve(函数句柄,初值) % 初值一般根据经验给出

方程:\( 2\mathrm{x}-\mathrm{x}^2=\mathrm{e}^{-\mathrm{x}} \)

f = @(x)2*x-x^2-exp(-x); fsolve(f,0) % ans = 0.4164

方程组1:\( \begin{cases} \mathrm{x}+\mathrm{by}=5\\ \mathrm{ax}-\mathrm{y}=\mathrm{x}\\ \end{cases} \)

a = 3; b = 5; f = @(x)funs(x,a,b); fsolve(f,[0,0]) function y = funs(x,a,b) % x = [x,y] y(1) = x(1) + b*x(2) - 5; y(2) = a*x(1) - x(2)-x(1); end ans = 0.4545 0.9091

方程组2:\( \begin{cases} \mathrm{e}^{-\mathrm{e}^{-\mathrm{x}_1-\mathrm{x}_2}}=\mathrm{x}_2\left( 1+{\mathrm{x}_1}^2 \right)\\ \mathrm{x}_1\cos \left( \mathrm{x}_2 \right) +\mathrm{x}_2\sin \left( \mathrm{x}_1 \right) =\frac{1}{2}\\ \end{cases} \)

f = @fun; fsolve(f,[0,0]) function y = fun(x) y(1) = exp(-exp(-x(1)-x(2))) - x(2)*(1+x(1)^2); y(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 1/2; end function y = funs(x,a,b) % x = [x,y] y(1) = x(1) + b*x(2) - 5; y(2) = a*x(1) - x(2)-x(1); end ans = 0.3532 0.6061 常微分方程和常微分方程组的解析解 dsolve(方程1,方程2,……初值1,初值2……)

方程1:\( \mathrm{y}'=2\text{x,初值y}\left( 0 \right) =1 \)

syms y(x) % 声明y是x的函数 eqn = diff(y) == 2*x; % diff表示导数 cond = y(0) == 1; % 初值 dsolve(eqn, cond) % ans = x^2 + 1

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwydsz.html