이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.
기호 방정식 풀기
이 예제에서는 기호 방정식 풀기에 대한 기본 사항을 보여줍니다.
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
다변량 방정식을 풀고 구조체에 출력 할당하기
여러 개의 변수에 대해 해를 구할 때는 개별 변수보다 구조체형 배열에 출력값을 저장하는 것이 더 편리할 수 있습니다. 출력 인수를 한 개 지정했는데 출력값이 여러 개 존재하면 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