-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewtonModified.m
More file actions
73 lines (65 loc) · 1.66 KB
/
NewtonModified.m
File metadata and controls
73 lines (65 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%A simple program that demonstrates the
% application of the Modified Newton method.
%*******************************************
%Created by: Mohammad Tawfik
%Email: mohammad.tawfik@gmail.com
%
%Code associated with lecture notes titled:
%Fundamentals of Numerical Analysis
%Published on ResearchGate.net at:
%https://www.researchgate.net/publication/321850359_Fundamentals_of_Numerical_Analysis_Book_Draft
%With DOI: 10.13140/RG.2.2.25680.81925
%Lecture videos are published on the
%#AcademyOfKnowledge website
%http://AcademyOfKnowledge.org
%
%With the direct link:
% https://sites.google.com/academyofknowledge.org/theakweb/engineering-and-science/applied-numerical-analysis
%
%*******************************************
%Clearing the memory
clear all
close all
clc
x1=-1; %Initial guess
%Problem parameters
Epsilon = 1e-3; %Tolerance
MaxIter =20; %Maximum number of iterations
%Initializing the counter and error
Counter=0;
Err=1000;
%The search loop
while true
Counter=Counter+1; %Incrementing the counter
%Evaluating the function
F1=exp(-x1)-0.5
%Evaluating the DeltaX
if x1==0
DeltaX=0.01;
else
DeltaX=0.01*x1;
end
%Evaluating the function at (x+DeltaX)
F1d=exp(-x1-DeltaX)-0.5;
%Evaluating the approximate slope
F1p=(F1d-F1)/DeltaX;
x2=x1-(F1/F1p) %The approximate solution
%Evaluating the relative error
if x2!=0
Err=abs((x2-x1)/x2);
else
Err=abs(x2-x1);
end
%Checking for termination
if or(F1==0,or(Err<Epsilon,Counter>MaxIter))
F1=exp(-x2)-0.5;
break
else
x1=x2;
end
end
%Displaying the solution
Counter
Err
x2
F1