Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

METODO JACOBI

%% Jacobi Method
%% Solution of x in Ax=b using Jacobi Method
% * Initailize 'A' 'b' & intial guess 'x'
%%
A=[12 7 3; 1 5 1; 2 7 -11];
b=[2 -5 6]';
x=[0 0 0]';
n=size(x,1);
normVal=Inf;
%%
% * Tolerence for method
tol=5e-2; itr=0;
%% Algorithm: Jacobi Method
%%
while normVal>tol
xold=x;
for i=1:n
sigma=0;
for j=1:n
if j~=i
sigma=sigma+A(i,j)*xold(j);
end
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
itr=itr+1;
normVal=norm(xold-x);
%normVal=abs(xold-x);
fprintf('\n%f %f %f %d %f', x, itr, normVal);
end
%%
fprintf('\nSolution of the system is : \n%f\n%f\n%f \nin %d iterations',x,itr);
METODO GAUSS SEIDEL
%% Gauss Seidel Method
%% Solution of x in Ax=b using Gauss Seidel Method
% * Initailize 'A' 'b' & intial guess 'x'
%%
A=[12 7 3; 1 5 1; 2 7 -11];
b=[2 -5 6]';
x=[0 0 0]';
n=size(x,1);
normVal=Inf;
%%
% * Tolerence for method
tol=5e-2; itr=0;
%% Algorithm: Gauss Seidel Method
%%
while normVal>tol
x_old=x;
for i=1:n
sigma=0;
for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end
for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
itr=itr+1;
normVal=norm(x_old-x);
fprintf('\n%f %f %f %d %f', x, itr, normVal);
end
%%
fprintf('\n\nSolution of the system is:\n \n%f\n%f\n%f\n%f in %d
iterations',x,itr);
PUNTO FIJO
close all
clear all
clc
syms x;
fx = @(x) exp(x/4);
%fx = inline(f);
x0=0;
error = 100;
i=0;
fprintf('\ti \txi \terror \n');
while (abs(error)>0.001)
i = i+1;
xi= feval(fx,x0);
error=((xi-x0)/xi)*100;

fprintf('\t%d \t%f \t%f \n',i,xi,error)


x0=xi;
end
BISECCION
f = @(x) (exp(x/4)-x);

xmin = -5;
xmax = 5;
xs = linspace(xmin, xmax,200);
ys = f(xs);
plot(xs, ys);
title('Grafica de f')
xlabel('x')
ylabel('y')

hold on;
plot(xs,ys)

xleft = 1;
xright = 2;
xtol = 1e-2;
plot(xleft, 0, 'x')
plot(xright, 0, 'x')

j = 1;
sleft = sign(f(xleft));
sright = sign(f(xright));
while xright - xleft > xtol
xmed = (xleft + xright)/2;
smed = sign(f(xmed));
if smed==sleft
xleft = xmed;
else
xright = xmed;
end
plot(xmed, 0, 'o');
error = abs(xright - xleft);
fprintf('Iteración: %d, Punto medio: %.6f, Error: %.6f\n', j,xmed,error);
j = j + 1;
end
legends = cell(1, j+2);
legends{1} = 'f';
legends{2} = 'left end';
legends{3} = 'right end';
for k=1:j-1
legends{k+3} = sprintf('step %d', k);
end

legend(legends)
hold off;

You might also like