-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.x5a b-b+1
res.y5 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.x10.35324661959671746608371888721268
res.x20.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