Need help with a riemann sum

5 views (last 30 days)
Nicolás Vásquez
Nicolás Vásquez on 4 Dec 2020
Commented: Setsuna Yuuki. on 4 Dec 2020
Hello. I'm quite new to this language and I'm trying to make a code that displays the results of a Riemann sum (L and R areas) going from 1 rectangle to 100 rectangles, and between points 0 (a) and 2 (b). I'm working with the definition; . I'm not sure how to accomplish that, but this is what i've got. It gives me an error. Thank you in advance.
function f=funcion(x)
f=@ (x) x^3 - 3*x^2 + 3*x;
end
clear; clc;
a = 0; b = 2;
n_rec = 1:100;
delta_x = (b-a)/n_rec;
x=zeros(1,n_rec+1);
for k = 1:length(x)
x(k) = a + (k-1)*delta_x;
end
f=funcion(x);
% Cálculo de áreas
area_R = 0;
for i = 1:n_rec
area_R = area_R + f(x(i + 1))*delta_x;
end
area_L = 0;
for j = 1:n_rec
area_L = area_L + f(x(j))*delta_x;
end
  2 Comments
Setsuna Yuuki.
Setsuna Yuuki. on 4 Dec 2020
Hola nicolas, probaré el código y te digo si encuentro el problema, si es que nadie lo encuentra antes jaja :D
Nicolás Vásquez
Nicolás Vásquez on 4 Dec 2020
Hola, Bastian! Agradezco mucho tu oferta de ayuda :D, pero resulta que ya pude solucionar mi problema. Tenía que hacer un "for" para definir n_rec = 1:100, y después hacer una seguidilla de arreglos para las variables. ¡Nada muy complicado! Si es que has estado intentando solucionar el problema y lograste hacerlo de otra forma o alguna parecida, me encantaría ver el código para poder aprender un poco más sobre todo esto >:). Gracias.

Sign in to comment.

Answers (1)

Setsuna Yuuki.
Setsuna Yuuki. on 4 Dec 2020
Edited: Setsuna Yuuki. on 4 Dec 2020
Cambié un poco el código, pero casi nada.
clearvars;
a = 0; b = 2;
n_rec = 100;
delta_x = (b-a)/n_rec;
x = 0:delta_x:2; %EL dx se puede hacer de esta forma.
f=funcion(x);
area_Integral = integral(f,0,2); %Calculo integral para comprobar el resultado de Riemann.
area_R = 0;
area_L = 0;
%Puse los dos juntos, puedes separarlos, es lo mismo en realidad.
for i = 1:n_rec
area_R = area_R + f(x(i + 1))*delta_x;
area_L = area_L + f(x(i))*delta_x;
end
resultados = table(area_Integral,area_L,area_R)
function f=funcion(x)
f=@ (x) x.^3 - 3*x.^2 + 3*x;
end
Espero que te sirva :D
  2 Comments
Nicolás Vásquez
Nicolás Vásquez on 4 Dec 2020
Acabo de ver esto, te respondí en la otra respuesta. Muchas gracias!
Setsuna Yuuki.
Setsuna Yuuki. on 4 Dec 2020
Que buena que encontraste como arreglarlo, con los errores se va aprendieno y que bueno encontrar a alguien que hable español por acá jaja

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!