Você está na página 1de 13

%METODO DE BISECCION...

clear all
y= inline('-20 +10.*x +2*x.^2 + x.^3', 'x');
x1 = 1; x2 = 2;
f1= y(x1);
f2= y(x2);
errorx = 100;
n= 1;
while errorx > 1e-7 && n < 501
x3 = (x2 + x1)/2;
f3= y(x3);
n = n + 1;
if f3 ==0
errorx = 0
break
end
if sign(f3) ~= sign(f2)
x1= x3;
f1= f3;
elseif sign(f3)~= sign(f1)
x2= x3;
f2= f3;
end
errorx = abs(x2 - x1);
end
niter = n -1
format long e
x3
f3
errorx
errorf = abs(f3)

%METODO DE INTERPOLACIN...
clear all
y= inline('-20 +10.*x +2*x.^2 + x.^3', 'x');
x1 = 1; x2 = 2;
f1= y(x1);
f2= y(x2);
errorx = 100;
n= 1;
while errorx > 1e-7 && n < 501
x3 = x2 - (x2 - x1)*f2/(f2 - f1);
f3= y(x3);
if n > 1
errorx = abs(x3 - xbuf);
end
n = n + 1;
if f3 ==0
errorx = 0
break

end
if sign(f3) ~= sign(f2)
x1= x3;
f1= f3;
elseif sign(f3)~= sign(f1)
x2= x3;
f2= f3;
end
xbuf = x3;
end
niter = n -1
format long e
x3
f3
errorx
errorf = abs(f3)
format short

%metodo de la secante...
clear all
y= inline('-20 +10.*x +2*x.^2 + x.^3', 'x');
x1 = 1; x2 = 2;
f1= y(x1);
f2= y(x2);
errorx = 100;
n= 1;
while errorx > 1e-7 && n < 501
x3 = x2 - (x2 - x1)*f2/(f2 - f1);
f3= y(x3);
if n > 1
errorx = abs(x3 - xbuf);
end
n = n + 1;
if f3 ==0
errorx = 0
break
end
x1= x2;
f1= f2;
x2= x3;
f2= f3;
xbuf = x3;
end
niter = n -1
format long e
x3
f3

errorx
errorf = abs(f3)
format short

%metodo de newton...
clear all
y = inline('-20 +10.*x +2*x.^2 + x.^3', 'x');
yder = inline('10 + 4.*x +3*x.^2', 'x');
x(1) = 2;
f(1) = y(x(1));
fder(1) = yder(x(1));
n = 2;
errorx = 100;
while errorx > 1e-7 && n < 501
x(n) = x(n-1) - f(n-1)/fder(n-1);
f(n) = y(x(n));
fder(n) = yder(x(n));
errorx = abs(x(n) - x(n-1));
n = n + 1;
if f(n-1) == 0
errorx = 0;
break
end
end
Niter = n - 1
format long e
x(Niter)
errorx
errorf = abs(f(Niter));
format short

function y = mifuncion(x, z)
y = x.^2 + z.^2;
return

clear all
x = 7;
y = 5;
z = mifuncion(x, y);

%graficar
clear all
f= inline( ' 7*sin(x) - exp(-x)-1' , 'x' )
x = -5: 0.1:5;
y = f(x);
figure('name','y vs x')
plot (x,y)
grid on
xlabel('x'), ylabel('y')
%graficar
clear all
f= inline( ' 7*sin(x) - exp(-x)-1' , 'x' )
x = -5: 0.1:5;
y = f(x);
figure('name','y vs x')
plot (x,y)
grid on
xlabel('x'), ylabel('y')

%METODO DE interpolacin lineal...


clear all
y= inline('-20 +10.*x +2*x.^2 + x.^3', 'x');
x1 = 1; x2 = 2;
f1= y(x1);
f2= y(x2);
errorx = 100;
n= 1;
while errorx > 1e-7 && n < 501
x3 = x2-(x2 + x1)*f2/(f2-f1);
f3= y(x3);
if n > 1
errorx = abs(x3 xbuf);
end
n=n+1
if f3 ==0
errorx = 0
break
end
if sign(f3) ~= sign(f2)
x1= x3;
f1= f3;
elseif sign(f3)~= sign(f1)

