Created
April 3, 2019 08:20
-
-
Save ecomaikgolf/9879a6d94afb22422924981f55116407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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 | |
integral = int(f,x,desde,hasta); | |
Error = double(abs(((abs(integral - Rn)/integral)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment