Need help with a riemann sum

조회 수: 4 (최근 30일)
Nicolás Vásquez
Nicolás Vásquez 2020년 12월 4일
댓글: Setsuna Yuuki. 2020년 12월 4일
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
Setsuna Yuuki.
Setsuna Yuuki. 2020년 12월 4일
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 2020년 12월 4일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Setsuna Yuuki.
Setsuna Yuuki. 2020년 12월 4일
편집: Setsuna Yuuki. 2020년 12월 4일
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
Nicolás Vásquez
Nicolás Vásquez 2020년 12월 4일
Acabo de ver esto, te respondí en la otra respuesta. Muchas gracias!
Setsuna Yuuki.
Setsuna Yuuki. 2020년 12월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by