blog/source/_posts/matlab-simpson.md

55 lines
945 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Matlab使用Simpson辛普森法计算数值积分
date: 2021-11-12 00:00:00
tags:
- Matlab
- 编程
category: 技术
---
![Matlab 运用Simpson辛普森公式求定积分附代码](http://os1.eigeen.com/static/2021/11/Snipaste_2021-11-12_14-41-58.png)
<!-- more -->
直接附代码
```matlab
simpson.m
function [out] = simpson(f,n,a,b)
%SIMPSON 此处显示有关此函数的摘要
% 此处显示详细说明
h=(b-a)/n;
s1=fi(f,1,h,a)+fi(f,n+1,h,a);
s2=0;
for index=3:2:n-1
s2=s2+2*fi(f,index,h,a);
end
s4=0;
for index=2:2:n
s4=s4+4*fi(f,index,h,a);
end
out=h/3*(s1+s2+s4);
end
function [out] = fi(f,i,h,a)
out=f(a+(i-1)*h);
end
```
调用:
```matlab
simpson(<函数句柄>, <n>, <下限>, <上限>)
```
> 参考文章
>
> [matlab用辛普森公式求积分_积分近似计算之辛普森公式_weixin_39726131的博客-CSDN博客](https://blog.csdn.net/weixin_39726131/article/details/110226598)