x2= x3;
f2= f3;

end
xbuf = x3;

end
niter = n -1
format long e
x3
f3
errorx
errorf = abs(f3)
format short

grafica exp tan discontinuo


clear all
x = 0:0.1:10;
y1 = inline('exp(-x)', 'x');
y2 = inline ('tan(x)', 'x');
f1 = y1 (x);
figure
plot(x, f1, 'b-')
hold on
f2 = y2 (x);
plot(x, f2, 'r-')
grid on
hold off

grafico tan exp continuo


clear all
x = 0:0.1:10;
y =inline('tan(x) - exp(-x)','x');
f = y(x);
figure
plot(x, f, 'k-')
grid on

uso de fzero

clear all
y = inline('tan(x)-exp(-x)', 'x');
[x, fval, exitflag] = fzero(y, [1 2]);

help fzero
FZERO Single-variable nonlinear zero finding.
X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0,
if X0 is a scalar. It first finds an interval containing X0 where the
function values of the interval endpoints differ in sign, then searches
that interval for a zero. FUN is a function handle. FUN accepts real
scalar input X and returns a real scalar function value F, evaluated
at X. The value X returned by FZERO is near a point where FUN changes
sign (if FUN is continuous), or NaN if the search fails.

X = FZERO(FUN,X0), where X0 is a vector of length 2, assumes X0 is a


finite interval where the sign of FUN(X0(1)) differs from the sign of
FUN(X0(2)). An error occurs if this is not true. Calling FZERO with a
finite interval guarantees FZERO will return a value near a point where
FUN changes sign.

X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting


guess. FZERO looks for an interval containing a sign change for FUN and
containing X0. If no such interval is found, NaN is returned.
In this case, the search terminates when the search interval
is expanded until an Inf, NaN, or complex value is found. Note: if
the option FunValCheck is 'on', then an error will occur if an NaN or
complex value is found.

X = FZERO(FUN,X0,OPTIONS) solves the equation with the default


optimization

parameters replaced by values in the structure OPTIONS, an argument


created with the OPTIMSET function. See OPTIMSET for details. Used
options are Display, TolX, FunValCheck, OutputFcn, and PlotFcns.

X = FZERO(PROBLEM) finds the zero of a function defined in PROBLEM.


PROBLEM is a structure with the function FUN in PROBLEM.objective,
the start point in PROBLEM.x0, the options structure in PROBLEM.options,
and solver name 'fzero' in PROBLEM.solver. The structure PROBLEM must
have
all the fields.

[X,FVAL]= FZERO(FUN,...) returns the value of the function described


in FUN, at X.

[X,FVAL,EXITFLAG] = FZERO(...) returns an EXITFLAG that describes the


exit condition of FZERO. Possible values of EXITFLAG and the corresponding
exit conditions are

1 FZERO found a zero X.


-1 Algorithm terminated by output function.
-3 NaN or Inf function value encountered during search for an interval
containing a sign change.
-4 Complex function value encountered during search for an interval
containing a sign change.
-5 FZERO may have converged to a singular point.
-6 FZERO can not detect a change in sign of the function.

[X,FVAL,EXITFLAG,OUTPUT] = FZERO(...) returns a structure OUTPUT


with the number of function evaluations in OUTPUT.funcCount, the
algorithm name in OUTPUT.algorithm, the number of iterations to
find an interval (if needed) in OUTPUT.intervaliterations, the
number of zero-finding iterations in OUTPUT.iterations, and the
exit message in OUTPUT.message.

Examples
FUN can be specified using @:
X = fzero(@sin,3)
returns pi.
X = fzero(@sin,3,optimset('Display','iter'))
returns pi, uses the default tolerance and displays iteration information.

