
- •Затверджено
- •Contents Contents
- •The purpose and the contents of laboratory works
- •Laboratory work №1 Solution of Nonlinear Equations by the Bisection method and Chord method
- •1.1 Purpose of the work
- •1.2 Tasks for laboratory work
- •1.3 The basic theoretical knowledge
- •1.3.1 Bisection method
- •Figure 1.1 – Bisection method
- •Chord method
- •Figure 1.4 – Chord method
- •1.3.3 Matlab function fzero and roots
- •1.4 Individual tasks
- •1.5 Control questions
- •Laboratory work №2 Solution of Nonlinear Equations by the newton method and simple iteratIvE method
- •Figure 2.1 – Newton method
- •Figire 2.2 - Dependence of the number of iterations on the accuracy of methods for the bisection (upper line) and the Newton method (bottom line)
- •2.3.2 The method of simple iteration
- •A sufficient condition for the convergence of the iterative process
- •Individual tasks
- •Laboratory work №3 Solution system of Linear Algebraic Equations
- •3.3.1 Direct methods
- •Inverse matrix:
- •3.3.2 Iterative methods
- •Condition number of a
- •3.4 Individual tasks
- •3.5 Control questions
- •Laboratory work №4
- •Visualization of 3d data in matlab
- •Plot3(X, y, z, 'style')
- •4.3.2 Instructions: meshgrid, plot3, meshc, surfc
- •4.3.3 Instructions: sphere, plot3, mesh
- •4.3.4 The simple animation in 3d
- •1. Working with a sphere
- •4.3.5 Summary of 3d Graphics
- •Individual tasks
- •Laboratory work №5 Solving systems of nonlinear equations
- •5.1 Purpose of the work
- •5.2 Tasks for laboratory work
- •5.3 The basic theoretical knowledge
- •5.3.1 Newton method to solve systems of non-linear equations
- •5.3.2 Matlab function for Newton method for a system of nonlinear equations
- •5.3.3 The matlab routine function fsolve
- •Input Arguments
- •Individual tasks
- •5.5 Control questions
- •List of the literature
- •Appendix a.
- •Individual tasks to Lab number 1, 2
- •Appendex b. The task for self-examination to Lab number 1, 2
Laboratory work №5 Solving systems of nonlinear equations
5.1 Purpose of the work
This lab will help students acquire the knowledge necessary for solving systems of nonlinear equations in Matlab.
5.2 Tasks for laboratory work
To study the iterative method (Newton) for solving system of nonlinear equations.
Make a programs-functions to find the roots of the Newton method.
To carry out the individual task.
Save results of the work (the programs, listing of calculation) at your personal files.
Draw up report.
5.3 The basic theoretical knowledge
Consider the solution to a system of n non-linear equations in n unknowns given by
The system can be written in a single expression using vectors, i.e.,
F(x)=0,
where the vector x contains the independent variables, and the vector F contains the functions fi(x):
х
=
;
F(x) =
.
We will look at one method in particular, Newton's method. We are going to write our own routine function for solving non-linear equations using Newton's method. Matlab has its own routines for solving systems of nonlinear equations (e.g. fsolve, which is loosely based on Newton's method). Look up the routines fsolve in the Matlab help file
5.3.1 Newton method to solve systems of non-linear equations
A Newton method for solving the system of linear equations requires the evaluation of a matrix, known as the Jacobian of the system, which is defined as:
If x = x0 (a vector) represents the first guess for the solution, successive approximations to the solution are obtained from
A convergence criterion for the solution of a system of non-linear equation could be, for example, that the maximum of the absolute values of the functions fi(x(n)) is smaller than a certain tolerance ε, i.e.,
.
We can also use as convergence criteria the difference between consecutive values of the solution, i.e.,
.
Example 5.1. To illustrate the solving system of non-linear equations:
we
start by defining an initial guess for the solution as
.
Then:
F(x(0))=(-0.25, -1.25, -1.0).
;
;
.
5.3.2 Matlab function for Newton method for a system of nonlinear equations
The following MATLAB function, newtonm, calculates the solution to a system of n nonlinear equations, f(x) = 0, given the vector of functions f and the Jacobian J, as well as an initial guess for the solution x0.
function [x,iter] = newtonm(x0,f,J,maxiter,epsilon)
% Newton method applied to a
% system of linear equations f(x) = 0,
% given the jacobian function J, with
% x = [x1;x2;...;xn], f = [f1;f2;...;fn]
% x0 is an initial guess of the solution
% maxiter - max. number of iterations
% epsilon – tolerance
xx = x0; % load initial guess
for iter=1: maxiter
JJ = feval(J,xx);
if abs(det(JJ))<epsilon
error('newtonm - Jacobian is singular - try new x0');
abort;
end;
xn = xx - inv(JJ)*feval(f,xx);
if (abs(feval(f,xn))<epsilon )|(norm(xx-xn)<epsilon)
x=xn;
return;
end;
xx = xn;
end;
error('No convergence after maxiter iterations.');
abort;
% end function
Solution using function newtonm
Next, we use function newtonm to solve the problem postulated earlier. The functions f and the Jacobian J need to be defined as separate functions.
We can define the function f as the following user-defined MATLAB function fun:
function [y]=fun(x)
% fun(x) = 0, with x = [x(1);x(2),x(3)]
% represents a system of 3 non-linear equations
y=[x(1)^2+x(2)^2+x(3)^2-1;
2*x(1)^2+x(2)^2-4*x(3);
3*x(1)^2-4*x(2)+x(3)^2];
%end function
The corresponding Jacobian is calculated using the user-defined MATLAB function funJacob:
function [w]=funJacob(x)
% Evaluates the Jacobian of a 3x3
% system of non-linear equations
w(1,1)=2*x(1); w(1,2)=2*x(2); w(1,3)=2*x(3);
w(2,1)=4*x(1); w(2,2)=2*x(2); w(2,3)=-4;
w(3,1)=6*x(1); w(3,2)=-4; w(3,3)=2*x(3);
%end function
Next, we use function newtonm to solve the problem postulated earlier.
A call to the function using the values of x0, fun, funJacob, mxiter and tolerance is:
>> x=[0.5;0.5;0.5];
>> maxiter=30; eps=1e-6;
>> [x,iter] = newtonm(x0,'fun','funJacob',maxiter,eps)
x =
0.7852
0.4966
0.3699
iter =
4
The result shows the number of iterations required for convergence and the solution found as x1== 0.7852, x2 = 0.4966 and x3=0.3699.