0% found this document useful (0 votes)
19K views10 pages

Susan

The document contains code for calculating derivatives of a function f(x) using finite difference methods. It defines functions f1, f2, f3, f4 to calculate the 1st, 2nd, 3rd and 4th derivatives using different step sizes h. The derivada function calls these to calculate the derivatives and prints the results for given x and h values. It also contains code for numerical integration using the trapezium rule.

Uploaded by

David Rios Vera
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19K views10 pages

Susan

The document contains code for calculating derivatives of a function f(x) using finite difference methods. It defines functions f1, f2, f3, f4 to calculate the 1st, 2nd, 3rd and 4th derivatives using different step sizes h. The derivada function calls these to calculate the derivatives and prints the results for given x and h values. It also contains code for numerical integration using the trapezium rule.

Uploaded by

David Rios Vera
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

function d=derivada (x,h)

y(1)=f1(x,h)

y(2)=f2(x,h)

y(3)=f3(x,h)

y(4)=f4(x,h)

d=y'

fprintf('\n\nf(%7.4f)=%7.4f\n',x,y(1))

fprintf('f(%7.4f)=%7.4f\n',x,y(2))

fprintf('f(%7.4f)=%7.4f\n',x,y(3))

fprintf('f(%7.4f)=%7.4f\n',x,y(4))

function y1=f1(x,h)

y1=(f(x+h)-f(x-h))/(2*h)

function y2=f2(x,h)

y2=(f(x+h)-2*f(x)+f(x-h))/h.^2

function y3=f3(x,h)

y3=(f(x+2*h)-2*f(x+h)+2*f(x-h)-f(x-2*h))/(2*h.^2)

function y4=f4(x,h)

y4=(f(x+2*h)-4*f(x+h)+6*f(x)-4*f(x-h)+f(x-2*h))/(h.^4)

function y=f(x)

y= exp(sin(x^2-1/x))

d=derivada (x,h)

??? Undefined function or variable 'x'.

>> x=0.8
x=

0.8000

>> h=0.001

h=

1.0000e-003

>> d=derivada (x,h)

??? Undefined function or method 'derivada' for input arguments of type

'double'.

??? Input argument "x" is undefined.

Error in ==> susaaaa at 2

y(1)=f1(x,h)

??? Input argument "x" is undefined.

Error in ==> derivada at 2

y(1)=f1(x,h)

>> d=derivada (x,h)


y=

0.5654

y=

0.5624

y1 =

1.4617

y=

1.4617

y=

0.5654

y=
0.5639

y=

0.5624

y2 =

6.1388

y=

1.4617 6.1388

y=

0.5668

y=
0.5654

y=

0.5624

y=

0.5610

y3 =

0.0144

y=

1.4617 6.1388 0.0144

y=

0.5668
y=

0.5654

y=

0.5639

y=

0.5624

y=

0.5610

y4 =

57.6975
y=

1.4617 6.1388 0.0144 57.6975

d=

1.4617

6.1388

0.0144

57.6975

f( 0.8000)= 1.4617

f( 0.8000)= 6.1388

f( 0.8000)= 0.0144

f( 0.8000)=57.6975

d=

1.4617

6.1388

0.0144

57.6975
f1x=1.4617

f1x =

1.4617

>> f3x=0.0144

f3x =

0.0144

>> f=f1x-((0.001)^3/6)*f3x

f=

1.4617

function Ih=trapecio(a,b,I2h,k)

if k==1

fa=fun(a);

fb=fun(b);

Ih=(fa+fb)*(a/b)/2.0;

else

n=2^(k-2);
h=(b-a)/n;

x=a+h/2.0;

sum=0.0;

for i=1:n

fx=fun(x);

sum=sum+fx;

x=x+h;

end

Ih=(I2h+h*sum)/2.0;

end

%%funcion a integrar

function y=fun(x)

y=exp(sin(x^2-1/x));

>> a= 0

a=

>> b=2

b=

2
>> I2h=5

I2h =

>> k=6

k=

>> Ih=trapecio(a,b,I2h,k)

Ih =

3.8934

>>

You might also like