
- •Затверджено
- •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
5.3.3 The matlab routine function fsolve
The MATLAB routine function fsolve is used to solve sets of nonlinear algebraic equations using a quasi-Newton method.
Syntax
x = fsolve(fun,x0,options) solves the equations with the optimization options specified in the structure options. Use function optimset to set these options.
Input Arguments
Function Arguments contains general descriptions of arguments passed into fsolve:
fun fun is a function that accepts a vector x and returns a vector F, the nonlinear equations evaluated at x.
If the Jacobian can also be computed and the Jacobian option is 'on', set by options = optimset('Jacobian','on') the function fun must return, in a second output argument, the Jacobian value J, a matrix, at x;
x0 Initial point for x;
options Options structure created with function optimset.
Output Arguments
[x,fval] = fsolve(fun,x0) returns the value of the objective function fun at the solution x.
[x,fval,exitflag] = fsolve(...) returns a value exitflag that describes the exit condition.
[x,fval,exitflag,output] = fsolve(...) returns a structure output that contains information about the optimization.
[x,fval,exitflag,output,jacobian] = fsolve(...) returns the Jacobian of fun at the solution x.
Example 5.2. This example solves the system of two equations and two unknowns:
Rewrite the equations in the form F(x) = 0:
Start your search for a solution at x0 = [-5 -5]. First, write a file that computes F, the values of the equations at x.
function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
Save this function file as myfun.m somewhere on your MATLAB path. Next, set up the initial point and options and call fsolve:
>>x0 = [-5; -5]; % Make a starting guess at the solution
>>options=optimset('Display','iter'); % Option to display output
>>[x,fval] = fsolve(@myfun,x0,options) % Call solver
After several iterations, fsolve finds an answer:
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 3 23535.6 2.29e+004 1
1 6 6001.72 1 5.75e+003 1
2 9 1573.51 1 1.47e+003 1
3 12 427.226 1 388 1
4 15 119.763 1 107 1
5 18 33.5206 1 30.8 1
6 21 8.35208 1 9.05 1
7 24 1.21394 1 2.26 1
8 27 0.016329 0.759511 0.206 2.5
9 30 3.51575e-006 0.111927 0.00294 2.5
10 33 1.64763e-013 0.00169132 6.36e-007 2.5
Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient.
x =
0.5671
0.5671
fval =
1.0e-006 *
-0.4059
-0.4059
Example
5.3. This
example solves the system of the equations:
Write a file that computes F and Jacobian, the values of the equations at x.
function [y,j]=funscj(x)
y=[x(1)+x(2)-sin(pi*x(1));x(1)-x(2)-cos(pi*x(2))];
if nargout>1
j=[1-pi*cos(pi*x(1)) 1;1 -1+pi*sin(pi*x(2))];
end;
A call to the function fsolve:
>>x0 = [1;0.5]; % Make a starting guess at the solution
>>options=optimset('Jacobian','on'); %
>>[x,ff,e_flag,inform]=fsolve(@funscj, x0,options)
x = 0.5000 0.5000
ff = 1.0e-008 *
0.1653
0.0000
e_flag = 1
inform = iterations: 6
funcCount: 7
algorithm: 'trust-region dogleg'
firstorderopt: 1.6525e-009
message: 'Optimization terminated: first-order optimality is less than options.TolFun.'