-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixed_point.m
More file actions
38 lines (38 loc) · 1.14 KB
/
Fixed_point.m
File metadata and controls
38 lines (38 loc) · 1.14 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
clc;
clear;
% Writing the function
% g=@(x) x-x^3-4*x^2+10 ;
% g=@(x) (10/x - 4*x)^(1/2) ;
% g=@(x) (1/2)*(10-x^3)^(1/2) ;
g=@(x) (10/(4+x))^(1/2) ;
% g=@(x) x- (x^3+4*x^2-10)/(3*x^2+8*x) ;
fprintf("\n The given equation is: x^3+4x^2-10=0. \n");
% Putting initial approximations
x_0=input('\n Enter the first initial approximation: ');
% Putting stopping criteria
N=input('\n Enter the maximum number of iterations: ');
eps=input('\n Enter the measure of accuracy: ');
% Data of iterations storing matrix
X=zeros(1,3);
fprintf("\n The Fixed-point iterations are given as:\n\n");
D=[' k ' ' x_k ' ' g(x_k) '];
disp(D);
X_0=[1 x_0 g(x_0);];
disp(cell2mat(compose('%14.10f',X_0)));
% Starting iterations
p=1;
k=1;
while (k<=N && p==1)
x=g(x_0);
X=[k x g(x)];
disp(cell2mat(compose('%14.10f',X)));
if (abs(x-x_0)<= eps)
fprintf("\n An approximate solution (with tolerance %f) of the given equation is %14.10f .\n", eps, x);
p=0;
end
x_0=x;
k=k+1;
end
if k==N+1
fprintf("\n Maximum number of iteraton reached. The method fail after %d iterations.\n", N);
end