You are on page 1of 11

ans =

1.
-->pwd
ans =
C:\Users\usuario\Documents
-->mode(7)
//Activa el modo de tutorial para el ejecutable
Modo de pausa: ingrese lneas vacas para continuar.
>>clc
//Limpia la pantalla
>>clear
//Borra todas las varible de la lnea de comandos
>>//*************************************************************
>>//***** Tutorial en Scilab sobre derivacin numrica *********
>>//*************************************************************
>>//Primero vamos a crear una funcin con sus respectivas derivadas
>>//La funcin es f(x)=2*x^2-cos(x)
>>deff("y=f(x)","y=2*x^2-cos(x)")
>>//Esta es la primera derivada de la funcin, f'(x)=4*x+x*sin(x)
>>deff("y=df(x)","y=4*x+x*sin(x)")
>>//Esta es la segunda derivada de la funcin, f''(x)=4+x*cos(x)
>>deff("y=d2f(x)","y=4+x*cos(x)")
>>//Ahora vamos a probar la funcin y sus dos primeras derivadas para x=0.5
>>f(1)
//La imagen de la funcin en x=0.5
ans =
1.4596977
>>df(1)
//La imagen de la primera derivada de la funcin en x=0.5
ans =
4.841471
>>d2f(1)
//La imagen de la la segunda derivada de la funcin en x=0.5
ans =
4.5403023
>>//Vamos a crear una funcin que aproxime la derivada de una funcin cualquiera
>>//por va numrica
>>//En la funcin vamos a incluir las diferencias progresivas, regresivas y centra
les.
>>deff("[dp,dr,dc]=dfn(f,x,h)",...
>>"dp=(f(x+h)-f(x))/h,dr=(f(x)-f(x-h))/h,dc=(f(x+h)-f(x-h))/(2*h)")
>>//Observe que la funcin se llama dfn: derivada de la funcin por va numrica
>>//Adems, la funcin anterior tiene tres salidas:
>>//dp (derivada progresiva), dc (derivada regresiva) y dc (derivada central)
>>//Vamos a probar la funcin para x=0.5 y h=0.1
>>x=0.5
x =
0.5
>>h=0.1
h =
0.1
>>[dp,dr,dc]=dfn(f,x,h)
dc =
2.4786269
dr =

2.2347843
dp =
2.7224695
>>//Pero la derivada real o analtica es:
>>dreal=df(1)
dreal =
4.841471
>>//Ahora vamos a organizar los resultados en una matriz llamada R
>>//En la primera columna el paso
>>R(1,1)=h
R =
0.1
>>R(2,1)=h
R =
0.1
0.1
>>R(3,1)=h
R =
0.1
0.1
0.1
>>//En la segunda columna la derivada numrica por los tres mtodos
>>R(1,2)=dp
R =
0.1
2.7224695
0.1
0.
0.1
0.
>>R(2,2)=dr
R =
0.1
2.7224695
0.1
2.2347843
0.1
0.
>>R(3,2)=dc
R =
0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
>>//En la terecera columna la deriva real o analtica
>>R(1,3)=dreal
R =
0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
>>R(2,3)=dreal
R =

4.841471
0.
0.

0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
>>R(3,3)=dreal

4.841471
4.841471
0.

R =
0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
>>//En la cuarta columna los errores absolutos
>>R(1,4)=abs(dp-dreal)
R =
0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
>>R(2,4)=abs(dr-dreal)
R =

2.1190015
0.
0.

0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
>>R(3,4)=abs(dc-dreal)
R =

2.1190015
2.6066867
0.

0.1
2.7224695
4.841471
2.1190015
0.1
2.2347843
4.841471
2.6066867
0.1
2.4786269
4.841471
2.3628441
>>//En la quinta columna los errores relativos
>>R(1,5)=abs((dp-dreal)/dreal)
R =
0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
>>R(2,5)=abs((dr-dreal)/dreal)
R =

2.1190015
2.6066867
2.3628441