FUN can also be an anonymous function:


X = fzero(@(x) sin(3*x),2)

If FUN is parameterized, you can use anonymous functions to capture the


problem-dependent parameters. Suppose you want to solve the equation
given
in the function myfun, which is parameterized by its second argument c.
Here
myfun is an M-file function such as

function f = myfun(x,c)
f = cos(c*x);

To solve the equation for a specific value of c, first assign the value to c.
Then create a one-argument anonymous function that captures that value of
c
and calls myfun with two arguments. Finally, pass this anonymous function
to
FZERO:

c = 2; % define parameter first


x = fzero(@(x) myfun(x,c),0.1)

Limitations
X = fzero(@(x) abs(x)+1, 1)
returns NaN since this function does not change sign anywhere on the
real axis (and does not have a zero as well).
X = fzero(@tan,2)
returns X near 1.5708 because the discontinuity of this function near the
point X gives the appearance (numerically) that the function changes sign
at X.

See also roots, fminbnd, function_handle.

Reference page in Help browser


doc fzero

function f = fa_(x, y)
f = y + x.^2 - x - 0.5;
return
function f = fb_(x, y)

f = x.^2 - 5*x.*y -y;


return

curva_nivel
clear all
x1 = -2:0.1:2;
y1 = -2:0.1:2;
[x, y] = meshgrid(x1, y1);
f1 = fa_(x, y);
f2 = fb_(x, y);
contour(x1, y1, f1, [0.0 0.0], 'b-')
hold on
contour(x1, y1, f2, [0.0 0.0], 'k-')
xlabel ('x'), ylabel('y')
grid on
hold off

polinomio de 3er grado


clear all
y = inline('5*x.^3 - 3*x.^2 + 1.5*x + 0.5', 'x');
x = -2:0.01:2;
f = y(x);
figure
plot(x, f)
grid on

MARTES 17/07/12 ANALISIS NUMERICO


SISTEMAS DE ECUACIONES LINEALES
EJERCICIO 1
>> A= [1 2 3; 4 5 6; 7 8 9] (ventana de comandos)

A=

>> b= [1 -3 7]'

b=

1
-3
7

>> x= A\b
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.541976e-018.

x=

1.0e+017 *

-0.6305
1.2610
-0.6305

>> A*x

ans =

-64
0

>> x= inv(A)*b
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.541976e-018.

x=

1.0e+017 *

-0.6305
1.2610
-0.6305

METODOS ITERATIVOS (cuaderno)


EJERICICO 2 METODO DE JACOBI editor
%Metodo de Jacobi
clear all
A =[7 -1 3; 2 -8 1; 1 -1 3];
b = [-3; 2; 1];
x0 = [0; 0; 0];
lim = 501;
ai = diag(A);
aii = diag(ai);
errorx = 100;
n = 1;
disp(' Num. Iter. valores x errorx')
while errorx > 0.0001 && n < lim
x = (b - sum((A - aii)*x0, 2))./ai;
errorx = 100*norm(x - x0)/norm(x);
bu = [n x' errorx];
disp(bu)
x0 = x;
n = n + 1;
end
Niter = n - 1

format long e
errorx
x
format short
%Metodo de gauss-seidel
clear all
A =[7 -1 3; 2 -8 1; 1 -1 3];
b = [-3; 2; 1];
x = [0; 0; 0];
ta = size(A);
ta = ta(1, 1);
lim = 501;
ai = diag(A);
aii = diag(ai);
errorx = 100;
n = 1;
disp(' Num. Iter. valores x errorx')
while errorx > 0.0001 && n < lim
x0 =x;
for m = 1:ta
x(m) = (b(m) - sum((A(m, :) - aii(m, :))*x, 2))./ai(m);
end
errorx = 100*norm(x - x0)/norm(x);
bu = [n x' errorx];
disp(bu)
n = n + 1;
end
Niter = n - 1
format long e
errorx
x
format short