Sei sulla pagina 1di 10

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

Numerical Differentiation and Integration


Franky Leonardo Prieto

AbstractThe use of differentiation and integration methods


is presented, using matlab programming language. For differentiation methods central differences seems to be the best option
in almost all cases. For integration methods, gauss quadrature
seems to be the best option.

left
right
central
left
right
central

1st
1st
2nd
2nd
2nd
4th

100

error

Index TermsNumerical Differentiation, Numerical Integration, Finite Differences, Newton-Cotes Quadrature, Gauss
Quadrature, Approximation, Numerical Methods.

error v n for function (


x) x .* log (x)
101

I. I NTRODUCTION
HIS work is intended to show the use of numerical
methods to approximate or interpolate between data. In
his development are implemented several numerical methods.
All this using matlab language, through qtoctave, an alternative
free software option.

10-1

10-2
1

1.5

2.5

3
n

3.5

4.5

Fig. 2. error v number of terms used for approximation

A. Mathematical background
1) Derivative: Recomend lecture 27 from [2]
2) Integration: Recomend chapter 7 from [1] first 5 sections

error v n for function (


x) x .* cos (x) - x . 2 .* sin (x)
102

left
right
central
left
right
central

1st
1st
2nd
2nd
2nd
4th

100

10-2
error

B. Development
1) For the next functions plot the error as a function of the
number of terms used to make the approximation n. Which
scheme of finite difference is better?: To make this, a function
to evaluate the derivative for different methods was created,
its code is in appendix A.
The function dev1 in appendix B takes ten points for each
function, evaluates the derivative for each one and compares
the numerical results with analytical values.
Figures 1 to 8 show the solicited plot for each function.

10-4

10-6

10-8
1

1.5

2.5

3
n

3.5

Fig. 3. error v number of terms used for approximation

error v n for function (


x) exp (2 * x)
101

left
right
central
left
right
central

100

1st
1st
2nd
2nd
2nd
4th

error v n for function (


x) 2 .* (log (x)) . 2 + 3 .* sin (x)
101

left
right
central
left
right
central

10

10-1

error

4.5

1st
1st
2nd
2nd
2nd
4th

10-1
10-2

error

10-2
10-3

10-3

10-4
10-4

10-5
1

1.5

2.5

3
n

3.5

4.5

10-5

10-6

Fig. 1. error v number of terms used for approximation

The better scheme to make approximations of derivative


values is the fourth order central differences, it gets less error
values in almost all cases. Only one function does not have
the best value using this scheme.

1.5

2.5

3
n

3.5

Fig. 4. error v number of terms used for approximation

4.5

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

error v n for function (


x) exp (2 .* x) - cos (2 .* x)
101

error v n for function (


x) (cos (3 .* x)) . 2 - exp (2 .* x)

left
right
central
left
right
central

100

101

1st
1st
2nd
2nd
2nd
4th

left
right
central
left
right
central

100

1st
1st
2nd
2nd
2nd
4th

10-1
10-1

error

error

10-2
10-2

10-3
-3

10

10-4

10-4

10-5

10-5

10-6
1

1.5

2.5

3
n

3.5

4.5

Fig. 5. error v number of terms used for approximation

1.5

2.5

3
n

3.5

4.5

Fig. 8. error v number of terms used for approximation

error v n for function (


x) log (x + 2) - (x + 1) . 2
102

left
right
central
left
right
central

10

results using this option are essentially the same than using
the analytical expression in figure 9 both results are plotted.

1st
1st
2nd
2nd
2nd
4th

102

10-4

100

10-6

10-2

10-8

10-4

error

10-2

original
numeric J
original
numeric J

1
1
2
2

10-6

10-10

10-8
10-12
1

1.5

2.5

3
n

3.5

4.5

10-10

Fig. 6. error v number of terms used for approximation

10-12
10-14

error v n for function (


x) x .* sin (x) + x . 2 .* cos (x)
101

left
right
central
left
right
central

100

