Вы находитесь на странице: 1из 44

Newton Method

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:

function Newton = Newton(myfun,P, N, tol)

disp('Newton METHOD');

syms x

f=myfun;

fd=diff(f,x);

disp('iter x 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);

axis([-2 10 -1.5 10])

if a==0;

fprintf('Solution of Function is %f /n/n', P);

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,'+');

fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', P, w, i);

fprintf(' Approximation with tolerance = %f \n', tol);

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

x = -1.966184 produces f(x) = -44.930071

7 iterations

Approximation with tolerance = 0.000010

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

x = 0.641186 produces f(x) = 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

x = 1.512135 produces f(x) = 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=0.00001

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

x = 0.206035 produces f(x) = 0.000000

7 iterations

Approximation with tolerance = 0.000010

Bisection Method
Code:

function bisect = bisection(fu, low, high, tol)

disp('BISECTION METHOD');

x=feval(fu,low);

y=feval(fu,high);

if x * y > 0

disp('Function has no solution in the given interval');

b = 'Error';

return

end

disp('iter low high average');

t=low:0.1:high;

hold on

%plot(t,t.^0.5-cos(t));

i=0;

while (abs(high - low) >= tol)

av=(high + low)/2;

z=feval(fu,av);

if z==0;
fprintf('Solution of Function is %f /n/n', av);

return

end

fprintf('%2i \t %f \t %f \t %f \n', i, low, high, av);

if x*z >0

low=av;

x=z;

else

high=av;

end

i=i+1;

end

w=feval(fu, av);

plot(av,w,'+');

fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', av, z, i-1);

fprintf(' Approximation with tolerance = %f \n', tol);

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

iter low high average

0 0.000000 1.000000 0.500000

1 0.500000 1.000000 0.750000

2 0.500000 0.750000 0.625000

3 0.625000 0.750000 0.687500

4 0.625000 0.687500 0.656250

5 0.625000 0.656250 0.640625

6 0.640625 0.656250 0.648438

7 0.640625 0.648438 0.644531

8 0.640625 0.644531 0.642578

9 0.640625 0.642578 0.641602

10 0.640625 0.641602 0.641113

11 0.641113 0.641602 0.641357

12 0.641113 0.641357 0.641235

13 0.641113 0.641235 0.641174

14 0.641174 0.641235 0.641205


15 0.641174 0.641205 0.641190

16 0.641174 0.641190 0.641182

x = 0.641182 produces f(x) = -0.000005

16 iterations

Approximation with tolerance = 0.000010

Bisection(Q.2)

>> f=@(x)3*x-exp(x);

>> low=1;

>> high=2;

>> tol=0.00001;

>> x=bisection(f,low,high,tol)

BISECTION METHOD

iter low high average

0 1.000000 2.000000 1.500000

1 1.500000 2.000000 1.750000

2 1.500000 1.750000 1.625000

3 1.500000 1.625000 1.562500


4 1.500000 1.562500 1.531250

5 1.500000 1.531250 1.515625

6 1.500000 1.515625 1.507813

7 1.507813 1.515625 1.511719

8 1.511719 1.515625 1.513672

9 1.511719 1.513672 1.512695

10 1.511719 1.512695 1.512207

11 1.511719 1.512207 1.511963

12 1.511963 1.512207 1.512085

13 1.512085 1.512207 1.512146

14 1.512085 1.512146 1.512115

15 1.512115 1.512146 1.512131

16 1.512131 1.512146 1.512138

x = 1.512138 produces f(x) = -0.000006

16 iterations

Approximation with tolerance = 0.000010

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

iter low high average

0 0.500000 1.000000 0.750000

1 0.500000 0.750000 0.625000

2 0.625000 0.750000 0.687500

3 0.625000 0.687500 0.656250

4 0.656250 0.687500 0.671875

5 0.671875 0.687500 0.679688

6 0.679688 0.687500 0.683594

7 0.679688 0.683594 0.681641

8 0.681641 0.683594 0.682617

9 0.681641 0.682617 0.682129

10 0.681641 0.682129 0.681885

11 0.681885 0.682129 0.682007

12 0.681885 0.682007 0.681946

13 0.681946 0.682007 0.681976

14 0.681946 0.681976 0.681961


15 0.681961 0.681976 0.681969

x = 0.681969 produces f(x) = -0.000027

15 iterations

Approximation with tolerance = 0.000010

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

iter low high average

0 -1.000000 0.000000 -0.500000

1 -1.000000 -0.500000 -0.750000

