Skip to content

Instantly share code, notes, and snippets.

@ecomaikgolf
Last active April 2, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecomaikgolf/c89fb2d8db1acd9830ab0704b8533e30 to your computer and use it in GitHub Desktop.
Save ecomaikgolf/c89fb2d8db1acd9830ab0704b8533e30 to your computer and use it in GitHub Desktop.
Sumas de riemann en matlab
%Reutiliza el script de la Práctica #1 para calcular L n y R n de
% f (x) = x^2 − 2x + 3 en [−2, 3] con 8, 16, 32 y 48 rectángulos respectivamente
syms x
f = @(x) -x^2 -2*x + 3
desde = -2
hasta = 3
cant = 40
h = (hasta - desde)/cant
xi = linspace(desde, hasta, cant+2)
for i = 1 : cant+1
yi(i) = f(xi(i));
end
Rn = h* sum(double(yi(1 : cant)))
Ln = h* sum(double(yi(2 : cant+1)))
ezplot(f,[desde hasta]);
hold on;
plot([xi(1) xi(end)],[0 0],'b')
for i = 1 : cant+1
if(f(xi(i)) >= 0)
%Rectangulo inferior
plot([xi(i) xi(i)],[0 double(f(fminbnd(f,xi(i),xi(i+1))))],'g')
plot([xi(i) xi(i+1)],[double(f(fminbnd(f,xi(i),xi(i+1)))) double(f(fminbnd(f,xi(i),xi(i+1))))],'g')
plot([xi(i+1) xi(i+1)],[double(f(fminbnd(f,xi(i),xi(i+1)))) 0],'g')
g = @(x) -f(x);
%Rectangulo superior
plot([xi(i) xi(i)],[0 double(f(fminbnd(g,xi(i),xi(i+1))))],'r')
plot([xi(i) xi(i+1)],[double(f(fminbnd(g,xi(i),xi(i+1)))) double(f(fminbnd(g,xi(i),xi(i+1))))],'r')
plot([xi(i+1) xi(i+1)],[double(f(fminbnd(g,xi(i),xi(i+1)))) 0],'r')
else
%Rectangulo inferior
plot([xi(i) xi(i)],[0 double(f(fminbnd(f,xi(i),xi(i+1))))],'r')
plot([xi(i) xi(i+1)],[double(f(fminbnd(f,xi(i),xi(i+1)))) double(f(fminbnd(f,xi(i),xi(i+1))))],'r')
plot([xi(i+1) xi(i+1)],[double(f(fminbnd(f,xi(i),xi(i+1)))) 0],'r')
g = @(x) -f(x);
%Rectangulo superior
plot([xi(i) xi(i)],[0 double(f(fminbnd(g,xi(i),xi(i+1))))],'g')
plot([xi(i) xi(i+1)],[double(f(fminbnd(g,xi(i),xi(i+1)))) double(f(fminbnd(g,xi(i),xi(i+1))))],'g')
plot([xi(i+1) xi(i+1)],[double(f(fminbnd(g,xi(i),xi(i+1)))) 0],'g')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment