Why doessn't my code work for compsite simpson one third rule?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Andrew Newell
2017-5-15
编辑:Andrew Newell
2017-5-15
You weren't defining the function properly. Try this:
func=@(x) 1+exp(-x);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!