Page 1 of 3 05 November 2012 Due date: 09 November 2012

05 November 2012
Due date: 09 November 2012
UNIVERSITI TEKNOLOGI MALAYSIA
SCHOOL OF PROFESSIONAL AND CONTINUING EDUCATION
SME 3023 Assignment 1b – Solution of Nonlinear Equations
1. Write a Matlab program to implement the incremental search method (which algorithm is
shown below) for finding the root of the equation, f  x   0 , using x1 as the starting point and
x1 as the initial step length. Use this program to find the root of the equation below, using x1
= 0.0 and x  =0.1 with approximated error less than 1 × 10-5.
1
f  x 
1.5 x
1  x 
2 2
 1  0.65 x
 0.65tan 1   
0
2
 x  1 x
Solution:
Program code
Copy and paste onto m-file editor to run the code
% Example from lecture slides
% Root finding -- incremental search method
%
clear all; close all; clc;
% Define the function
fx = inline('1.5*x./(1+x.^2).^2 - 0.65*atan(1./x) + 0.65*x./(1+x.^2)','x');
% As a guide, plot the function over a range of x to see where the function
% intercepts the x-axis.
% Try with x from 0 to 2 with 0.1 step to see if function intersects x-axis
Page 1 of 3
x = 0:0.1:2;
y = fx(x);
plot(x,y); grid;
% Start incremental search
% Define epsilon s, es
es = 1e-5;
% Step 1 of algorithm
% Set an initial guess
xi = 0.0;
deltax = 0.1;
% Print header for table and first row on screen
fprintf ('\n
x
f(x)
deltax
ea\n')
fprintf ('%f %f %f\n', xi, fx(xi), deltax)
% Define ea. ea is needed to execute while-loop. If ea > es, while-loop
% will be executed.
ea = (xi+deltax)/xi;
% Before while-loop starts, the condition must be satisfied. In this case,
% the condition is ea > es. es is 1e-5 and ea needs to be defined (line 31)
% or else while loop will not be executed.
while ea > es
% Step 2 of algorithm
% Call function fx to calculate its value at xi
fxi = fx(xi);
% Before xi is incremented, "oldxi" value needs to be preserved. The
% statement below means "assign/replace oldxi value with xi"
oldxi = xi;
% Step 3 of algorithm
% Increment of xi
% The statement is NOT "xi equal to xi + deltax".
% The statement below means "assign xi with xi + deltax" or "replace
% value of xi with xi + deltax". The pseudo code is "newxi = oldxi +
% deltax"
xi = xi + deltax;
% Calculate ea. This is necessary for while loop to either stop or
% continue. The ea value will be compared with es, if ea < as, stop
% while-loop, if ea > as, continue while-loop
ea = (xi-oldxi)/xi;
% Step
% Call
% Note
% line
fxdelx
4 of algorithm
function fx to calculate its value at xi+deltax
that xi in the statement below is "newxi" or xi+deltax. Refer to
51
= fx(xi);
% Step 5 of algorithm
% Compare sign of f(xi) and f(xi+deltax). In this program, the
% statement "fxi*fxdelx < 0" compares sign of fxi and fxdelx.
% If sign changes, reset newxi (line 51) to oldxi, and reduce deltax
if fxi*fxdelx < 0
% reset newxi to oldxi. Use this statement, "xi = oldxi", or
xi = xi - deltax;
% reduce deltax by 10. The statement means "change deltax with
% deltax/10"
deltax = deltax/10;
end
fprintf ('%f %f %f %f\n', xi, fx(xi), deltax, ea)
% Repeat step 2 (line 39) with "newxi" if ea > es. Stop if ea < es.
% This can be done using "while loop". Note that xi is now "newxi".
end
% Print root after exiting the while-loop
fprintf('\nRoot is: %f\n',xi)
fprintf('Estimated error is: %f\n',ea)
Page 2 of 3
Program output
x
0.000000
0.100000
0.200000
0.300000
0.400000
0.400000
0.410000
0.420000
0.430000
0.440000
0.450000
0.460000
0.470000
0.480000
0.480000
0.480000
0.480100
0.480200
0.480300
0.480400
0.480500
0.480600
0.480700
0.480800
0.480800
0.480810
0.480820
0.480830
0.480840
0.480850
0.480860
0.480860
0.480861
f(x)
-1.021018
-0.744832
-0.490344
-0.273916
-0.103653
-0.103653
-0.089228
-0.075266
-0.061763
-0.048715
-0.036116
-0.023961
-0.012243
-0.000956
-0.000956
-0.000956
-0.000845
-0.000734
-0.000624
-0.000513
-0.000403
-0.000292
-0.000182
-0.000071
-0.000071
-0.000060
-0.000049
-0.000038
-0.000027
-0.000016
-0.000005
-0.000005
-0.000004
deltax
0.100000
0.100000
0.100000
0.100000
0.100000
0.010000
0.010000
0.010000
0.010000
0.010000
0.010000
0.010000
0.010000
0.010000
0.001000
0.000100
0.000100
0.000100
0.000100
0.000100
0.000100
0.000100
0.000100
0.000100
0.000010
0.000010
0.000010
0.000010
0.000010
0.000010
0.000010
0.000001
0.000001
ea
1.000000
0.500000
0.333333
0.250000
0.200000
0.024390
0.023810
0.023256
0.022727
0.022222
0.021739
0.021277
0.020833
0.020408
0.002079
0.000208
0.000208
0.000208
0.000208
0.000208
0.000208
0.000208
0.000208
0.000208
0.000021
0.000021
0.000021
0.000021
0.000021
0.000021
0.000021
0.000002
Root is: 0.480861
Estimated error is: 0.000002
Page 3 of 3