数值计算·第三集:求解方程(组)的根(Matlab版)

版权声明:本文为博主原创文章,未经博主允许不得转载。若有任何问题,请联系QQ:575925154(加好友时,请备注:CSDN) https://blog.csdn.net/Miracle0_0/article/details/82817001

本集的数值案例如下:

 Example 1:
       syms p x r
       solve(p*sin(x) == r) 

%chooses 'x' as the unknown and returns
 
         ans =
                asin(r/p)
           pi - asin(r/p)
 
    Example 2:
       syms x y
       [Sx,Sy] = solve(x^2 + x*y + y == 3,x^2 - 4*x + 3 == 0) 

%returns
 
         Sx =
          1
          3
 
         Sy =
             1
          -3/2
 
    Example 3:
       syms x y
       S = solve(x^2*y^2 - 2*x - 1 == 0,x^2 - y^2 - 1 == 0) 

%returns
       %the solutions in a structure.
 
         S =
           x: [8x1 sym]
           y: [8x1 sym]
 
    Example 4:
       syms a u v
       [Su,Sv] = solve(a*u^2 + v^2 == 0,u - v == 1) 

%regards 'a' as a
       %parameter and solves the two equations for u and v.
 
    Example 5:
       syms a u v w
       S = solve(a*u^2 + v^2,u - v == 1,a,u) 
%regards 'v' as a
       %parameter, solves the two equations, and returns S.a and S.u.
 
       %When assigning the result to several outputs, the order in which
       %the result is returned depends on the order in which the variables
       %are given in the call to solve:
       [U,V] = solve(u + v,u - v == 1, u, v) 
       %assigns the value for u to U
       %and the value for v to V. In contrast to that
       [U,V] = solve(u + v,u - v == 1, v, u) 
       %assigns the value for v to U
       %and the value of u to V.
 
    Example 6:
       syms a u v
       [Sa,Su,Sv] = solve(a*u^2 + v^2,u - v == 1,a^2 - 5*a + 6) 
%solves
       %the three equations for a, u and v.
 
    Example 7:
       syms x
       S = solve(x^(5/2) == 8^(sym(10/3))) 
%returns all three complex solutions:
 
         S =
                                                         16
          - 4*5^(1/2) - 4 + 4*2^(1/2)*(5 - 5^(1/2))^(1/2)*i
          - 4*5^(1/2) - 4 - 4*2^(1/2)*(5 - 5^(1/2))^(1/2)*i
 
    Example 8:
       syms x
       S = solve(x^(5/2) == 8^(sym(10/3)), 'PrincipalValue', true)
       %selects one of these:
 
         S =
         - 4*5^(1/2) - 4 + 4*2^(1/2)*(5 - 5^(1/2))^(1/2)*i
 
    Example 9:
       syms x
       S = solve(x^(5/2) == 8^(sym(10/3)), 'IgnoreAnalyticConstraints', true)
      %ignores branch cuts during internal simplifications and, in this case,
      %also returns only one solution:
 
         S =
         16
 
    Example 10:
       syms x
       S = solve(sin(x) == 0) returns 0
       
       S = solve(sin(x) == 0, 'ReturnConditions', true) 
%returns a structure expressing
       %the full solution:
 
       S.x = k*pi
       S.parameters = k
       S.conditions = in(k, 'integer')
 
    Example 11:
       syms x y real 
       [S, params, conditions] = solve(x^(1/2) = y, x, 'ReturnConditions', true)
       %assigns solution, parameters and conditions to the outputs. 
       %In this example, no new parameters are needed to express the solution:
  
       S = 
       y^2
 
       params =
       Empty sym: 1-by-0
    
       conditions =
       0 <= y
 
    Example 12:
       syms a x y
       [x0, y0, params, conditions] = solve(x^2+y, x, y, 'ReturnConditions', true)
       %generates a new parameter z to express the infinitely many solutions.
       %This z can be any complex number, both solutions are valid without 
       %restricting conditions:
       
       x0 =
       -(-z)^(1/2)
       (-z)^(1/2)
 
       y0 =
       z
       z
 
       params =
       z
 
       conditions =
       true
       true
 
    Example 13:
       syms t positive
       solve(t^2-1)
 
         ans =
         1
 
       solve(t^2-1, 'IgnoreProperties', true)
 
         ans =
           1
          -1
 
    Example 14:
       solve(x^3-1) returns all three complex roots:
 
         ans =
                              1
          - 1/2 + (3^(1/2)*i)/2
          - 1/2 - (3^(1/2)*i)/2
 
       solve(x^3-1, 'Real', true) only returns the real root:
 
         ans =
         1

例1:解超越方程组.

\left\{\begin{matrix} x^x-4=0\\ 2xy+x=1 \end{matrix}\right.

e1 = sym('x^x-4=0');
e2 = sym('2*x*y+x=1');
[x,y] = solve(e1,e2);
fprintf('x = %f \n',x);
fprintf('y = %f \n',y);
%%结果
%x = 2.000000 
%y = -0.250000 

例2:解非线性方程组.

{\left\{\begin{matrix} x+y+z=u\\ 2xz-yu=-1\\ (x+y)^2-z=u\\xu+yz=4 \end{matrix}\right.}{}

e1 = sym('x+y+z=u');
e2 = sym('2*x*z-y*u=-1');
e3 = sym('(x+y)^2-z=u');
e4 = sym('x*u+y*z=4');
[x,y,z,u] = solve(e1,e2,e3,e4);
disp(double(x));
disp(double(y));
disp(double(z));
disp(double(u));

 

猜你喜欢

转载自blog.csdn.net/Miracle0_0/article/details/82817001