Você está na página 1de 17

Método de jacobi

function jacobi(A,b,x0,tol,itmax)
% Solve the system Ax=b using the Jacobi iteration method.
n=length(b);
x=zeros(n,1);
fprintf('\n');
disp(' The augmented matrix is =')
Augm=[A b]
Y=zeros(n,1);
Y=x0;
for k=1:itmax+1
for i=1:n
S=0;
for j=1:n
if (j~=i)
S=S+A(i,j)*x0(j);
end
end
if(A(i,i)==0)
break
end
x(i)=(-S+b(i))/A(i,i);
end
err=abs(norm(x-x0));
rerr=err/(norm(x)+eps);
x0=x;
Y=[Y x];
if(rerr<tol)
break
end
end
% Print the results
if(A(i,i)==0)
disp(' division by zero')
elseif (k==itmax+1)
disp(' No convergence')
else
fprintf('\n');
disp(' The solution vectors are:')
fprintf('\n');
disp('iter # 0 1 2 3 4 ...')
fprintf('\n');
for i=1:n
fprintf('x%1.0f = ',i)
fprintf('%10.6f',Y(i,[1:k+1]))
fprintf('\n');
end
fprintf('\n');
disp(['The method converges after ',num2str(k),' iterations
to']);
x
end

Ejemplo
A=[3 1 -1;1 2 1; 2 -3 1]
A =
1 1 -1
1 2 1
2 -3 1
>> B=[40;30;50]
B =
40
30
50
>> X=inv(A)*B
X =
31.5385
2.3077
-6.1538
format rational
format
>> x0=[0;0;0]
x0 =
0
0
0
>> jacobi(A,B,x0,0.000001,50)
The solution vectors are:

iter # 0 1 2 3 4 ...
x1 = 0.000000 13.333333 11.666667 12.333333 13.777778 13.322222
12.477778 12.799259 13.290741 13.069704 12.785531 12.934536
13.097672 12.998796 12.905882 12.970645 13.023098 12.981143
12.951830 12.978751 12.994940 12.977810 12.968994 12.979812
12.984531 12.977746 12.975275 12.979503 12.980760 12.978140
12.977528 12.979141 12.979421 12.978433 12.978319 12.978921
12.978957 12.978591 12.978590 12.978810 12.978799 12.978667
12.978681 12.978760 12.978747 12.978700 12.978710 12.978738
12.978730 12.978714 12.978720
x2 = 0.000000 15.000000 16.666667 14.000000 13.500000 15.155556
15.366667 14.356667 14.285185 14.898222 14.907444 14.537240
14.552141 14.774571 14.753290 14.620329 14.640512 14.719579
14.703037 14.656270 14.668830 14.696339 14.687244 14.671157
14.677531 14.686880 14.682520 14.677123 14.680051 14.683144
14.681207 14.679449 14.680714 14.681704 14.680887 14.680335
14.680858 14.681162 14.680830 14.680665 14.680874 14.680962
14.680831 14.680785 14.680867 14.680890 14.680839 14.680828
14.680859 14.680864 14.680845
x3 = 0.000000 10.000000 13.666667 15.333333 13.466667 12.588889
13.764444 14.228889 13.494296 13.254815 13.711052 13.830254
13.548529 13.492216 13.665224 13.689621 13.583939 13.575068
13.639290 13.641091 13.602261 13.603322 13.626680 13.624749
13.610770 13.612707 13.621030 13.619402 13.614472 13.615727
13.618630 13.617713 13.616013 13.616660 13.617649 13.617205
13.616632 13.616932 13.617261 13.617062 13.616875 13.617005
13.617111 13.617026 13.616967 13.617021 13.617054 13.617019
13.617002 13.617023 13.617033

The method converges after 50 iterations to


x=
610/47
690/47
640/47
>> X=inv(A)*B
X=
610/47
690/47
640/47
Método de gauss seidel
function seidel(A,b,x0,tol,itmax)
% Solve the system Ax=b using Gauss-Seidel iteration method.
n=length(b);
x=zeros(n,1);
fprintf('\n');
disp(' The augmented matrix is =')
Augm=[A b]
Y=zeros(n,1);
Y=x0;
for k=1:itmax+1
for i=1:n
S=0;
for j=1:i-1
S=S+A(i,j)*x(j);
end
for j=i+1:n
S=S+A(i,j)*x0(j);
end
if(A(i,i)==0)
break
end
x(i)=(-S+b(i))/A(i,i);
end
err=abs(norm(x-x0));
rerr=err/(norm(x)+eps);
x0=x;
Y=[Y x];
if(rerr<tol)
break
end
end
% Print the results
if(A(i,i)==0)
disp(' division by zero')
elseif (k==itmax+1)
disp(' No convergence')
else
fprintf('\n');
disp(' The solution vectors are:')
fprintf('\n');
disp('iter # 0 1 2 3 4 ...')
fprintf('\n');
for i=1:n
fprintf('x%1.0f = ',i)
fprintf('%10.6f',Y(i,[1:k+1]))
fprintf('\n');
end
fprintf('\n');
disp(['The method converges after ',num2str(k),' iterations
to']);
x
end

A=[8 1 -1 2 2;1 9 3 -1 1;1 2 20 10 2;2 -3 6 15 -1;1 1 4 -3 10]


A=
8 1 -1 2 2
1 9 3 -1 1
1 2 20 10 2
2 -3 6 15 -1
1 1 4 -3 10
>> B=[25;22;30;15;20]
B=
25
22
30
15
20
>> x0=[0;0;0;0;0]
x0 =
0
0
0
0
0
>> X=inv(A)*B
X=
2.2926
1.9349
0.5382
0.9763
1.6549
>> jacobi(A,B,x0,0.000001,100)

The augmented matrix is =


Augm =
8 1 -1 2 2 25
1 9 3 -1 1 22
1 2 20 10 2 30
2 -3 6 15 -1 15
1 1 4 -3 10 20
The solution vectors are:

iter # 0 1 2 3 4 ...

x1 = 0.000000 3.125000 2.256944 2.551997 2.337472 2.385436


2.326541 2.329714 2.311304 2.308520 2.301969 2.299713 2.297119
2.295851 2.294747 2.294107 2.293616 2.293307 2.293084 2.292938
2.292835 2.292766 2.292719 2.292687 2.292665 2.292650 2.292639
2.292632 2.292628 2.292624 2.292622 2.292621
x2 = 0.000000 2.444444 1.486111 2.000849 1.805419 1.928915
1.891804 1.924019 1.918672 1.928020 1.928213 1.931290 1.931987
1.933131 1.933593 1.934060 1.934306 1.934509 1.934631 1.934721
1.934780 1.934821 1.934849 1.934868 1.934881 1.934890 1.934896
1.934900 1.934903 1.934905 1.934906 1.934907
x3 = 0.000000 1.500000 0.399306 0.821458 0.551163 0.633007
0.561934 0.573927 0.553283 0.552934 0.546150 0.544639 0.542123
0.541114 0.540088 0.539548 0.539104 0.538837 0.538637 0.538509
0.538418 0.538358 0.538316 0.538287 0.538268 0.538255 0.538246
0.538239 0.538235 0.538232 0.538230 0.538229
x4 = 0.000000 1.000000 0.605556 0.912778 0.841163 0.928287
0.922362 0.949638 0.953278 0.962910 0.966073 0.969857 0.971663
0.973266 0.974183 0.974893 0.975337 0.975660 0.975871 0.976019
0.976118 0.976186 0.976233 0.976265 0.976286 0.976301 0.976311
0.976318 0.976323 0.976326 0.976328 0.976330
x5 = 0.000000 2.000000 1.143056 1.647639 1.489965 1.617595
1.593848 1.630101 1.629948 1.641673 1.644046 1.648344 1.650001
1.651739 1.652636 1.653386 1.653832 1.654168 1.654382 1.654535
1.654636 1.654707 1.654754 1.654787 1.654809 1.654824 1.654835
1.654842 1.654846 1.654850 1.654852 1.654853
The method converges after 31 iterations to
x=
2.2926
1.9349
0.5382
0.9763
1.6549
>> seidel(A,B,x0,0.00001,12)

The augmented matrix is =


Augm =
8 1 -1 2 2 25
1 9 3 -1 1 22
1 2 20 10 2 30
2 -3 6 15 -1 15
1 1 4 -3 10 20
No convergence
>> seidel(A,B,x0,0.00001,100)

The augmented matrix is =


Augm =
8 1 -1 2 2 25
1 9 3 -1 1 22
1 2 20 10 2 30
2 -3 6 15 -1 15
1 1 4 -3 10 20

The solution vectors are:

iter # 0 1 2 3 4 ...

x1 = 0.000000 3.125000 2.570080 2.454247 2.372675 2.332035


2.312052 2.302201 2.297343 2.294948 2.293766 2.293184 2.292897
2.292755 2.292685 2.292651 2.292634
x2 = 0.000000 2.097222 1.709787 1.822715 1.880186 1.907959
1.921615 1.928354 1.931676 1.933315 1.934123 1.934521 1.934718
1.934815 1.934862 1.934886 1.934898
x3 = 0.000000 1.134028 0.807042 0.669563 0.602924 0.570146
0.553965 0.545988 0.542053 0.540113 0.539157 0.538685 0.538452
0.538338 0.538281 0.538253 0.538240
x4 = 0.000000 0.549167 0.755724 0.867879 0.922834 0.949955
0.963325 0.969918 0.973170 0.974773 0.975564 0.975954 0.976146
0.976241 0.976287 0.976310 0.976322
x5 = 0.000000 1.188917 1.475914 1.564843 1.610394 1.632929
1.644045 1.649525 1.652228 1.653560 1.654217 1.654541 1.654701
1.654780 1.654819 1.654838 1.654848

The method converges after 16 iterations to


x=
2.2926
1.9349
0.5382
0.9763
1.6548
LAGRANGE INTERPOLACION
function lagrange(x,y,a)
% Coefficients of the Lagrange interpolating polynomial.
n=length(x);
p=0;
for k=1:n
b(k)=1;
d(k)=1;
for j=1:n
if j~= k
b(k)=b(k)*(x(k)-x(j));
d(k)=d(k)*(a-x(j));
end
end
c(k)=y(k)/b(k);
p=p+c(k)*d(k);
end
c
fprintf('\n p(a)= %10.6f',p)
fprintf('\n')

EJEMPLO
X=[-1; 0; 3 ]
X=
-1
0
3
>> Y=[8;-2;4]
Y=
8
-2
4
>> lagrange(x,y,2)
Undefined function or variable 'x'.
Did you mean:
>> lagrange(X,Y,2)
c=
2.0000 0.6667 0.3333

p(a)= -4.000000
>> FORMAT RATIONAL
Undefined function or variable 'FORMAT'.
Did you mean:
>> format RATIONAL
>> lagrange(X,Y,2)
c=
2 2/3 1/3

p(a)= -4.000000
>> lagrange(X,Y,1)
c=
2 2/3 1/3

p(a)= -6.000000
ECUACIONES DIFERENCIALES ORDINARIAS
METODO DE RUNGEN KUTTA DE ORDEN 2 Y 4
function rk2_4(f,a,b,y0,n,order)
% solve the initial-value problem y'=f(t,y), y(a)=y0
% using the Runge-Kutta methods.
fprintf('\n')
disp([' Runge-Kutta method of order = ',num2str(order)])
h=(b-a)/n;
y=y0;
if (order==2)
disp('_____________________________________________')
disp(' t k1 k2 y Exact error ')
disp('_____________________________________________')
fprintf('\n')
fprintf('%6.2f ---- ---- %12.6f %12.6f %4.2f\n',a,y,y,0)
for i=1:n
t=a+(i-1)*h;
k1=feval(f,t,y);
k2=feval(f,t+h,y+h*k1);
y=y+h*(k1+k2)/2;
t=t+h;
% Enter the exact solution if known as g=g(t) otherwise set
g='n'.
g='n';
if (g~='n')
err=abs(g-y);
fprintf('%6.2f %12.6f %12.6f %12.6f %12.6f
%8.2e\n',t,k1,k2,y,g,err)
else
fprintf('%6.2f %12.6f %12.6f %12.6f\n',t,k1,k2,y)
end
end
end
if (order==4)
disp('_______________________________________________')
disp(' t k1 k2 k3 k4 y Exact error ')
disp('_______________________________________________')
fprintf('\n')
fprintf('%6.2f ---- ---- ---- ---- %12.6f
%12.6f%4.2f\n',a,y,y,0)
for i=1:n
t=a+(i-1)*h;
k1=feval(f,t,y);
k2=feval(f,t+h/2,y+h*k1/2);
k3=feval(f,t+h/2,y+h*k2/2);
k4=feval(f,t+h,y+h*k3);
y=y+h*(k1+2*k2+2*k3+k4)/6;
t=t+h;
% Enter the exact solution if known as g=g(t) otherwise set
g='n'.
g=exp(-t)+2*t-2;
if (g~='n')
err=abs(g-y);
fprintf('%6.2f %12.6f %12.6f %12.6f %12.6f %12.6f
%12.6f%8.2e\n', t, k1, k2, k3, k4, y, g, err)
else
fprintf('%6.2f %12.6f %12.6f %12.6f %12.6f
%12.6f\n',t,k1,k2,k3, k4, y)
end
end
end

ejemplo
function f=f1(t,y)
f=2*t-y;

rk2_4('f1',0,1,-1,10,4)

function f=f3(t,y)
f=-4*t^3*y;
rk2_4('f3',1,2,1,10,4)

regla del trapecio


function trapez(f,a,b,n)
% Compute the integral of f(x) from a to b using the
trapezoid rule
h=(b-a)/n;
disp('_______________________________________________')
disp([' i xi f(xi) h=',num2str(h) ])
disp('_______________________________________________')
S=feval(f,a);
fprintf(' %2.0f %12.4f %14.6f\n',0,a,S);
for i=1:n-1
x=a+h*i;
g=feval(f,x);
S=S+2*g;
fprintf(' %2.0f %12.4f %14.6f\n',i,x,g);
end
S=S+feval(f,b);
fprintf(' %2.0f %12.4f %14.6f\n',n,b,feval(f,b));
INT=h*S/2;
fprintf('\n The intergral of f(x) is =%16.8f\n',INT);

ejemplo
function f=f1(x)
f=7+14*x^6;
trapez('f1',0,1,5)

Método de Simpson

function simpson(f,a,b,n)
% Compute the integral of a f from a to b using Simpson’s
% composite rule. n must be even.

h=(b-a)/n;
disp('______________________________________________')
disp([' i xi f(xi) h=',num2str(h) ])
disp('______________________________________________')
S=feval(f,a);
fprintf(' %2.0f %12.4f %14.6f\n',0,a,S);
for i=1:n/2
m=2*i-1;
x=a+h*m;
g=feval(f,x);
S=S+4*g;
fprintf(' %2.0f %12.4f %14.6f\n',m,x,g);
m=2*i;
x=a+h*m;
g=feval(f,x);
if(i==n/2)
S=S+g;
else
S=S+2*g;
end;
fprintf(' %2.0f %12.4f %14.6f\n',m,x,g);
end
INT=h*S/3;
fprintf('\n The intergral of f(x) is =%16.8f\n',INT);
ejemplo
function f=f4(x)
f=7+14*x.^6;

simpson('f4',0,1,20)

Método de Simpson

function simpson(f,a,b,n)
% Compute the integral of a f from a to b using Simpson’s
% composite rule. n must be even.

h=(b-a)/n;
disp('______________________________________________')
disp([' i xi f(xi) h=',num2str(h) ])
disp('______________________________________________')
S=feval(f,a);
fprintf(' %2.0f %12.4f %14.6f\n',0,a,S);
for i=1:n/2
m=2*i-1;
x=a+h*m;
g=feval(f,x);
S=S+4*g;
fprintf(' %2.0f %12.4f %14.6f\n',m,x,g);
m=2*i;
x=a+h*m;
g=feval(f,x);
if(i==n/2)
S=S+g;
else
S=S+2*g;
end;
fprintf(' %2.0f %12.4f %14.6f\n',m,x,g);
end
INT=h*S/3;
fprintf('\n The intergral of f(x) is =%16.8f\n',INT);

ejemplo
function f=f4(x)
f=7+14*x.^6;
simpson('f4',0,1,20)

function [solucion v_error]=Biseccion(f,a,b)


% Bisección

% Datos:
% f: es la función
% a,b: los valores extremos del intervalo
% Resultados
% solución: valor de la solución
% i es el número de iteración

tolerancia=10^-6;
i=0; %% Paso 0
error=0;

fa=feval('f',a);

if fa==0,
solucion=a;
return,
end

fb=feval('f',b);

if fb==0,
solucion=b;
return,
end

if fa*fb<=0
while abs(b-a)/2>tolerancia
%%%Empiezo la bisección la bisección
m=(a+b)/2;
fm=feval('f',m);
fprintf('%5d%10.5f%10.5f%10.5f\n',i,a,b,m);
i=i+1; % Ve al paso 1
if fa*fm<=0
b=m;
fb=fm;
else
a=m;
fa=fm;
end
%%%Terminé la bisección
end

solucion=m;
%error porcentual
v_error=((abs(b-a))/solucion)*100;
%error porcentual
else
disp('Cambia tu intervalo de búsqueda');
solucion=NaN;
v_error=NaN;
end

Você também pode gostar