2 -1.000000 -0.750000 -0.875000

3 -0.875000 -0.750000 -0.812500

4 -0.812500 -0.750000 -0.781250

5 -0.812500 -0.781250 -0.796875


6 -0.812500 -0.796875 -0.804688

7 -0.804688 -0.796875 -0.800781

8 -0.800781 -0.796875 -0.798828

9 -0.798828 -0.796875 -0.797852

10 -0.798828 -0.797852 -0.798340

11 -0.798340 -0.797852 -0.798096

12 -0.798340 -0.798096 -0.798218

13 -0.798218 -0.798096 -0.798157

14 -0.798218 -0.798157 -0.798187

15 -0.798187 -0.798157 -0.798172

16 -0.798172 -0.798157 -0.798164

x = -0.798164 produces f(x) = 0.000016

16 iterations Approximation with tolerance = 0.000010

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

iter low high average

0 -2.000000 -1.000000 -1.500000

1 -2.000000 -1.500000 -1.750000

2 -1.750000 -1.500000 -1.625000

3 -1.750000 -1.625000 -1.687500

4 -1.750000 -1.687500 -1.718750

5 -1.750000 -1.718750 -1.734375

6 -1.734375 -1.718750 -1.726563

7 -1.734375 -1.726563 -1.730469

8 -1.734375 -1.730469 -1.732422

9 -1.732422 -1.730469 -1.731445

10 -1.732422 -1.731445 -1.731934

11 -1.732422 -1.731934 -1.732178

12 -1.732178 -1.731934 -1.732056

13 -1.732056 -1.731934 -1.731995


14 -1.732056 -1.731995 -1.732025

15 -1.732056 -1.732025 -1.732040

16 -1.732056 -1.732040 -1.732048

x = -1.732048 produces f(x) = -0.000010

16 iterations

Approximation with tolerance = 0.000010

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:

function secant = secant(f, P0, P1, N, tol)

disp('SECANT METHOD');

disp('iter P f(P) ');

i=1;

while (i~=N)

A=feval(f,P0);

B=feval(f,P1);
P=P1-(B*(P1-P0))/(B-A);

if abs(P - P1) <= tol

fprintf('Solution of Function is %f /n/n', P);

return

end

fprintf('%2i \t %f \t %f \n', i, P, B);

P0=P1;

P1=P;

i=i+1;

end

%w=feval(f, P);

plot(P,a,'+');

fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', P, B, i);

fprintf(' Approximation with tolerance = %f \n', tol);

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

Solution of Function is 2.924018

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

x = -0.866528 produces f(x) = -0.052313

6 iterations

Approximation with tolerance = 0.000010

Newton Modified Method

Purpose: It has the same purpose like Newton method but it is modified version of newton

method which give more precise result in less number of iteration.

Code:

function aitken = Newtonmodified(gx, P, N, tol)

disp('NEWTON MODIFIED METHOD');

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);

if P1==0 || abs(P - P0) <= tol;

fprintf('Solution of Function is %f \n\n ', P);

return

end

fprintf('%2i \t %f \t %f \t %f \n', i, P, P1, P2);

P=P0;

i=i+1;

end

w=feval(gx, P);

%plot(P,a,'+');

fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', P, w, i);

fprintf(' Approximation with tolerance = %f \n', tol);

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)

NEWTON MODIFIED METHOD

iter P0 P1 P2

1 0.500000 0.200426 0.272749

2 0.258684 0.257231 0.257608

3 0.257530 0.257530 0.257530

Solution of Function is 0.257530

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)

NEWTON MODIFIED METHOD

iter P0 P1 P2

1 0.750000 0.840040 0.878722

2 0.907859 0.909030 0.909563

3 0.910007 0.910007 0.910007

4 0.910008 0.910008 0.910008

x = 0.910008 produces f(x) = 0.910008

5 iterations

Approximation with tolerance = 0.000000

NewtonM(Q.3)

>> f=@(x)cos(x);

>> P=0.5;

>> N=5;

>> tol=0.000000000000001;

>> x=Newtonmodified(f,P,N,tol)

NEWTON MODIFIED METHOD


iter P0 P1 P2

1 0.500000 0.877583 0.639012

2 0.731385 0.744250 0.735596

3 0.739076 0.739091 0.739081

4 0.739085 0.739085 0.739085

x = 0.739085 produces f(x) = 0.739085

5 iterations

Approximation with tolerance = 0.000000

NewtonM(Q.4)

>> f=@(x)3^-x;

>> P=0.5;

