0% found this document useful (0 votes)
36 views

Algoritmo para El Método de Bisección

The document contains algorithms for several numerical methods to find the root of a function: bisection method, secant method, Jacobi iterative method, and Lagrange interpolation method. It includes the code implementation of each method in MATLAB and examples of running the code on sample functions to find a root between given bounds or the value of a function at a given point using past data points.

Uploaded by

brigida_carvajal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Algoritmo para El Método de Bisección

The document contains algorithms for several numerical methods to find the root of a function: bisection method, secant method, Jacobi iterative method, and Lagrange interpolation method. It includes the code implementation of each method in MATLAB and examples of running the code on sample functions to find a root between given bounds or the value of a function at a given point using past data points.

Uploaded by

brigida_carvajal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Algoritmo para el mtodo de biseccin

function raiz=biseccion(a,b)
e=0.0005;
k=0;
fxa=f1(a);
fxb=f1(b);
if fxa*fxb<=0
while (abs(b-a)/2)>e
m=(a+b)/2;
fprintf('%5d%10.5f%10.5f%10.5f\n',k,a,b,m);
k=k+1;
fxm=f1(m);
if fxa*fxm<=0
b=m;
fxb=fxm;
else
a=m;
fxa=fxm;
end
end
raiz=m;
else
fprintf('cambiar limites');
end

function y=f1(x);
y=sqrt(x)-3*log(x);

biseccion(0.1,2)
0 0.10000 2.00000
1 1.05000 2.00000
2 1.05000 1.52500
3 1.28750 1.52500
4 1.40625 1.52500
5 1.46563 1.52500
6 1.49531 1.52500
7 1.49531 1.51016
8 1.50273 1.51016
9 1.50273 1.50645
10 1.50459 1.50645
ans =
1.5055

1.05000
1.52500
1.28750
1.40625
1.46563
1.49531
1.51016
1.50273
1.50645
1.50459
1.50552

Algoritmo para el mtodo de la Secante

function raiz=secante(x1,x2)
e=0.0005;
nt=50;
k=0;
while 1
x=x2-f1(x2)/( (f1(x2)-f1(x1) )/(x2-x1));
fprintf('%5d%10.5f%10.5f%10.5f\n',k,x1,x2,x);
k=k+1;
x1=x2;
x2=x;
if (abs(x1-x2)<=e | k==nt)
break;
end
end
if k<nt
raiz=x;
end

>> secante(0.1,1)
0 0.10000 1.00000
1 1.00000 1.14460
2 1.14460 1.43125
3 1.43125 1.49485
4 1.49485 1.50497
ans =
1.5053

1.14460
1.43125
1.49485
1.50497
1.50525

Algoritmo para el mtodo de Jacobi


function x=jacobi(A,b);
[n,n]=size(A);
x=zeros;
y=zeros;
e=0.0005;
nt=50;
k=0;
fprintf('%5d',k);
for m=1:n
fprintf('%10.5f',x(m));
end;
while 1
f=1;
for i=1:n
s=0;
for j=1:n
if i~=j
s=s+A(i,j)*x(j)/A(i,i);
end
end
y(i)=b(i)/A(i,i)-s;
end
k=k+1;
fprintf('\%10.5f',k);
for i=1:n
if abs(y(i)-x(i))<e
f=0;
end
x(i)=y(i);
fprintf('\%10.5f',x(i));
end
if (nt==k)|(f==1)
break
end
end

>> jacobi(a,b)
0 0.00000
1 -1.50000
2 -2.93750
3 -2.77083
4 -2.78516
5 -2.76671
6 -2.76874
7 -2.76821
8 -2.76845
ans =
-2.7684
3.3684
-1.6632

0.00000
3.00000
3.41667
3.39583
3.37326
3.36849
3.36824
3.36837
3.36842

0.00000
-0.25000
-1.75000
-1.65104
-1.67969
-1.66200
-1.66363
-1.66296
-1.66318

Algoritmo para el mtodo de Lagrange


function p=Lagrange(x,y,x0)
n=length(x)
l=zeros(n,1);
fprintf('
k
x
y
L\n');
for k=1:n
prod1=1;
prod2=1;
for i=1:n
if i~=k
prod1=prod1.*(x0-x(i));
prod2=prod2.*(x(k)-x(i));
end
end
l(k)=prod1./prod2;
end
p=0;
for k=1:n
p=p+l(k).*y(k);
fprintf('%5d%10.6f%10.6f%10.6f\n',k,x(k),y(k),l(k));
end

>> Lagrange(x,y,1975)
n=
5
k
1
2
3
4
5

x
1950
1960
1970
1980
1990

ans =
1.4408e+003

y
827
1058
1304
1582
1836

L
0.023438
-0.156250
0.703125
0.468750
-0.039063

You might also like