Integrate with Simpson's Rule

조회 수: 32 (최근 30일)
Danny Helwegen
Danny Helwegen 2019년 1월 13일
편집: David Goodmanson 2019년 1월 14일
Hi i have, probably an easy, problem. I made a function (see the outcome below) and i need to integrate the outcome, but since the interval is small the subscript indices aren't integers. This is the problem:
f =
0.0028 0.1333 0.2667 0.4000 0.5333 0.6667 0.8000 0.9333 1.0667 1.1972
So then i use the Simpson Rule:
%interval is [0,1.2]
a = 0; b = 1.2; n =10; h = (b-a)/n
xi=a:h:b
I = h/3*(f(xi(1))+2*sum(f(xi(3:2:end-2)))+4*sum(f(xi(2:2:end)))+f(xi(end)));
But i get the error: Subscript indices must either be real positive integers or logicals.
How can I solve this?

채택된 답변

Matt J
Matt J 2019년 1월 14일
편집: Matt J 2019년 1월 14일
It doesn't look like you xi should be involved, since you already have the samples of f
I = h/3*(f((1))+2*sum(f((3:2:end-2)))+4*sum(f((2:2:end)))+f((end)));
  댓글 수: 1
David Goodmanson
David Goodmanson 2019년 1월 14일
편집: David Goodmanson 2019년 1월 14일
Addressing f_i directly (assuming the x_i are equally spaced) is the right method, but the problem is that f has an even number of points. So the end point f(10) gets a weight of 5, not 1.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by