0.4376772
0.
0.

0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
>>R(3,5)=abs((dc-dreal)/dreal)
R =

2.1190015
2.6066867
2.3628441

0.4376772
0.5384080
0.

0.1
0.1
0.1
>>//Vamos
>>x=0.5
x =

2.7224695
4.841471
2.2347843
4.841471
2.4786269
4.841471
a probar la funcin para

0.5
>>h=0.01
h =
0.01
>>[dp,dr,dc]=dfn(f,x,h)
dc =
2.4794175
dr =
2.4550297
dp =

2.1190015
0.4376772
2.6066867
0.5384080
2.3628441
0.4880426
x=0.5 y h=0.01

2.5038054
>>//Pero la derivada real o analtica es:
>>dreal=df(1)
dreal =
4.841471
>>//Ahora vamos a organizar los resultados en una matriz llamada R
>>//En la primera columna el paso
>>R(4,1)=h
R =
0.1
0.1
0.1
0.01
>>R(5,1)=h
R =

2.7224695
2.2347843
2.4786269
0.

4.841471
4.841471
4.841471
0.

2.1190015
2.6066867
2.3628441
0.

0.4376772
0.5384080
0.4880426
0.

0.1
0.1
0.1
0.01
0.01
>>R(6,1)=h
R =

2.7224695
2.2347843
2.4786269
0.
0.

4.841471
4.841471
4.841471
0.
0.

2.1190015
2.6066867
2.3628441
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.

0.1
2.7224695
4.841471
2.1190015
0.4376772
0.1
2.2347843
4.841471
2.6066867
0.5384080
0.1
2.4786269
4.841471
2.3628441
0.4880426
0.01
0.
0.
0.
0.
0.01
0.
0.
0.
0.
0.01
0.
0.
0.
0.
>>//En la segunda columna la derivada numrica por los tres mtodos
>>R(4,2)=dp
R =
0.1
0.1
0.1
0.01
0.01
0.01
>>R(5,2)=dr
R =

2.7224695
2.2347843
2.4786269
2.5038054
0.
0.

4.841471
4.841471
4.841471
0.
0.
0.

2.1190015
2.6066867
2.3628441
0.
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
0.1
0.1
0.01
0.01
0.01
>>R(6,2)=dc
R =

2.7224695
2.2347843
2.4786269
2.5038054
2.4550297
0.

4.841471
4.841471
4.841471
0.
0.
0.

2.1190015
2.6066867
2.3628441
0.
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
0.1
0.1
0.01
0.01

2.7224695
2.2347843
2.4786269
2.5038054
2.4550297

4.841471
4.841471
4.841471
0.
0.

2.1190015
2.6066867
2.3628441
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.

0.01
2.4794175
0.
0.
0.
>>//En la terecera columna la deriva real o analtica
>>R(4,3)=dreal
R =
0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
0.01
2.5038054
0.01
2.4550297
0.01
2.4794175
>>R(5,3)=dreal
R =

4.841471
4.841471
4.841471
4.841471
0.
0.

2.1190015
2.6066867
2.3628441
0.
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
0.01
2.5038054
0.01
2.4550297
0.01
2.4794175
>>R(6,3)=dreal
R =

4.841471
4.841471
4.841471
4.841471
4.841471
0.

2.1190015
2.6066867
2.3628441
0.
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
0.01
2.5038054
0.01
2.4550297
0.01
2.4794175
>>//En la cuarta columna
>>R(4,4)=abs(dp-dreal)
R =

4.841471
4.841471
4.841471
4.841471
4.841471
4.841471
los errores

2.1190015
2.6066867
2.3628441
0.
0.
0.
absolutos

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
0.01
2.5038054
0.01
2.4550297
0.01
2.4794175
>>R(5,4)=abs(dr-dreal)
R =

4.841471
4.841471
4.841471
4.841471
4.841471
4.841471

