Purpose: It is a method for finding successively better approximations to the roots (or zeroes)
of a real-valued function. It is one example of a root-finding algorithm.
x: f(x)=0
Code:
disp('Newton METHOD');
syms x
f=myfun;
fd=diff(f,x);
t=(-1-P):0.1:(1+P);
hold on
plot(t,t.^3-25);
i=1;
while (i~=N)
a = feval(f,P);
b = subs(fd,P);
z = P-(a/b);
if a==0;
return
end
fprintf('%2i \t %f \t %f \n', i, P, a );
P=z;
i=i+1;
end
w=feval(f, P);
plot(P,a,'+');
end
Newton(Q.1)
f=@(x)x^2+6*(x^5)+9*(x^4)-2*(x^3)-6*(x^2)+1;
>> P=-1;
>> N=7;
>> tol=0.00001
tol =
1.0000e-05
>> x=Newton(f,P,N,tol)
Newton METHOD
iter x f(x)
1 -1.000000 1.000000
2 -0.500000 0.375000
3 -0.928571 0.839115
4 -0.580012 0.332893
5 -2.764441 -438.034753
6 -2.314396 -141.184797
7 iterations
Newton(Q.2)
>> f=@(x)2*x*cos(2*x)-(x+1)^2;
>> P=-2.5;
>> N=7
N =7
>> tol=0.00001
tol =1.0000e-05
>> x=Newton(f,P,N,tol)
Newton METHOD
iter x f(x)
1 -2.500000 -3.668311
2 -2.221180 -0.306242
3 -2.191935 -0.006293
4 -2.191308 -0.000003
5 -2.191308 -0.000000
6 -2.191308 -0.000000
x = -2.191308 produces f(x) = -0.000000
7 iterations
Approximation with tolerance = 0.000010
Newton(Q.3)
>> f=@(x)x-2^-x;
>> P=0.5;
>> N=7
N =7
>> tol=0.00001
tol = 1.0000e-05
>> x=Newton(f,P,N,tol)
Newton METHOD
iter x f(x)
1 0.500000 -0.207107
2 0.638986 -0.003178
3 0.641185 -0.000001
4 0.641186 -0.000000
5 0.641186 -0.000000
6 0.641186 -0.000000
7 iterations
Approximation with tolerance = 0.000010
Newton(Q.4)
>> f=@(x)3*x-exp(x)
f = @(x)3*x-exp(x)
>> P=1.5;
>> N=7
N =7
>> tol=0.00001
tol = 1.0000e-05
>> x=Newton(f,P,N,tol)
Newton METHOD
iter x f(x)
1 1.500000 0.018311
2 1.512358 -0.000344
3 1.512135 -0.000000
4 1.512135 -0.000000
5 1.512135 -0.000000
6 1.512135 -0.000000
7 iterations
Approximation with tolerance = 0.000010
Newton(Q.5)
>> f=@(x)x+1-2*sin(pi*x);
>> P=0.4;
>> N=7
N= 7
tol = 1.0000e-05
>> x=Newton(f,P,N,tol)
Newton METHOD
iter x f(x)
1 0.400000 -0.502113
2 -0.133249 1.679740
3 0.221078 -0.058981
4 0.205669 0.001470
5 0.206035 0.000001
6 0.206035 0.000000
7 iterations
Bisection Method
Code:
disp('BISECTION METHOD');
x=feval(fu,low);
y=feval(fu,high);
if x * y > 0
b = 'Error';
return
end
t=low:0.1:high;
hold on
%plot(t,t.^0.5-cos(t));
i=0;
av=(high + low)/2;
z=feval(fu,av);
if z==0;
fprintf('Solution of Function is %f /n/n', av);
return
end
if x*z >0
low=av;
x=z;
else
high=av;
end
i=i+1;
end
w=feval(fu, av);
plot(av,w,'+');
end
Bisection(Q.1)
>> f=@(x)x-2^-x;
>> low=0;
>> high=1;
>> tol=0.00001
tol = 1.0000e-05
>> x=bisection(f,low,high,tol)
BISECTION METHOD
16 iterations
Bisection(Q.2)
>> f=@(x)3*x-exp(x);
>> low=1;
>> high=2;
>> tol=0.00001;
>> x=bisection(f,low,high,tol)
BISECTION METHOD
16 iterations
Bisection(Q.3)
>> f=@(x)x+1-2*sin(pi*x);
>> low=0.5;
>> high=1;
>> tol=0.00001;
>> x=bisection(f,low,high,tol)
BISECTION METHOD
15 iterations
Bisection(Q.4)
f=@(x)2*x*cos(2*x)-(x+1)^2;
>> low=-1;
>> high=0;
>> tol=0.00001;
>> x=bisection(f,low,high,tol)
Bisection method
Bisection (Q.5)
f=@(x)x^2-3;
>> low=-2
low = -2
>> high=-1
high = -1
>> tol=0.00001;
>> x=bisection(f,low,high,tol)
BISECTION METHOD
16 iterations
Secant Method
Purpose: The secant method is a root-finding algorithm that uses a succession of roots of secant
lines to better approximate a root of a function f. The secant method can be thought of as
a finite difference approximation of Newton's method.
Code:
disp('SECANT METHOD');
i=1;
while (i~=N)
A=feval(f,P0);
B=feval(f,P1);
P=P1-(B*(P1-P0))/(B-A);
return
end
P0=P1;
P1=P;
i=i+1;
end
%w=feval(f, P);
plot(P,a,'+');
end
Secant (Q.1)
>> f=@(x)x^3-25;
>> P1=2.3;
>> P2=2.5;
>> N=6;
>> tol=0.00001;
>> x=secant(f,P1,P2,N,tol)
SECANT METHOD
iter P f(P)
1 3.042221 -9.375000
2 2.905657 3.156084
3 2.923292 -0.467997
4 2.924022 -0.018606
Secant(Q.2)
>> f=@(x)-x^3-cos(x);
>> P1=-1;
>> P2=0;
>> N=6;
>> tol=0.00001;
>> x=secant(f,P1,P2,N,tol)
SECANT METHOD
iter P f(P) a
1 -0.685073 -1.000000
2 -1.252076 -0.452850
3 -0.807206 1.649524
4 -0.847784 -0.165560
5 -0.866528 -0.052313
6 iterations
Purpose: It has the same purpose like Newton method but it is modified version of newton
Code:
disp('iter P0 P1 P2 ');
i=1;
while (i~=N)
P1 = feval(gx,P);
P2 = feval(gx,P1);
t1 = (P1-P);
t2 = (P2-2*P1+P);
P0 = P-(t1^2/t2);
return
end
P=P0;
i=i+1;
end
w=feval(gx, P);
%plot(P,a,'+');
end
NewtonM(Q.1)
>> f=@(x)(2-exp(x)+x^2)/3;
>> P=0.5;
>> N=5;
>> tol=0.000000000000001;
>> x=Newtonmodified(f,P,N,tol)
iter P0 P1 P2
Newton Method(Q.2)
>> f=@(x)(exp(x)/3)^0.5
f=
@(x)(exp(x)/3)^0.5
>> P=0.75;
>> N=5;
>> tol=0.000000000000001;
>> x=Newtonmodified(f,P,N,tol)
iter P0 P1 P2
5 iterations
NewtonM(Q.3)
>> f=@(x)cos(x);
>> P=0.5;
>> N=5;
>> tol=0.000000000000001;
>> x=Newtonmodified(f,P,N,tol)
5 iterations
NewtonM(Q.4)
>> f=@(x)3^-x;
>> P=0.5;
>> N=5;
>> tol=0.000000000000001;
>> x=Newtonmodified(f,P,N,tol)
iter P0 P1 P2
NewtonM(Q.5)
>> f=@(x)cos(x-1);
>> P=2;
>> N=5;
>> tol=0.000000000000001;
>> x=Newtonmodified(f,P,N,tol)
iter P0 P1 P2
5 iterations
Which gives rise to the sequence which is hoped to converge to a point If is continuous, then
Code:
t=(-1-P):0.1:(1+P);
hold on
plot(t,((10-t.^3).^0.5)/2);
i=1;
while (i~=N)
fp=feval(gx,P);
return
end
P=fp;
i=i+1;
end
w=feval(gx, P);
plot(P,w,'+');
end
Fixed Point(Q.1)
>> g=@(x)(3+x-2*x^2)^1/4;
>> P=-0.5;
>> N=7;
>> tol=0.00001;
>> x=fixedpoint(f,P,N,tol)
iter P f(P)
1 -0.500000 0.070737
2 0.070737 0.598425
3 0.598425 0.920446
4 0.920446 0.996837
5 0.996837 0.999995
g= @(x)x-(x^3)-4*(x^2)-10
>> g=@(x)x-(x^3)-4*(x^2)+10
g = @(x)x-(x^3)-4*(x^2)+10
>> P=1.5
P =1.5000
>> N=10
N = 10
>> tol=0.00001;
>> x=fixedpoint(f,P,N,tol)
iter P f(P)
1 1.500000 0.877583
2 0.877583 0.992516
3 0.992516 0.999972
4 0.999972 1.000000
Graphs:Bisection(Q.4)
Bisection(Q.3)
Bisection(Q.2)
Bisection(Q.1)
Bisection(Q.5)
Newton(Q.1)
Newton(Q.4)
Newton(Q.3)
Newton(Q.4)
Newton(Q.5)
Secant(Q.1)
Secant(Q.2)
Secant(Q.3)
CUBIC SPLINES
Cubic spline interpolation is a fast, efficient and stable method of function interpolation.
Parallel with the rational interpolation, the spline interpolation is an alternative for the
polynomial interpolation.
The main advantages of spline interpolation are its stability and calculation simplicity. Sets of
linear equations which should be solved to construct splines are very well-conditioned,
therefore, the polynomial coefficients are calculated precisely. As a result, the calculation
scheme stays stable even for big N. The construction of spline coefficients table is performed in
O(N) time, and interpolation - in O(log(N)) time.
Given a function f defined on [a, b] and a set of nodes a = x0 < x1 < <xn = b,
a cubic spline interpolant S for f is a function that satisfies the following
Conditions:
(a) S(x) is a cubic polynomial, denoted Sj(x), on the subinterval [xj , xj+1]
for each j = 0, 1, . . . , n 1.
(b) Sj+1(xj+1) = Sj(xj+1) for each j = 0, 1, . . . , n 2
(c) Sj(xj) = f (xj) and Sj(xj+1) = f (xj+1) for each j = 0, 1, . . . , n 1.
(d) (xj+1) = (xj+1) for each j = 0, 1, . . . , n 2;
(e) (xj+1) = (xj+1) for each j = 0, 1, . . . , n 2.
(f) One of the following sets of boundary conditions is satisfied:
(i) (x0) = (xn) =0 (natural (or free) boundary).
(ii) (x0) = (x0) and (xn) = (xn) (clamped boundary).
A cubic spline is a spline constructed of piecewise third-order polynomials which pass through a
set of control points. The second derivative of each polynomial is commonly set to zero at the
endpoints, since this provides a boundary condition that completes the system of equations. .
When the free boundary conditions occur, the spline is called a natural spline, and its graph
approximates the shape that a long flexible rod would assume if forced to go through the data
points
Matlab Code:
Q1:
Total number of points you want to enter: 21
Enter the value of x: 0.8
Enter the value of y: 1.2
Enter the value of x: 1.3
Enter the value of y: 1.5
Enter the value of x: 1.4
Enter the value of y: 0.482
Enter the value of x: 1.5
Enter the value of y: 0.598
Enter the value of x: 2.1
Enter the value of y: 2.1
Enter the value of x: 6
Enter the value of y: 2.25
Enter the value of x: 7
Enter the value of y: 2.3
Enter the value of x: 8
Enter the value of y: 2.25
Enter the value of x: 9
Enter the value of y: 1.95
Enter the value of x: 11.6
Enter the value of y: 0.7
Enter the value of x: 12.6
Enter the value of y: 0.5
Enter the value of x: 13
Enter the value of y: 0.4
Enter the value of x: 13.3
Enter the value of y: 0.25
Enter the value of x: 13
Enter the value of y: 0.4
Enter the value of x: 5
Enter the value of y: 2.1
Enter the value of x: 3.9
Enter the value of y: 2.4
Enter the value of x: 11
Enter the value of y: 0.9
Enter the value of x: 3
Enter the value of y: 2.7
Enter the value of x: 8
Enter the value of y: 2.25
Enter the value of x: 9
Enter the value of y: 1.95
Enter the value of x: 2.6
Enter the value of y: 2.6
Plot of splines:
Code:
n=input('Total points: ');
k=n-1;
for j= 1:n
y(j)=input('Enter the x value: ');
a(j)=input('Enter the y value: ');
end
a1=input('1st point derivative value: ');
a2=input('last point derivative value: ');
% Vector h with subintervals:
h = zeros(k,1);
for j = 1:k
h(j) = x(j+1) - x(j);
end
% Coefficient matrix A:
X = zeros(n);
% Natural Spline boundary conditions:
X(1,1)= 2*h(1);
X(1,2)= h(1);
X(n,k)= h(k);
X(n,n) = 2*h(k);
for i = 2:k
X(i,i-1) = h(i-1);
X(i,i) = 2*(h(i-1)+h(i));
X(i,i+1) = h(i);
end
% Vector b:
b1 = zeros(n,1);
b1(1,1)=(3.0/h(1))*(a(2)-a(1)) - (3.0 * d1);
b1(n,1)=(3.0 * d2) - (3.0/h(k))*(a(n)-a(k));
for i = 2:k
b(i) = ((3/h(i))*(a(i+1)-a(i))) - ((3/h(i-1))*(a(i)-a(i-1)));
end
% Coefficient vector cj:
cj = A\b;
% Coefficient vector bj:
bj = zeros(k,1);
for i = 1:k
bj(i) = ((1/h(i))*(a(i+1)-a(i)) - ((1/3)*h(i))*(2*(cj(i))+cj(i+1)));
end
% Coefficient vector dj:
dj = zeros(k,1);
for i = 1:k
dj(i) = (1/(3*h(i))) * (cj(i+1)-cj(i));
end
A = zeros(k,5);
for i = 1:k
A(i,1) = (i);
A(i,2) = a(i);
A(i,3) = bj(i);
A(i,4) = cj(i);
A(i,5) = dj(i);
end
hold on
for i = 1:n-1
f = @(x) a(i) + bj(i).*(x-x(i)) + cj(i).*(x-x(i)).^2 + dj(i).*(x-x(i)).^3;
xf = linspace(x(i),x(i+1));
plot(xf,f(xf),'b');
ylabel('f(x)');
xlabel('x');
end
hold off
A
Total points: 15
Plot: