Está en la página 1de 9

SALTO CAIDE LIBRE CON METODO NEWTON – RAPSHON

%% Implementacion del metodo de Newton

clc, clear all;

func = @(m) sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;

dfunc=@(m) 1/2*sqrt(9.81/(m*0.25))*tanh((9.81*0.25/m)^(1/2)*4)-
9.81/(2*m)*sech(sqrt(9.81*0.25/m)*4)^2;

%% Iniciando variables

xr = 40; %Aproximacion Inicial de la Raiz

es = 0.001; %Tolerancia

maxit = 50; %Maximo Numero de iteracciones

ea = 100;

iter = 0;

%% Inicio del Bucle

disp(' k xi ea(%)')

while (1)

xrold = xr;

xr = xr-func(xr)/dfunc(xr);

fprintf('%3i%11.2f\t%11.6f\n',iter,xr,ea)

iter = iter + 1;

if xr ~= 0

ea = abs((xr-xrold)/xr)*100;

end

if ea <= es || iter >= maxit

break;

end

end

root = xr;
METODO DE PUNTO FIJO PARA F(X) = X – 2-X = 0
%% Implementacion del metodo de punto fijo

clc, clear all;

g = @(x)(2.^(-x));

x=[0:0.5:2];

g1=g(x);

plot(x,g1)

f=@(x)x;

f1=f(x);

hold on

plot(x,f1)

grid on

%% Iniciando variables

xr = 1; %Aproximacion Inicial de la Raiz

es = 0.00001; %Tolerancia

maxit = 50; %Maximo Numero de iteracciones

iter = 0;

ea = 100;

%% Inicio del Bucle

disp(' k xi ea(%)')

while (1)

xrold = xr;

xr = g(xrold);

fprintf('%3i%11.4f\t%11.6f\n',iter,xr,ea)

iter = iter + 1;

if xr ~= 0

ea = abs((xr-xrold)/xr) * 100;

end

if ea <= es || iter >= maxit

break;

end

end

root = xr

g(root)-xr
RAICES EN [3,6] PARA COS(10X) + SEN (3X) = 0 ( FALSA POSICION O BISECCION)

%% Definiendo la funcion

clc, clear all;

func = @(x) sin(10*x)+cos(3*x);

x=[3:0.01:6];

f1=func(x);

plot(x,f1)

grid on;

%%

xmin = 3;

xmax = 6;

ns = 100; % number of subintervals

%% Busqueda Incremental

x = linspace(xmin,xmax,ns);

f = func(x);

% xb(k,1) is the lower bound of the kth sign change

% xb(k,2) is the upper bound of the kth sign change

nb = 0; xb = []; %xb es vacion almenos que se detecte un cambio de signo

for k = 1:length(x)-1

if sign(f(k)) ~= sign(f(k+1)) %check for sign change

nb = nb + 1;

xb(nb,1) = x(k);

xb(nb,2) = x(k+1);

end

end

if isempty(xb) %display that no brackets were found

disp('no brackets found')

disp('check interval or increase ns')

else

disp('number of brackets:') %display number of brackets

disp(nb)

[m,n]=size(xb);

disp(' k xl xu')

for i=1:m

fprintf('%3i%11.2f\t%11.6f\n',i, xb(i,1),xb(i,2));

end

end
GRAFICANDO EL SALTO DE CAIDA LIBRE

%% Definiendo constantes fisica

cd = 0.25;

g = 9.81;

v = 36;

t = 4;

%% Graficando

mp = linspace(50,200);

fp = sqrt(g*mp/cd).*tanh(sqrt(g*cd./mp)*t)-v;

plot(mp,fp),grid

ABRIENDO FUNCION

function M = fm(m)

M=sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;

End
INSEARCH

function xb = incsearch(func,xmin,xmax,ns)

% incsearch: incremental search root locator

% xb = incsearch(func,xmin,xmax,ns):

% finds brackets of x that contain sign changes

% of a function on an interval

% input:

% func = name of function

% xmin, xmax = endpoints of interval

% ns = number of subintervals (default = 50)

% output:

% xb(k,1) is the lower bound of the kth sign change

% xb(k,2) is the upper bound of the kth sign change

% If no brackets found, xb = [].

% Incremental search

x = linspace(xmin,xmax,ns);

f = func(x);

nb = 0; xb = []; %xb is null unless sign change detected