2.1190015
2.6066867
2.3628441
2.3376656
0.
0.

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
2.7224695
0.1
2.2347843
0.1
2.4786269
0.01
2.5038054
0.01
2.4550297
0.01
2.4794175
>>R(6,4)=abs(dc-dreal)
R =

4.841471
4.841471
4.841471
4.841471
4.841471
4.841471

2.1190015
2.6066867
2.3628441
2.3376656
2.3864413
0.

0.4376772
0.5384080
0.4880426
0.
0.
0.

2.1190015
2.6066867
2.3628441
2.3376656
2.3864413
2.3620534
relativos

0.4376772
0.5384080
0.4880426
0.
0.
0.

0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
0.01
2.5038054
4.841471
0.01
2.4550297
4.841471
0.01
2.4794175
4.841471
>>//En la quinta columna los errores
>>R(4,5)=abs((dp-dreal)/dreal)
R =

0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
0.01
2.5038054
4.841471
0.01
2.4550297
4.841471
0.01
2.4794175
4.841471
>>R(5,5)=abs((dr-dreal)/dreal)
R =

2.1190015
2.6066867
2.3628441
2.3376656
2.3864413
2.3620534

0.4376772
0.5384080
0.4880426
0.4828420
0.
0.

0.1
2.7224695
4.841471
0.1
2.2347843
4.841471
0.1
2.4786269
4.841471
0.01
2.5038054
4.841471
0.01
2.4550297
4.841471
0.01
2.4794175
4.841471
>>R(6,5)=abs((dc-dreal)/dreal)
R =

2.1190015
2.6066867
2.3628441
2.3376656
2.3864413
2.3620534

0.4376772
0.5384080
0.4880426
0.4828420
0.4929166
0.