1st
1st
2nd
2nd
2nd
4th

error

10-2

10-3

10-4

10-5

10-6
1.5

2.5

3
n

3.5

4.5

10

12

14

Fig. 9. Original result v numerical jacobian use

10-1

Fig. 7. error v number of terms used for approximation

2) Using the code of Newton methods (for one and several


variable) already programmed by you in the first Homework,
add the function to evaluate the derivative using the best
scheme of finite differences. : To make this, function to
evaluate jacobian was created, it is in appendix D using fourth
order central differences. This function is used to develop the
7th exercise from past homework, plotting the error for each
equation system. Code can be found in the appendix E. The

3) For the next functions plot the error as a function of the


number of terms used to make the approximation n. Which
formula quadrature is better? : To make this, a function to
evaluate integrals using different quadratures was created, and
it is in the appendix F.
For each function, integral was evaluated for three intervals
2 to 8 (..), 4 to 8 () and 6 to 8 (continue line) The code used
is in the appendix G. And the graphs obtained are shown in
figures 10 to 17.
II. C ONCLUSIONS
Different methods were evaluated to calculate derivatives
and integrals. Among numerical differentiation methods, when
the method is used to evaluate derivatives for an interval (not
only one point), it is recommended to review extreme values
and expressions modified in accordance to revision. (omitted
for work code on appendix C ).
The best to make error lesser, it is to use fourth order central
differences, although there are functions were the difference
is not appreciated the computational cost is low.

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

logerror v n for (
x) exp (2 * x) from x=2,4,6, to x=8

logerror v n for (
x) 2 .* (log (x)) . 2 + 3 .* sin (x) from x=2,4,6, to x=8
0
10

10

Newton Cotes
Gauss

Newton Cotes
Gauss
0

10

10-5
10-5
-10

10
-10

10

10-15
10-15

-20

-20

10

10

10

Fig. 10. Error v number of terms used for integration of e2x

10

Fig. 13. Error v number of terms used for integration of 2x log(x)2 +3 sin(x)

logerror v n for (
x) x .* log (x) from x=2,4,6, to x=8
100

logerror v n for (
x) exp (2 .* x) - cos (2 .* x) from x=2,4,6, to x=8
105

Newton Cotes
Gauss

10-2

Newton Cotes
Gauss
100

10-4
10-6

10-5

10-8
10-10

10-10
10-12

10-15
10-14
10-16
0

10

Fig. 11. Error v number of terms used for integration of x log(x)

logerror v n for (
x) x .* cos (x) - x . 2 .* sin (x) from x=2,4,6, to x=8
105
Newton Cotes
Gauss

10-20
0

10

Fig. 14. Error v number of terms used for integration of e2x cos(2x)

logerror v n for (
x) log (x + 2) - (x + 1) . 2 from x=2,4,6, to x=8
100
Newton Cotes
Gauss

10-2

100
10-4
10-5

10

-6

10

-8

-10

10

10-10
-12

10

-15

10

-14

10
-20

10

10

Fig. 12. Error v number of terms used for integration of x cos(x)x2 sin(x)

The application of numerical jacobian (celtral differences


4th order) for newton raphson method do not show any
difference to analytical results.
Using integration methods show in several cases better
results for gauss quadrature option. It is convenient to know the
function form for integration, if the function has positive and
negative values the numerical integration through quadrature
methods can make bad approximations.

10-16
0

10

Fig. 15. Error v number of terms used for integration of log(x+2)(x+1)2x

A PPENDIX A
S OURCE CODE FOR NUMERICAL DERIVATIVE
function NDev=NumDev (f,x,dx,method)
nv=length(x);
switch method
case {l1 1}
NDev =(f(x)-f(x-dx))./dx;

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

logerror v n for (
x) x .* sin (x) + x . 2 .* cos (x) from x=2,4,6, to x=8
0
10

figure(1100+fn)
hold on

Newton Cotes
Gauss

for x = 1:10
dx=0.1;

10-5

-10