for k = 1:length(x)-1

if sign(f(k)) ~= sign(f(k+1)) %check for sign change

nb = nb + 1;

xb(nb,1) = x(k);

xb(nb,2) = x(k+1);

end

end

if isempty(xb) %display that no brackets were found

disp('no brackets found')

disp('check interval or increase ns')

else

disp('number of brackets:') %display number of brackets

disp(nb)

end
METODO FALSO POSICION

function [r, k] = RegulaFalsi(f, a, b, kmax, tol)

% RegulaFalsi uses the regula falsi method to approximate a root of f(x) = 0

% in the interval [a,b].

% [r, k] = RegulaFalsi(f, a, b, kmax, tol), where

% f is an anonymous function representing f(x),

% a and b are the limits of interval [a,b],

% kmax is the maximum number of iterations (default 20),

% tol is the scalar tolerance for convergence (default 1e-4),

% r is the approximate root of f(x) = 0,

% k is the number of iterations needed for convergence.

if nargin < 5 || isempty(tol), tol = 1e-4; end

if nargin < 4 || isempty(kmax), kmax = 20; end

c = zeros(1,kmax); % Pre-allocate

if f(a)*f(b) > 0

r = 'failure';

return;

end

disp(' k a b')

for k = 1:kmax,

c(k) = (a*f(b)-b*f(a))/(f(b)-f(a)); % Find the x-intercept

if f(c(k)) == 0 % Stop if a root has been found

return;

end

fprintf('%2i %11.6f%11.6f\n',k,a,b)

if f(b)*f(c(k)) > 0 % Check sign changes

b = c(k); % Adjust the endpoint of interval

else a = c(k);

end

c(k+1) = (a*f(b)-b*f(a))/(f(b)-f(a)); % Find the next x-intercept

if abs((c(k+1)-c(k))*100/c(k+1)) < tol, % Stop if tolerance is met

r = c(k+1);

return

end

end
METODO DE LA BISECTRIZ PARA SALTADOR

%% Implementacion del Metodo de la Biseccion

%% Para encotrar la masa del saltador

clc, clear all;

cd = 0.25;

g = 9.81;

v = 36;

t = 4;

% fm = @(mp) sqrt(g*mp/cd).*tanh(sqrt(g*cd./mp)*t)-v;

xu = 150; %Rango superior

xl = 100; %Rango Inferior

es = 0.01; %Tolerancia

maxit = 50; %Maximo Numero de iteracciones

iter = 0;

xr = xl;

ea = 100;

% %% Bucle

% disp(' k xl xu xr=(xu+xl)/2 ea(%)')

% while (1)

% xrold = xr;

% xr = (xl + xu)/2;

% fprintf('%3i %11.2f%11.2f%11.2f%11.6f\n',iter,xl,xu,xr,ea)

% iter = iter + 1;

% if xr ~= 0

% ea = abs((xr-xrold)/xr) * 100;

% end

% test = fm(xl)*fm(xr);

% if test < 0

% xu = xr;

% elseif test > 0

% xl = xr;

% else

% ea = 0;

% end

% if ea <= es || iter >= maxit

% break;
% end

% end

% root = xr;

%fm(root)

%% Usando la funcion Bisect, que implementa el meto de la biseccion.

FUNCION BISECT

function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)

% bisect: root location zeroes

% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):

% uses bisection method to find the root of func

% input:

% func = name of function

% xl, xu = lower and upper guesses

% es = desired relative error (default = 0.0001%)

% maxit = maximum allowable iterations (default = 50)

% p1,p2,... = additional parameters used by func

% output:

% root = real root

% fx = function value at root

% ea = approximate relative error (%)

% iter = number of iterations

if nargin<3

error('at least 3 input arguments required');

end

test = func(xl,varargin{:})*func(xu,varargin{:});

if test>0

error('no sign change');

end

if nargin<4||isempty(es)

es=0.0001;

end

if nargin<5||isempty(maxit)

maxit=50;

end

iter = 0;

xr = xl;

ea = 100;

while (1)
xrold = xr;

xr = (xl + xu)/2;

iter = iter + 1;

if xr ~= 0

ea = abs((xr-xrold)/xr) * 100;

end

test = func(xl,varargin{:})*func(xr,varargin{:});

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea <= es || iter >= maxit

break;

end

end

root = xr; fx = func(xr, varargin{:});

También podría gustarte