Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

シンボリック方程式の求解

この例では、シンボリック方程式の求解に関する基礎を説明します。

2 次方程式の求解

関数 solve を使用して、2 次方程式を解きます。

解を求める変数を指定せずに、2 次方程式を解きます。関数 solve は、解の出力先として x を選択します。

disp('Solve a quadratic equation without specifying which variable to solve for. The solve function chooses x to return a solution.')
disp('>> syms a b c x')
disp('>> eqn = a*x^2 + b*x + c == 0')
disp('>> S = solve(eqn)')
syms a b c x
eqn = a*x^2 + b*x + c == 0
S = solve(eqn)
Solve a quadratic equation without specifying which variable to solve for. The solve function chooses x to return a solution.
>> syms a b c x
>> eqn = a*x^2 + b*x + c == 0
>> S = solve(eqn)
 
eqn =
 
a*x^2 + b*x + c == 0
 
 
S =
 
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
 

解を求める変数 a を指定し、それについて 2 次方程式を解きます。

disp('Solve for the variable a')
disp('Sa = solve(eqn,a)')
Sa = solve(eqn,a)
Solve for the variable a
Sa = solve(eqn,a)
 
Sa =
 
-(c + b*x)/x^2
 

多変数方程式の求解と構造体への出力の代入

多変数について解くときに、出力を個別の変数ではなく 1 つの構造体配列に保存する方が便利なことがあります。関数 solve は、単一の出力引数が指定され、かつ複数の出力が存在する場合に構造体を返します。

方程式系を解いて、その解を構造体配列に返します。

disp('Solve a system of equations to return solutions in a structure array')
disp('>> eqns = [2*u + v == 0, u - v == 1];')
disp('>> S = solve(eqns,[u v])')
syms u v
eqns = [2*u + v == 0, u - v == 1];
S = solve(eqns,[u v])
Solve a system of equations to return solutions in a structure array
>> eqns = [2*u + v == 0, u - v == 1];
>> S = solve(eqns,[u v])

S = 

  struct with fields:

    u: 1/3
    v: -2/3

構造体の要素を指定して、解にアクセスします。

disp('Access the solutions within the structure')
disp('>> S.u')
S.u
disp('>> S.v')
S.v
Access the solutions within the structure
>> S.u
 
ans =
 
1/3
 
>> S.v
 
ans =
 
-2/3
 

構造体配列を使用すると、解を他の式に簡単に代入できます。関数 subs を使用して、解 S を他の式に代入します。

disp('Use the subs function to substitute the solutions into other expressions')
disp('>> e1 = subs(u^2, S)')
e1 = subs(u^2,S)
disp('>> e2 = subs(3*v + u, S)')
e2 = subs(3*v + u,S)
Use the subs function to substitute the solutions into other expressions
>> e1 = subs(u^2, S)
 
e1 =
 
1/9
 
>> e2 = subs(3*v + u, S)
 
e2 =
 
-5/3
 

関数 solve が空のオブジェクトを返した場合、解は存在しません。

disp('The solve function returns an empty object if no solutions exist')
disp('>> solve([3*u+2, 3*u+1],u)')
S = solve([3*u+2, 3*u+1],u)
The solve function returns an empty object if no solutions exist
>> solve([3*u+2, 3*u+1],u)
 
S =
 
Empty sym: 0-by-1
 

方程式の数値的な求解

関数 solve が方程式をシンボリックに解くことができない場合は、関数 vpasolve を使用して数値解を求めようとします。関数 vpasolve は、最初に求めた解を返します。

次の方程式を解いてみます。関数 solve はシンボリック解を求めることができないため、数値解を返します。

disp('The following equation returns a numeric solution because the solve function cannot find a symbolic solution')
syms x
disp('>> eqn = sin(x) == x^2 - 1;')
eqn = sin(x) == x^2 - 1;
disp('>> solve(eqn,x)')
S = solve(eqn,x)
The following equation returns a numeric solution because the solve function cannot find a symbolic solution
>> eqn = sin(x) == x^2 - 1;
>> solve(eqn,x)
Warning: Unable to solve symbolically. Returning a numeric solution using <a
href="matlab:web(fullfile(docroot, 'symbolic/vpasolve.html'))">vpasolve</a>. 
 
S =
 
-0.63673265080528201088799090383828
 

方程式の左辺と右辺をプロットします。方程式には正の解もあることがわかります。

disp('Plot the left and right sides of the equation to see that the equation also has a positive solution')
disp('>> fplot([lhs(eqn) rhs(eqn)], [-2 2])')
fplot([lhs(eqn) rhs(eqn)], [-2 2])
Plot the left and right sides of the equation to see that the equation also has a positive solution
>> fplot([lhs(eqn) rhs(eqn)], [-2 2])

数値ソルバー vpasolve を直接呼び出して区間を指定することにより、他の解を求めます。

disp('Find the other solution by calling the numeric solver vpasolve')
disp('>> V = vpasolve (eqn,x,[0,2])')
V = vpasolve(eqn,x,[0 2])
Find the other solution by calling the numeric solver vpasolve
>> V = vpasolve (eqn,x,[0,2])
 
V =
 
1.4096240040025962492355939705895