Need help with a riemann sum

5 ビュー (過去 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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by