Why doessn't my code work for compsite simpson one third rule?

this is the function file I created to integrate my function func=1+exp(-x) but my m file does not run?
function I=csimp(func,a,b,n)
h = (b-a)/(n-1);
xi0 = feval(func,a)+feval(func,b);
xi1 = 0; xi2 = 0;
for i = 1:n-1
x = a+i*h;
if mod(i,2) == 0
xi2 = xi2+feval(func,x);
else
xi1 = xi1+feval(func,x);
end
end
I= h*(xi0+2*xi2+4*xi1)/3;
this is my m file
a=0;
b=4;
n=7;
func=1+exp(-x);
I=csimp(func,a,b,n)

댓글 수: 1

The formatting of your function was a bit weird, so I cleaned it up.

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

답변 (1개)

Andrew Newell
Andrew Newell 2017년 5월 15일
편집: Andrew Newell 2017년 5월 15일
You weren't defining the function properly. Try this:
func=@(x) 1+exp(-x);
See Anonymous functions for how to define and use an inline function.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 5월 15일

편집:

2017년 5월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by