0.1
2.7224695
4.841471
2.1190015
0.4376772
0.1
2.2347843
4.841471
2.6066867
0.5384080
0.1
2.4786269
4.841471
2.3628441
0.4880426
0.01
2.5038054
4.841471
2.3376656
0.4828420
0.01
2.4550297
4.841471
2.3864413
0.4929166
0.01
2.4794175
4.841471
2.3620534
0.4878793
>>//Las concluciones son las siguientes:
>>//1- Las formulas de diferencias progresivas y regresivas tienen a...
>>//
dar resultados con el mismo error para un mismo valor de x
>>//2- Las formulas de diferencias centrales son ms exactas que...
>>//
las formulas progresivas y regresivas para un mismo valor de x
>>//3- Al reducir el tamao del paso, todas las formulas tiende a ...
>>//
tener menos errorrer. La formula de diferencia central se acerca ...
>>//
ms rpido al valor real que las progresivas y regresivas
>>//Ahora vamos a mirar lo que ocurre con la formulas para segundas derivadas
>>//Primero, vamos a crear una funcin que aproxime la segunda derivada
>>//de una funcin cualquiera por va numrica
>>//En la funcin vamos a incluir las diferencias progresivas, regresivas y centra
les.
>>deff("[dp,dr,dc]=d2fn(f,x,h)",...
//Cabecera de la funcin
>>"dp=(f(x)-2*f(x+h)+f(x+2*h))/h^2,... //Derivada progresiva
>>dr=(f(x)-2*f(x-h)+f(x-2*h))/h^2,...
//Derivada regresiva
>>dc=(f(x-h)-2*f(x)+f(x+h))/h^2")
//Derivada central
>>//Observe que la funcin se llama d2fn: derivada segunda de la funcin por va numric
a
>>//Adems, la funcin anterior tiene tres salidas:
>>//dp (derivada progresiva), dc (derivada regresiva) y dc (derivada central)
>>//Vamos a probar la funcin para x=0.5 y h=0.1
>>x=0.5
x =
0.5
>>h=0.1
h =
0.1
>>[dp,dr,dc]=d2fn(f,x,h)
dc =
4.8768515
dr =

4.9202937
dp =
4.8246481
>>//Pero la segunda derivada real o analtica es:
>>d2real=d2f(1)
d2real =
4.5403023
>>//Ahora vamos a organizar los resultados en una matriz llamada R2
>>//En la primera columna el paso
>>R2(1,1)=h
R2 =
0.1
>>R2(2,1)=h
R2 =
0.1
0.1
>>R2(3,1)=h
R2 =
0.1
0.1
0.1
>>//En la segunda columna la derivada numrica por los tres mtodos
>>R2(1,2)=dp
R2 =
0.1
4.8246481
0.1
0.
0.1
0.
>>R2(2,2)=dr
R2 =
0.1
4.8246481
0.1
4.9202937
0.1
0.
>>R2(3,2)=dc
R2 =
0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
>>//En la terecera columna la deriva real o analtica
>>R2(1,3)=d2real
R2 =
0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
>>R2(2,3)=d2real
R2 =

4.5403023
0.
0.

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
>>R2(3,3)=d2real

4.5403023
4.5403023
0.

R2 =
0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
>>//En la cuarta columna los errores absolutos
>>R2(1,4)=abs(dp-d2real)
R2 =
0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
>>R2(2,4)=abs(dr-d2real)
R2 =

0.2843458
0.
0.

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
>>R2(3,4)=abs(dc-d2real)
R2 =

0.2843458
0.3799914
0.

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
>>//En la quinta columna los errores
>>R2(1,5)=abs((dp-d2real)/d2real)
R2 =

0.2843458
0.3799914
0.3365492
relativos

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
>>R2(2,5)=abs((dr-d2real)/d2real)
R2 =

0.2843458
0.3799914
0.3365492

0.0626271
0.
0.

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
>>R2(3,5)=abs((dc-d2real)/d2real)
R2 =

0.2843458
0.3799914
0.3365492

0.0626271
0.0836930
0.

0.1
0.1
0.1
>>//Vamos
>>x=0.5
x =

4.8246481
4.5403023
0.2843458
0.0626271
4.9202937
4.5403023
0.3799914
0.0836930
4.8768515
4.5403023
0.3365492
0.0741248
a probar la funcin para x=0.5 y h=0.01

0.5
>>h=0.01
h =
0.01
>>[dp,dr,dc]=d2fn(f,x,h)
dc =
4.8775752
dr =
4.8823255
dp =

4.8727372
>>//Pero la segunda derivada real o analtica es:
>>d2real=d2f(1)
d2real =
4.5403023
>>//Ahora vamos a organizar los resultados en una matriz llamada R2
>>//En la primera columna el paso
>>R2(4,1)=h
R2 =
0.1
0.1
0.1
0.01
>>R2(5,1)=h
R2 =

4.8246481
4.9202937
4.8768515
0.

4.5403023
4.5403023
4.5403023
0.

0.2843458
0.3799914
0.3365492
0.

0.0626271
0.0836930
0.0741248
0.

0.1
0.1
0.1
0.01
0.01
>>R2(6,1)=h
R2 =

4.8246481
4.9202937
4.8768515
0.
0.

4.5403023
4.5403023
4.5403023
0.
0.

0.2843458
0.3799914
0.3365492
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
0.01
0.
0.
0.01
0.
0.
0.01
0.
0.
>>//En la segunda columna la derivada
>>R2(4,2)=dp
R2 =

0.2843458
0.3799914
0.3365492
0.
0.
0.
numrica por

0.0626271
0.0836930
0.0741248
0.
0.
0.
los tres mtodos

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
0.
0.01
0.
>>R2(5,2)=dr
R2 =

4.5403023
4.5403023
4.5403023
0.
0.
0.

0.2843458
0.3799914
0.3365492
0.
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
4.8823255
0.01
0.
>>R2(6,2)=dc
R2 =

4.5403023
4.5403023
4.5403023
0.
0.
0.

0.2843458
0.3799914
0.3365492
0.
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.
0.

4.5403023
4.5403023
4.5403023
0.
0.

0.2843458
0.3799914
0.3365492
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.

0.1
0.1
0.1
0.01
0.01

4.8246481
4.9202937
4.8768515
4.8727372
4.8823255

0.01
4.8775752
0.
0.
0.
>>//En la terecera columna la deriva real o analtica
>>R2(4,3)=d2real
R2 =
0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
4.8823255
0.01
4.8775752
>>R2(5,3)=d2real
R2 =

4.5403023
4.5403023
4.5403023
4.5403023
0.
0.

0.2843458
0.3799914
0.3365492
0.
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
4.8823255
0.01
4.8775752
>>R2(6,3)=d2real
R2 =

4.5403023
4.5403023
4.5403023
4.5403023
4.5403023
0.

0.2843458
0.3799914
0.3365492
0.
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
4.8823255
0.01
4.8775752
>>//En la cuarta columna
>>R2(4,4)=abs(dp-d2real)
R2 =

4.5403023
4.5403023
4.5403023
4.5403023
4.5403023
4.5403023
los errores

0.2843458
0.3799914
0.3365492
0.
0.
0.
absolutos

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
4.8823255
0.01
4.8775752
>>R2(5,4)=abs(dr-d2real)
R2 =

4.5403023
4.5403023
4.5403023
4.5403023
4.5403023
4.5403023

0.2843458
0.3799914
0.3365492
0.3324349
0.
0.

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.1
4.8246481
0.1
4.9202937
0.1
4.8768515
0.01
4.8727372
0.01
4.8823255
0.01
4.8775752
>>R2(6,4)=abs(dc-d2real)
R2 =

4.5403023
4.5403023
4.5403023
4.5403023
4.5403023
4.5403023

0.2843458
0.3799914
0.3365492
0.3324349
0.3420232
0.

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.2843458
0.3799914
0.3365492
0.3324349
0.3420232
0.3372729
relativos

0.0626271
0.0836930
0.0741248
0.
0.
0.

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
0.01
4.8727372
4.5403023
0.01
4.8823255
4.5403023
0.01
4.8775752
4.5403023
>>//En la quinta columna los errores
>>R2(4,5)=abs((dp-d2real)/d2real)
R2 =

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
0.01
4.8727372
4.5403023
0.01
4.8823255
4.5403023
0.01
4.8775752
4.5403023
>>R2(5,5)=abs((dr-d2real)/d2real)
R2 =

0.2843458
0.3799914
0.3365492
0.3324349
0.3420232
0.3372729

0.0626271
0.0836930
0.0741248
0.0732187
0.
0.

0.1
4.8246481
4.5403023
0.1
4.9202937
4.5403023
0.1
4.8768515
4.5403023
0.01
4.8727372
4.5403023
0.01
4.8823255
4.5403023
0.01
4.8775752
4.5403023
>>R2(6,5)=abs((dc-d2real)/d2real)
R2 =

0.2843458
0.3799914
0.3365492
0.3324349
0.3420232
0.3372729

0.0626271
0.0836930
0.0741248
0.0732187
0.0753305
0.

0.1
4.8246481
4.5403023
0.2843458
0.0626271
0.1
4.9202937
4.5403023
0.3799914
0.0836930
0.1
4.8768515
4.5403023
0.3365492
0.0741248
0.01
4.8727372
4.5403023
0.3324349
0.0732187
0.01
4.8823255
4.5403023
0.3420232
0.0753305
0.01
4.8775752
4.5403023
0.3372729
0.0742842
>>//Las concluciones para la segunda derivada son las siguientes:
>>//1- Las formulas de diferencias progresivas y regresivas tienen a...
>>//
dar resultados con el mismo error para un mismo valor de x
>>//2- Las formulas de diferencias centrales son ms exactas que...
>>//
las formulas progresivas y regresivas para un mismo valor de x
>>//3- Al reducir el tamao del paso, todas las formulas tiende a ...
>>//
tener menos errorrer. La formula de diferencia central se acerca ...
>>//
ms rpido al valor real que las progresivas y regresivas
>>diary(0)