10

10-15

-20

10

10

Fig. 16. Error v number of terms used for integration of x sin(x)+x2 cos(x)

logerror v n for (
x) (cos (3 .* x)) . 2 - exp (2 .* x) from x=2,4,6, to x=8
102
Newton Cotes
Gauss
100

10-2

10-4

10-6

10-8

10-10
0

10

Fig. 17. Error v number of terms used for integration of cos(3x)2 e2x

case {l2 2}
NDev = (3*f(x)-4*f(x-dx)+...
f(x-2*dx))./(2*dx);
case {r1 3}
NDev =(f(x+dx)-f(x))./dx;
case {r2 4}
NDev = (-3*f(x)+4*f(x+dx)...
-f(x+2*dx))./(2*dx);
case {c2 5}
NDev =(f(x+dx)-f(x-dx))./(2*dx);
case {c4 6}
NDev =(-f(x+2*dx) +8*f(x+dx) ...
-8*f(x-dx) +f(x-2*dx))./(12*dx);
end
end
A PPENDIX B
S OURCE CODE FOR DEVELOPING FIRST EXERCISE
function dev1()
clrlst=brgkcmy;
%plotfs();
%dev1a();
for fn = 1:8
[f,Df]=selfun(fn);

for n = 2:4
switch n
% select methods in
% NumDev function
% that uses n terms
case 2
mlist=[1 3 5 ];
case 3
mlist=[2 4];
case 4
mlist=[6];
end
for l=1:length(mlist)
err=(abs((NumDev(f,x,dx, ...
mlist(l))-Df(x))/Df(x)));
semilogy(n,err,[o ...
clrlst(mlist(l))])
end
end
end
xlim([1 5])
%title([log error v n for function ...
%,strrep(char(f),@(x),) ])
title(...
[error v n for function ...
,func2str(f) ])
xlabel(n)
ylabel(error)
legend(left 1st, ...
right 1st, ...
central 2nd, ...
left 2nd,...
right 2nd,...
central 4th)
legend(boxoff)
print([dev1plt, num2str(fn),.eps]...
, -depsc,-S1200,900)
%
print([dev1plt, num2str(fn),...
%
.png], -dpng)
end
end
function plotfs()
for k=1:8
[f,Df]=selfun(k);
x = linspace(0,10);
y = f(x);
figure(k)
plot(x,y);
end
end

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

function [f Df]=selfun (numeqn)


%select function and its derivative
switch numeqn
case 1
f=@(x) exp(2.*x);
Df=@(x) 2.*exp(2.*x);
case 2
f=@(x) x.*log(x);
Df=@(x) log(2.*x+1);
case 3
f=@(x) x.*cos(x) - x.2.*sin(x);
Df=@(x) cos(x)-3.*x.*sin(x)- x.2 ...
.*cos(x);
case 4
f=@(x) 2.*(log(x)).2+3.*sin(x);
Df=@(x) 3.*cos(x)+4.*log(x)./x;
case 5
%
f=@(x) 0;
f=@(x) exp(2.*x)-cos(2.*x);
Df=@(x) 2.*(exp(2.*x)+sin(2.*x));
case 6
f=@(x) log(x+2) -(x+1).2;
Df=@(x) -2.*(x+1)+1./(x+2) ;
case 7
f=@(x) x.*sin(x)+x.2.*cos(x);
Df=@(x) 3.*x.*cos(x) + (1-x.2)...
.*sin(x);
case 8
f=@(x) (cos(3.*x)).2-exp(2.*x);
Df=@(x) -3.*sin(6.*x)-2.*exp(2.*x);
end

%for each case generate a tmp matrix