>> N=5;

>> tol=0.000000000000001;

>> x=Newtonmodified(f,P,N,tol)

NEWTON MODIFIED METHOD

iter P0 P1 P2

1 0.500000 0.577350 0.530315

2 0.548101 0.547633 0.547915


3 0.547809 0.547809 0.547809

Solution of Function is 0.547809

NewtonM(Q.5)

>> f=@(x)cos(x-1);

>> P=2;

>> N=5;

>> tol=0.000000000000001;

>> x=Newtonmodified(f,P,N,tol)

NEWTON MODIFIED METHOD

iter P0 P1 P2

1 2.000000 0.540302 0.896187

2 0.826427 0.984974 0.999887

3 1.001435 0.999999 1.000000

4 1.000000 1.000000 1.000000

x = 1.000000 produces f(x) = 1.000000

5 iterations

Approximation with tolerance = 0.000000


Fixed Point Method

Purpose: Fixed-point iteration is a method of computing fixed points of iterated functions.


More specifically, given a function defined on the real numbers with real values and given a
point in the domain of, the fixed point iteration is

Which gives rise to the sequence which is hoped to converge to a point If is continuous, then

one can prove that the obtained is a fixed point of f.


Formula:

Code:

function fixedPoint = fixedpoint(gx, P, N, tol)

disp('fixed POint METHOD');

disp('iter P f(P) ');

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);

if abs(P - fp) <= tol

fprintf('fixed point of Function is %f \n\n ', fp);

return

end

fprintf('%2i \t %f \t %f \n', i, P, fp);

P=fp;
i=i+1;

end

w=feval(gx, P);

plot(P,w,'+');

fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', P, w, i);

fprintf(' Approximation with tolerance = %f \n', tol);

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)

fixed POint METHOD

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

fixed point of Function is 1.000000

Fixed Point (Q.2)


>> 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

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)

fixed POint METHOD

iter P f(P)

1 1.500000 0.877583

2 0.877583 0.992516

3 0.992516 0.999972

4 0.999972 1.000000

fixed point of Function is 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.

Conditions For Cubic Spline Interpolant:

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).

Natural Cubic Splines:

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

The vector equation Ax = b, where A is the (n+1)(n+ 1) matrix is given by:

Matlab Code:

inp=input('Total number of points you want to enter: ');


k=inp-1;
for j= 1:inp
y(j)=input('Enter the value of x : ');
a(j)=input('Enter the value of y: ');
end
% Vector h with subintervals:
h = zeros(k,1);
for j = 1:k
h(j) = y(j+1) - a(j);
end
% Coefficient matrix X:
X = zeros(inp);
% Natural Spline boundary conditions:
X(1,1)= 1;
X(inp,inp) = 1;
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:
b = zeros(inp,1);
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
M = zeros(k,5);
for i = 1:k
M(i,1) = (i);
M(i,2) = a(i);
M(i,3) = bj(i);
M(i,4) = cj(i);
M(i,5) = dj(i);
end
hold on
for i = 1:n-1 f = @(y) a(i) + bj(i).*(y-y(i)) + cj(i).*(y-y(i)).^2 + dj(i).*(y-y(i)).^3;
yf = linspace(y(i),y(i+1));
plot(yf,f(yf),'b');
ylabel('f(x)');
xlabel('x');
end
hold off
S

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:

Clamped Cubic Splines:

In the mathematical field of numerical analysis, spline interpolation is a form of interpolation


where the interpolant is a special type of piecewise polynomial called a spline. However, for
this type of boundary condition to hold, it is necessary to have either the values of the
derivative at the endpoints or an accurate approximation to those values.

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

Enter the x value = 1


Enter the y value = 3
Enter the x value = 2
Enter the y value = 3.7
Enter the x value = 5
Enter the y value = 3.9
Enter the x value = 6
Enter the y value = 4.2
Enter the x value = 7
Enter the y value = 5.7
Enter the x value = 8
Enter the y value = 6.6
Enter the x value = 10
Enter the y value = 7.1
Enter the x value = 13
Enter the y value = 6.7
Enter the x value = 17
Enter the y value = 4.5
Enter the x value = 20
Enter the y value = 7
Enter the x value = 23
Enter the y value = 6.1
Enter the x value = 24
Enter the y value = 5.6
Enter the x value = 25
Enter the y value = 5.8
Enter the x value = 27
Enter the y value = 5.2
Enter the x value = 27.7
Enter the y value = 4.1
Enter the x value = 28
Enter the y value = 4.3

Plot: