To solve a system of equations, We could write these in matrix form

advertisement
To solve a system of equations,
2𝑥2 − 3𝑥3 = −5
𝑥1 + 𝑥2 + 𝑥3 = 6
2𝑥1 + 3𝑥2 = 𝑥3 = 5
We could write these in matrix form as,
0 2 −3 𝑥1
−5
(1 1 1 ) (𝑥2 ) = ( 6 )
2 3 −1 𝑥3
5
Or,
𝐴𝑋 = 𝑅
Where
0 2 −3
𝐴 = (1 1 1 )
2 3 −1
𝑥1
𝑥
𝑋 = ( 2)
𝑥3
−5
𝑅=( 6 )
5
To solve this set of linear equations by the Gaussian Elimination method we can use the matlab
function SysSolve(A,R), which will return the solution vector 𝑋 = [𝑥1 𝑥2 𝑥3 ]𝑇 giving
𝑋 = SysSolve(𝐴, 𝑅)
Start Matlab. Ensure that the current directory is set to that directory containing the files:
SysSolve.m, GaussianElimination.m, diagonalise.m.
Also there should be the files: Newton.m and myJacobian.m.
Enter the following lines in the Matlab command window.
>> A=[0 2 -3;1 1 1;2 3 -1]
>> R=[-5 6 5]
>> SysSolve(A,R)
The out put is,
ans =
1
2
3
i.e. 𝑋 = [1 2 3]𝑇
Newton’s Method for nonlinear equations
I have used just two variables [𝑥1 𝑥2 ] in this example here, because then they can be graphed (using
𝑥 = 𝑥1 , and 𝑦 = 𝑥2 , for example) and their intersection points, or solution points, can be easily
visualised.
Let the two nonlinear equations of interest be,
1
𝑓1 = 𝑥12 + (𝑒 𝑥1 − 𝑒 𝑥2 ) = 0
2
𝑥1
𝑓2 = 4𝑥2 − tan ( ) = 0
𝑥2
We can solve these nonlinear equations using the Matlab function Newton(F,X,X0,Tol), which will
return the solution vector 𝑋 = [𝑥1 𝑥2 𝑥3 ]𝑇 giving
𝑋 = Newton(𝐹, 𝑋, 𝑋0 , 𝑇𝑜𝑙)
Where,
𝐹 = [𝑓1 𝑓2 ]
𝑋 = [𝑥1 𝑥2 }
𝑋0 = [𝑥01 𝑥02 ]
Where 𝑥01 𝑎𝑛𝑑 𝑥02 are initial guesses for a starting value at which to begin the iteration process.
𝑇𝑜𝑙 = a tolerance − some numeric value
Enter the following lines into Matlab Command Window.
>> syms x1 x2 x3
>> f1=x1^2+1/2*(exp(x1)-exp(x2))
>> f2=4*x2-tan(x1/x2)
>> F=[f1 f2]
>> X=[x1 x2]
X0=[0.5 0.5]
>> Tol=0.01
>> Newton(F,X,X0,Tol)
Here is the output from Newton(F,X,X0,Tol)
Step
No. (k)
=======
0
1
2
3
4
Solution
Vector Xk
========
[0.5
0.5]
[0.282477
[0.236841
[0.191816
[0.187289
One-norm
Error
=====
0.321874]
0.285345]
0.247377]
0.243127]
0.395648
0.082165
0.082993
0.008777
ans =
0.1873 0.2431
So, the solution vector is
𝑋 = [0.1873 0.2431]
I include the graphs of 𝑓1 and 𝑓2 showing their intersection point (ignoring the trivial solution 𝑥1 =
𝑥2 = 0)
1
2
I have used 𝑥1 = 𝑥 and 𝑥2 = 𝑦, with the curve starting on the left is: 𝑥 2 + (𝑒 𝑥 − 𝑒 𝑦 ) = 0, and the
𝑥
other one is: 4𝑦 − tan (𝑦) = 0,
Download