%with diagonals contents.
%and it is modified for extreme values
tmp = ones(N,3)*diag([-1 1 0]);
tmp(1,2)=-1;
tmp(2,3)=1;
%left
Dl = full(spdiags(tmp,[-1 0 1 ]...
,N,N))/dx;
tmp = ones(N,3)*diag([0 -1 1]);
tmp(N-1,1)=-1;
tmp(N,2)=1;
%right
Dr = full(spdiags(tmp,[-1 0 1 ]...
,N,N))/dx;
tmp = (ones(N,3)*diag([-1 0 1]));
% (2 )/2dx
tmp(1,2)=-2;
tmp(2,3)=2;
tmp(N-1,1)=-2;
tmp(N,2)=2;
%center
Dc= full(spdiags(tmp,[-1 0 1]...
, N,N))/(2*dx);
x= min:(max-min)/(N-1):max;
clrlst=ybrgcmkb;
%figure (10)
%hold on

end
eqnm=k;

A PPENDIX C
S OURCE CODE FOR DEVELOPING THIRD EXERCISE
function dev1b()
for k =7 %1:8
errl=[];
errl2=[];
errr=[];
errc=[];
errm=[];
err2=[];
Nlist=[];
for N = 10 :30: 1000 %N= 5 %points number
min= 1;
max= 10;
dx= (max-min)/(N-1);
%Derivative matrix generation

switch eqnm
case 1
f=@(x) exp(2.*x);
Df=@(x) 2.*exp(2.*x);
case 2
f=@(x) x.*log(x);
Df=@(x) log(2.*x+1);
case 3
f=@(x) x.*cos(x) - x.2.*sin(x);
Df=@(x) cos(x)-3.*x.*sin(x)- ...
x.2 .*cos(x);
case 4
f=@(x) 2.*(log(x)).2+3.*sin(x);
Df=@(x) 3.*cos(x)+4.*log(x)./x;
case 5
f=@(x) 0;
f=@(x) exp(2.*x)-cos(2.*x);
Df=@(x) 2.*(exp(2.*x)+...
sin(2.*x));
case 6
f=@(x) log(x+2) -(x+1).2;
Df=@(x) -2.*(x+1)+1./(x+2) ;
case 7

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

f=@(x) x.*sin(x)+x.2.*cos(x);
Df=@(x) 3.*x.*cos(x) + ...
(1-x.2).*sin(x);
case 8
f=@(x) (cos(3.*x)).2-...
exp(2.*x);
Df=@(x) -3.*sin(6.*x)-...
2.*exp(2.*x);
end
y=f(x);
Nlist=[Nlist N];
Dfx=Df(x);
Dy = Dl * y;
errl= [errl norm(Dy-Dfx)./sqrt(N-1)];
errl2= [errl2 errorf(Dy,Df(x)) ];
Dy =[] ;
for l=1:length(x)
Dyn=NumDev(f,x(l),dx,l2);
Dy=[Dy Dyn];
end
errm=[errm norm(Dy-Dfx)./sqrt(N-1)];
%Dy =[] ;
%for l=1:length(x)
%Dyn=NumDev(f,x(l),dx,"c4");
%
Dy=[Dy Dyn];
%end
%err2=[err2 norm(Dy-Dfx)./sqrt(N-1)];

%
%
%
%
%

plot
plot
plot
plot
plot

(Nlist,errl,b)
(Nlist,errl2, k)
(Nlist,errm,g)
(Nlist,errr, c)
(Nlist,errc, m)

