1   % Demo: Graphics Method GM
2   % Test : 8 - 4.5*(x - sin(x)) = 0
 3   clc
 4   disp('Demo: Graphics Method')
 5   disp('======================')
 6   x = -1:0.1:4;
 7   fun = 8 - 4.5*(x - sin(x));
 8   data = [x' fun']
 9   % plotear:
10   plot(x,fun,'LineWidth',2)
11   hold on
12   xlabel('Eje(x)','FontSize', 18)
13   ylabel('Fun(x)', 'FontSize', 18)
14   title('Demo: Graphics Method', 'FontSize', 16)
15   grid on
16   xsol = 2.43
17   plot(xsol, 0,'r*', 'LineWidth', 10)
18
19
 1   %Demo : Solucion Ecuacion de Segundo Grado
 2    % ax^2 + bx + c =0
 3    % Input: a, b,c
 4    % Output: x1, x2
 5    clc
 6    disp('Demo : Equation Solution Second Degree')
 7    disp('=======================================')
 8    a=input('valor de a = ');
 9    b=input('valor de b = ');
10    c=input('valor de c = ');
11    % main:
12    d=b^2-4*a*c; % discriminate
13    if d>0
14    disp('')
15    disp('raices reales y diferentes')
16   x1=(-b+sqrt(d))/(2*a);
17   x2=(-b-sqrt(d))/(2*a);
18
19    elseif d==0
20    disp('')
21    disp('raices reales e iguales')
22    x1=-b/(2*a);
23    x2=-b/(2*a);
24
25    else
26    disp('')
27    disp('raices complejas y conjugadas')
28    x1=(-b+i*sqrt(-d))/(2*a);
29    x2=(-b-i*sqrt(-d))/(2*a);
30
31    end
32    disp('')
33    disp('valores de la ecuación de segundo grado:')
34    x1 , x2
 1   %Demo: Bisection Method
 2   % File : bisection.m
 3   % Author: Numerical Methods
 4   % teste : 8-4.5*(x - sin(x))=0
 5   % Initial interal : a=2, b=3
 6   % Code M-File
 7   clc
 8   disp('Demo : Bisection Method')
 9   disp('=======================')
10   % Input
11   a=2,b=3 %[a,b] intervalo de busqueda inicial
12   imax = 20 % numero maximo de teraciones permitidas
13   tol = 10^(-3) % error maximo permitido
14   fun = @(x) 8-4.5*(x - sin(x))
15   % main
16   fa = fun(a);
17   fb = fun(b);
18   if fa*fb >0
19   disp('No hay solucion en el intervalo [a,b]')
20   else
21   disp('i a b xs f(xs) tol ')
22   disp('=================================================================')
23   for i = 1:imax
24   xs = (a+b)/2;
25   toli= (b-a)/2;
26   fxs = fun(xs);
27   fprintf('%3i %11.6f %11.6f %11.6f %11.6f %11.6f\n',i,a,b,xs,fxs,toli)
28   if fxs == 0
29   fprintf('solucion exacta x = %11.6f encontrada',xs)
30   break
31   endif
32
33   if toli < tol
34   break
35   endif
36
37   if i == imax
38   fprintf('Solucion no fue obtenida en % i iteraciones', imax)
39   break
40   endif
41
42   if fun(a)*fxs < 0
43   b = xs;
44   else
45   a = xs;
46   endif
47   end
48
49   endif
50   fprintf('\n\n')
51   fprintf('solucion xs = %11.6f\n', xs)
52