figure(110+k)
hold on
semilogy(Nlist,errl, b)
semilogy(Nlist,errl2, k)
semilogy(Nlist,errr, c)
semilogy(Nlist,errc, m)
semilogy (Nlist,errm,g)
title(log error v points)
legend(left 1st,left 1st a, left 2nd, ri
legend(boxoff);
print([err,num2str(110+k),.eps],-depsc);
end %first k
end %function

function er=errorf( A,B)


dt = size(A,2)-1;
er = norm(A(2:dt)-B(2:dt))...
/sqrt(dt-2);
end
Derivative of (
x) x .* sin (x) + x . 2 .* cos (x)
40
left 1st
right 1st
central
analytical
20

Dy = Dr * y;
errr= [errr norm(Dy-Dfx)./sqrt(N-1)];
%errr= [errr error(Dy,Df(x)) ];

-20

Dy = Dc * y;
errc= [errc norm(Dy-Dfx)./sqrt(N-1)];
%errc= [errc error(Dy,Df(x)) ];

-60

%%%figure(k)
%plot (x,y, clrlst(k) )
figure(k)
hold on
plot(x,Dl*y,c, x,Dr*y,y...
,x,Dc*y,r,x,Df(x),k )

%
%

end %N
title([Derivative of ,...
func2str(f)]);
legend(left 1st,right 1st,...
central,analytical)
print([derfun,num2str(k) ...
,.eps],-depsc)
figure (100+k)
hold on

-40

-80
0

10

Fig. 18. Numeric Differentiation, comparison

A PPENDIX D
S OURCE CODE FOR NUMERICAL JACOBIAN
function J = Jacob(f,z,dz)
%f vectorial function, n variables
% z vector n elements around
%will be evaluated,
% dz vector n elements
%f1dx f2dx
J=[];
for i= 1:length(z)
%matriz to select dz element
T=zeros(length(z));
T(i,i)=1;

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

iter = 0;
xiter=iter;

log error v points


102

left 1st
left 1st a
left 2nd
right 1st
central 2nd

101

x=x0;
x2=x0;

100

errf= 1;
errf2=1;
10-1

10-2

10-3
0

200

400

600

800

1000

Fig. 19. Log error v points for several methods using matrix for differentiation

%each parcial derivative variable i


%evaluated Central differences 4th order
tmp= (-f(z+2*T*dz)...
+8*f(z+T*dz)...
-8*f(z-T*dz)...
+f(z-2*T*dz))./(12*dz(i)); ...
J=[J tmp];
end
end
A PPENDIX E
S OURCE CODE FOR DEVELOPING SECOND EXERCISE
function dev2()
tolf = 1e-6;
dh=tolf;
x0=[2;4]; %x0=[6 1]
colorlist = [b,m,c, r];
for m = 1:2
switch m
%select function
case 1
%function
f= @(x) [x(1).2+x(2).2-2 ;...
x(1)+x(2)-2 ];
%jacobian
fp= @(x) [2.*x(1) 2.*x(2); 1 1 ];
case 2
%function
f= @(x) [x(1).2-x(1).*x(2)+3 ...
.*x(2).2-27 ; x(1)-x(2)-2 ];
%jacobian
fp= @(x) [2.*x(1)-x(2), ...
6.*x(2)-x(1) ; 1, -1 ];
end

while (errf(end) > tolf)


%fp(x)
fp2=Jacob(f,x2,[0.01;0.01]);
norm(fp2-fp(x))
h = mldivide ( fp(x), f(x) );
h2 = mldivide ( fp2, f(x2) );
xn = x-h;
xn2 = x2-h2;
errf=[ errf abs(max(f(xn))) ];
errf2=[ errf2 abs(max(f(xn2))) ];
x=xn;
x2=xn2;
iter = iter +1;
xiter = [ xiter iter ];
if iter >200
break;
end
end
figure(70);
hold on;
semilogy(xiter, errf,colorlist(m));
semilogy(xiter, errf2,...
[*,colorlist(m+2)]);
end
legend( original 1 , numeric J 1,...
original 2, numeric J 2);
legend(boxoff)
print(graph1_7_1.eps,-depsc,-S800,600);
end
A PPENDIX F
S OURCE CODE FOR DIFFERENT INTEGRATION METHODS
function integ=integrate(f,a,b,method,n)
switch method
case {1 trapecio}
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:length(x)-1
integ=integ+h/2 ...
*(f(x(k))+f(x(k+1)));
end
case{2 simpson}

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

if (mod(n,2)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k = 1:2:length(x)-2
integ=integ + h/3*( f(x(k)) ...
+ 4*f(x(k+1)) + f(x(k+2)) );
end
else
disp(n should be mult(2))
integ=NaN;
end
case{3 simpson38}
if(mod(n,3)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:3:length(x)-2
integ=integ+3/8*h...
*(f(x(k))+3*f(x(k+1))...
+3*f(x(k+2))+f(x(k+3)));
end
else
disp(n should mult(3))
integ=NaN;
end
case{4 bode}
if(mod(n,4)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:4:length(x)-3
integ=integ+2/45*h ...
*(7*(f(x(k))+f(x(k+4))) ...
+32*(f(x(k+1))+f(x(k+3)))...
+12*f(x(k+2)));
end
else
disp(n should mult(4))
integ=NaN;
end
case {5 }
if(mod(n,5)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:5:length(x)-4
integ=integ+5*h/288 ...
*(19*(f(x(k))+f(x(k+5)) ) ...
+75*(f(x(k+1))+f(x(k+4)) ) ...
+50*(f(x(k+2))+f(x(k+3)) ));
end
else
disp(n should mult(5))
integ=NaN;
end
case {6}
if(mod(n,6)==0)

h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:6:length(x)-5
integ=integ+h/140*...
(41*(f(x(k)) + f(x(k+6)) )...
+216*( f(x(k+1))+f(x(k+5)))...
+27*( f(x(k+2))+f(x(k+4)))...
+272*f(x(k+3)) );
end
else
disp(n should mult(6))
integ=NaN;
end
%%
case {7}
if(mod(n,7)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:7:length(x)-6
integ=integ+7*h/17280*...
(751*(f(x(k)) + f(x(k+7)) ) ...
+ 3577*(f(x(k+1)) + f(x(k+6))) ...
+ 1323*(f(x(k+2)) + f(x(k+5))) ...
+ 2989*(f(x(k+3)) + f(x(k+4))));
end
else
disp(n should mult(7))
integ=NaN;
end
case {8}
if(mod(n,8)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:8:length(x)-7
integ=integ+4*h/14175* ...
(989*(f(x(k)) + f(x(k+8)) ) ...
+ 5888*(f(x(k+1))+f(x(k+7))) ...
- 928*(f(x(k+2))+f(x(k+6)) ) ...
+10496*(f(x(k+3))+f(x(k+5))) ...
- 4540*f(x(k+4)) );
end
else
disp(n should mult(8))
integ=NaN;
end
case {9}
if(mod(n,9)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:9:length(x)-8
integ=integ+9*h/89600*...
(2857*(f(x(k)) + f(x(k+9)) ) ...
+15741*(f(x(k+1))+f(x(k+8))) ...
+ 1080*(f(x(k+2))+f(x(k+7))) ...

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

+ 19344*(f(x(k+3))+f(x(k+6)))...
+ 5778*(f(x(k+4))+f(x(k+5))));
end
else
disp(n should mult(9))
integ=NaN;
end
case {10}
if(mod(n,10)==0)
h=(b-a)/n;
x=a:h:b;
integ=0;
for k=1:10:length(x)-9
integ=integ+5*h/299376 ...
(16067
*
*(f(x(k))+f(x(k+10)))...
+106300*(f(x(k+1))+f(x(k+9)))...
-48525*(f(x(k+2))+f(x(k+8)))...
+272400*(f(x(k+3))+f(x(k+7)))...
-260550*(f(x(k+4))+f(x(k+6)))...
+427368*f(x(k+5)));
end
else
disp(n should mult(10))
integ=NaN;
end
%%
otherwise
m=method-10;
%case {11 gauss2}
if(mod(n,m)==0)
h=(b-a)/n;
integ=0;
for ca=a:m*h:(b-m*h)
cb=ca+m*h;
%lgwt function calculates weigths and
%computes x values, for calculating
%integral using gauss-legendre quadrature
%downloaded from:
%http://www.mathworks.com/matlabcentral
%/fileexchange/4540-legendre-gauss%quadrature-weights-and-nodes/content/
%lgwt.m

%lower limit
min = 2;
%upper limit
max = 8;
for k = 1:8
for min = 2:2:6
%select function f
%and its analytical integral
[f,Intf]=selfun(k);
ln = [];
lerr = [];
lerrg = [];
figure(300+k)
hold on
for n= 1:10
switch n
case 1
%
mlist = [11];
err=abs(...
(integrate(f,min,max,11,n)...
-(Intf(max)-Intf(min)))./ ...
(Intf(max)-Intf(min)));
%
ln=[ln n];
%
lerrg=[lerrg err];
semilogy(n,err,*g)

end
end
A PPENDIX G
S OURCE CODE FOR DEVELOPING THIRD EXERCISE
function dev3()

otherwise
mlist = [n-1,10+n]
ln = [ln n];
err=abs(...
(integrate(f,min,max,n-1,2*(n-1))...
-(Intf(max)-Intf(min)))./ ...
(Intf(max)-Intf(min)));
lerr = [lerr err];

%
[x, w] = lgwt(m,ca,cb);
integ=integ+sum(w.*f(x));
end
else
disp([n should mult(, ...
num2str(m) ,)])
integ=NaN;
end

plotfs();

semilogy(n,err,*r)
errg=abs(...
(integrate(f,min,max,10+n,2*(n))...
-(Intf(max)-Intf(min)))./ ...
(Intf(max)-Intf(min)));
lerrg = [lerrg errg];
%semilogy(n,err,*g)
end
end
lerr
lerrg
switch min
case 2
t=:;

UNIVERSIDAD CENTRAL - UNIVERSIDAD JORGE TADEO LOZANO,MAYO 2015

case 4
t=--;
case 6
t=;
end
semilogy(ln,lerr,[t,b])
semilogy(ln,lerrg,[t,g])
end
title([logerror v n for ,...
func2str(f) , from x=, ...
2,4,6,, ...%num2str(min),
to x=,...
num2str(max)])
legend( ,Newton Cotes,Gauss);
legend(boxoff);
print([sln3_,... %num2str(min), ...
sev8_, num2str(k),.eps]...
, -depsc,-S800,600);
end
end
function [f Intf]=selfun(k)
switch k
case 1
f=@(x) exp(2*x);
Intf=@(x) exp(2*x)/2;
case 2
f=@(x) x.*log(x);
Intf=@(x) x2*(2*log(x)-1)/4;
case 3
f=@(x) x.*cos(x)-x.2.*sin(x);
Intf=@(x) (x.2-1).*cos(x)-x.*sin(x);
case 4
f=@(x) 2.*(log(x)).2+3.*sin(x);
Intf=@(x) 2.*x.*((log(x)).2-2.* ...
log(x)+2) -3.*cos(x);
case 5
f=@(x) exp(2.*x)-cos(2.*x);
Intf=@(x) (exp(2.*x) -sin(2.*x))./2;
case 6
f=@(x) log(x+2)-(x+1).2;
Intf=@(x) (x+2).*log(x+2)...
-x.*(x.2+3.*x+6)/3;
case 7
f=@(x) x.*sin(x)+x.2.*cos(x);
Intf=@(x) (x.2-1).*sin(x)+x.*cos(x);
case 8
f=@(x) (cos(3.*x)).2-exp(2.*x);
Intf=@(x)(6.*x-6.*exp(2.*x))./12;
end
end

function plotfs()
for k=1:8
[f,Intf]=selfun(k);
x = linspace(0,10);
y = f(x);

10

figure(k)
plot(x,y);
end
end
R EFERENCES
[1] S. R. Otto and J. P. Denier, An Introduction to Programming and
Numerical Methods in MATLAB. Springer, 2005.
[2] T. Young and M. J. Mohlenkamp, Introduction to Numerical Methods
and Matlab Programming for Engineers. Athens, Ohio: Department of
Mathematics Ohio University , 2014.
[3] http://www.mathworks.com/matlabcentral/fileexchange/4540-legendregauss-quadrature-weights-and-nodes/content/lgwt.m

Potrebbero piacerti anche