-
Matlab 辛普森积分方式
2018-11-23 21:52:37Matlab函数,数值积分中的辛普森积分公式,用来求数据积分 -
MATLAB 与 C 语言的混合编程实战之辛普森积分法、自适应辛普森积分
2019-11-19 21:46:54题目大意是让你用c系语言实现辛普森积分法对定积分的粗略估计,所谓辛普森积分法即为: 定义:辛普森法则(Simpson's rule)是一种数值积分方法,是牛顿-莱布尼茨公式的特殊形式,以二次曲线逼近的方式取代矩形或...题目要求
题目大意是让你用c系语言实现辛普森积分法对定积分的粗略估计,所谓辛普森积分法即为:
定义:辛普森法则(Simpson's rule)是一种数值积分方法,是牛顿-莱布尼茨公式的特殊形式,以二次曲线逼近的方式取代矩形或梯形积分公式,以求得定积分的数值近似解。其近似值如下:
那很明显可以看出,改进积分结果有两种方法,一是二分区间之后再次二分不断逼近,二是从积分间隔入手,不断缩小积分间隔
给出Matlab-C++代码
//Author:glm #include<cstdio> #include<cmath> #include<iostream> #include "mex.h" #define ll long long int #define rg register ll inline double f(double x) { if(x==0)return 1; return sin(x)/x; } inline double calculate(double a,double b)//int(f,a,b)=(b-a)/6*(f(a)+4*f((a+b)/2+f(b)) { double sum=0; for(rg i=0;i<(ll)((b-a)/0.025);i++) { double a1=a+0.0250*i,b1=a1+0.0250,mid=(a1+b1)/2.0; sum+=(mid-a1)/6*(f(a1)+4*f((a1+mid)/2)+f(mid))+(b1-mid)/6*(f(mid)+4*f((b1+mid)/2)+f(b1)); } return sum; /* int parts=(int)((b-a)/0.01); double ans=0; for (int i=0;i<parts;i++) { double ai=a+i*0.01; double bi=ai+0.01; double mi=(ai+bi)/2; ans+=(mi-ai)/6*(f(ai)+4*f((ai+mi)/2)+f(mi))+(bi-mi)/6*(f(mi)+4*f((bi+mi)/2)+f(bi)); //ans+=(bi-ai)/6*(f(ai)+4*f((ai+bi)/2)+f(bi)); } return ans;*/ } void mexFunction (int nlhs,mxArray *plhs[],int nrhs,const mxArray * prhs[]) { double *a; double b,c; plhs[0]=mxCreateDoubleMatrix(1,1,mxREAL); a=mxGetPr(plhs[0]);// b=*(mxGetPr(prhs[0])); c=*(mxGetPr(prhs[1])); *a=calculate(b,c); }
报告的latex代码~
\documentclass[12pt]{article} \usepackage{amsmath} \usepackage{mathtools} \usepackage{graphics,epsfig} \usepackage{xspace} \usepackage{color} \usepackage{amssymb} \usepackage{subfigure} \usepackage{multirow} \usepackage{listings} \usepackage{graphicx} \usepackage{url} %%uncomment following line if you are going to use ``Chinese'' %\usepackage{ctex} \definecolor{ballblue}{rgb}{0.13, 0.67, 0.8} \definecolor{cornflowerblue}{rgb}{0.39,0.58,0.93} \definecolor{babyblueeyes}{rgb}{0.63, 0.79, 0.95} % preset-listing options \lstset{ backgroundcolor=\color{white}, basicstyle=\footnotesize, language=matlab, breakatwhitespace=false, breaklines=true, % sets automatic line breaking captionpos=b, % sets the caption-position to bottom commentstyle=\color{ballblue}, % comment style extendedchars=true, frame=single, % adds a frame around the code keepspaces=true, keywordstyle=\color{blue}, % keyword style numbers=left, numbersep=5pt, numberstyle=\tiny\color{blue}, rulecolor=\color{babyblueeyes}, stepnumber=1, stringstyle=\color{black}, % string literal style tabsize=4, % sets default tabsize to 4 spaces title=\lstname } \usepackage{geometry} \geometry{ a4paper, total={210mm,297mm}, left=20mm, right=20mm, top=20mm, bottom=20mm, } \marginparwidth = 10pt \begin{document} \title{Weekly Assignment:Simpson’s rule of calculation} \author{ \\ Stdudent Guo LiMin \\ Id: 22920182204174} \maketitle \section{Problem Description} Given function $f(x)=\cfrac{sinx}{x}$ , the integral range [a, b], please work out its numerical integral in range [a, b]. You are required to calculate the numerical integral by Simpson’s rule. \\\\{\color{red}Requirements:}\\\\ 1. Your function myQuad MUST be implemented in C and called by Matlab\\ 2. The command interface in Matlab looks like: v = myQuad(a, b);\\ 3. You are NOT allowed to use “recursive procedure” when you implement Simpson’s rule\\ 4. Given $F(x) =\int^{6}_{-2}\cfrac{sinx}{x}dx$, please plot out the curve of F(x) in the range [-2 6]\\ 5. According to Simpson’s rule, the number of intervals will impact on the precision. You are required to try different numbers of intervals and see how it impacts your result. Please present an analysis about this in your report. \section{Code of Solution} The followings are codes implemented by C++,no fancy algorithm included,working out this problem only by using simple simulations\\ % %if one wants to paste part of the code into the report %one can put in following manner \begin{lstlisting}[language=c++, linewidth=\linewidth,caption={main working function(myQuad.cpp)}] #include<cstdio> #include<cmath> #include<iostream> #include "mex.h" #define ll long long int #define rg register ll inline double f(double x) { return sin(x)/x; } inline double calculate(double a,double b)//int(f,a,b)=(b-a)/6*(f(a)+4*f((a+b)/2+f(b)) { double sum=0; for(rg i=0;i<(ll)((b-a)/0.001);i++) { double a1=a+0.001*i,b1=a1+0.001,mid=(a1+b1)/2.0; sum+=(mid-a1)/6*(f(a1)+4*f((a1+mid)/2)+f(mid))+(b1-mid)/6*(f(mid)+4*f((b1+mid)/2)+f(b1)); } return sum; /* int parts=(int)((b-a)/0.01); double ans=0; for (int i=0;i<parts;i++) { double ai=a+i*0.01; double bi=ai+0.01; double mi=(ai+bi)/2; ans+=(mi-ai)/6*(f(ai)+4*f((ai+mi)/2)+f(mi))+(bi-mi)/6*(f(mi)+4*f((bi+mi)/2)+f(bi)); //ans+=(bi-ai)/6*(f(ai)+4*f((ai+bi)/2)+f(bi)); } return ans;*/ } void mexFunction (int nlhs,mxArray *plhs[],int nrhs,const mxArray * prhs[]) { double *a; double b,c; plhs[0]=mxCreateDoubleMatrix(1,1,mxREAL); a=mxGetPr(plhs[0]);// b=*(mxGetPr(prhs[0])); c=*(mxGetPr(prhs[1])); *a=calculate(b,c); } \end{lstlisting} \begin{lstlisting}[language=c++, linewidth=\linewidth,caption={Draw the contrast curve and main interface code(test.m)}] mex myQuad.cpp syms t a=-2:0.27:6; b=-2:0.27:6; c=-2:0.27:6; cnt=0; for x=-2:0.27:6 y=int(sin(t)/t,-2,x); cnt=cnt+1; b(cnt)=y; c(cnt)=myQuad(-2,x); b(cnt),c(cnt); end plot(a,b,'r',a,c,'b') \end{lstlisting} \section{Experiment Theory and Results} Given function f(x), and its range of x [a,b], it is possible to estimate its integral in range [a,b] by quandratic interpolation based on “Simpson’s rule”. The “Simpson’s rule” is given as follows.\\ \begin{equation} \begin{split}$\int^{b}_{a}\cfrac{sinx}{x}dx &=\cfrac{(b-a)}{6}*[f(a)+4*f(\cfrac{a+b}{2})+f(b)]\\ &=\cfrac{(m-6)}{6}*[f(a)+4*f(\cfrac{a+m}{2})+f(m)]+\cfrac{(b-m)}{6}*[f(m)+4*f(\cfrac{b+m}{2})+f(b)]\nonumber$ \end{split} \end{equation} \\ What’s more,The more number of segments used in the range [a, b], the better is the approximation. \\\\ To start with,we should learn how to compile c/c++ files with matlab and how to write fantastic and unified-formatted papers, this aspect of learning has been updated to my personal blog {\color{red}\url{newcoder-glm.blog.csdn.net}},so for more information,please go up to visit my blog. \\\\ After mastering the mixtrue of C/C++& matlab programming technique(It's just an interface that calls an external compiler in essence),as easy as pie we can write the codes above and should curve figures(check out the attachment for more details) which shows graphs of the result and the exact result obtained by Simpson integral method when the interval of integral interval is divided differently(when the value is 0.05 0.25) \begin{table}[h] \caption{Circumstances when we apply different intervals} \begin{center} \begin{tabular}{|c|c|c|c|} \hline Interval & 0.01 & 0.10 & 0.25\\ \hline Results & \textbf{2.551496047169967}& \textbf{2.551496048068467} & \textbf{2.551496047173387 } \\ \hline \end{tabular} \end{center} \end{table} \subsection{further ideas} We can see from the Simpson integral (the simple three-point formula) that there is some error, and it's easy to see that the smaller the interval, the more accurate the integral, however,if we shorten the interval, of course the results' accuracy come up, but at the cost of higher time complexity.\\ So here we introduce "Adaptive Simpson’s rule" which is the integral can automatically control the size and length of the cutting interval, so that the accuracy can meet the requirements. In particular, if the midpoint of [a,b] is c, the result will be directly returned if and only then {\color{red}$[S(a,c)+S(c,b)-S(a,b)]<15Eps$} Otherwise, the recursive call will divide the interval again. The accuracy of Eps should be reduced by half when recursion is called. \begin{figure}[h] \centering {\includegraphics[width=0.7\linewidth]{025.pdf}} \hspace{0.15in} \end{figure} \begin{figure}[h] \centering {\includegraphics[width=0.7\linewidth]{005.pdf}} \hspace{0.15in} \end{figure} \section{What I learned from the Project} 1.learning how to programing cpp files with powerful matlab,excuted with high efficiency and conciseness} \\\\2.the ability of exploring new territory that never have been leart and mastering it(hand in report in time)} \\\\3.learning how to write fantastic and unified-formatted papers with modernized Latex} \section{Two attached pictures of point three} \end{document}
报告的pdf~
-
自适应辛普森积分matlab_数值积分(Python)
2020-11-24 10:11:09120701101数值积分 复合梯形法辛普森1/3法CONTENTSCONTENTS1. 复合梯形法1.1 代码1.2 计算结果1.3 图像结果2. 辛普森1/3法2.1 代码2.2 计算结果2.3 图像结果3. 源码编辑器1. 复合梯形法1.1 代码# -*- coding: utf-8 ...120701101 数值积分
- 复合梯形法
- 辛普森1/3法
CONTENTS
- CONTENTS
- 1. 复合梯形法
- 1.1 代码
- 1.2 计算结果
- 1.3 图像结果
- 2. 辛普森1/3法
- 2.1 代码
- 2.2 计算结果
- 2.3 图像结果
- 3. 源码编辑器
1. 复合梯形法
1.1 代码
# -*- coding: utf-8 -*-
1.2 计算结果
运行代码即可得到结果,
该区间内的数值积分为
1.3 图像结果
复合梯形 2. 辛普森1/3法
2.1 代码
# -*- coding: utf-8 -*-
2.2 计算结果
该区间内的数值积分为
2.3 图像结果
辛普森1/3法 3. 源码编辑器
Anaconda(强烈推荐) + Spyder 4(和MATLAB有着相似界面的编辑器)
-
复合辛普森公式求解定积分 matlab
2021-03-31 09:19:56复合辛普森积分 已知函数表达式与积分区间 精度esp正相关与1/num %复合辛普森积分 %已知函数表达式与积分区间 clc;clear; a=0;b=1;%积分范围 num=1000;%积分准确度 h=(b-a)/(2*num); f=@(x)exp(-x);%积分表达式 I=0;...复合辛普森积分
已知函数表达式与积分区间
精度esp正相关与1/num%复合辛普森积分 %已知函数表达式与积分区间 clc;clear; a=0;b=1;%积分范围 num=1000;%积分准确度 h=(b-a)/(2*num); f=@(x)exp(-x);%积分表达式 I=0;%积分结果 I=f(a)-f(b); for i=1:num I=I+(2*f(a+2*i*h)+4*f(a+(2*i-1)*h)); end I=I*h/3
-
编程MATLAB程序实现复化梯形和辛普森数值积分
2013-04-25 20:03:58MATLAB程序实现复化梯形和辛普森数值积分 -
编程MATLAB程序 实现复化梯形和辛普森数值积分.pdf
2020-11-11 17:08:32数值分析实验报告 [1] 掌握复化梯形和辛普森数值积分法的基本原理和方法 实 验 [2] 编程 MATLAB程序实现复化梯形和辛普森数值积分 目 的 1. 编程序实现复化梯形数值积分求积公式 function y=f(x) y=sqrt(x*log(x; 实... -
复化梯形法和辛普森数值积分的matlab实现程序
2013-06-30 11:06:20这是一段复化梯形法和辛普森数值积分的matlab实现程序。 -
辛普森积分算法源代码
2012-12-28 00:25:29辛普森积分算法源代码 matlab格式编写,欢迎下载 -
编程MATLAB程序实现复化梯形和辛普森数值积分.pdf
2020-09-02 12:41:44数值分析实验报告 实 [1] 掌握复化梯形和辛普森数值积分法的基本原理和方法 验 目 [2] 编程 MA TLAB 程序实现复化梯形和辛普森数值积分 的 1. 编程序实现复化梯形数值积分求积公式 function y=f(x) y=sqrt(x*log(x;... -
matlab 积分_MATLAB龙贝格积分算法
2020-11-24 10:11:39由于其采用的是逐次分半计算,后一次计算是对前一次近似结果的修正,因此相对于辛普森和科特斯求积方法精度更高;并且其前一次分割计算的函数值在分半之后还可以被继续使用,因此提升了计算的效率,是一种精度较高的...什么是龙贝格积分算法
龙贝格(Romberg)积分算法也被称为逐次分半加速算法,通过把积分区间逐次分半的方法进行数值积分求解。由于其采用的是逐次分半计算,后一次计算是对前一次近似结果的修正,因此相对于辛普森和科特斯求积方法精度更高;并且其前一次分割计算的函数值在分半之后还可以被继续使用,因此提升了计算的效率,是一种精度较高的加速计算积分的方法。
龙贝格积分算法计算过程
龙贝格积分算法是一种将理查森外推加速法应用于复合梯形公式而形成的一种积分方法,递推公式可表示为
(1)取,求 (2)利用变步长梯形公式求,其中为区间的分半次数,即(3)按照次序依据递推公式逐个求出加速值 (4)比较递推矩阵相邻对角元素,两者之差绝对值小于预设精度要求时终止计算。龙贝格积分算法程序设计
龙贝格算法程序设计如下:
function [y,T]=romberg(f,a,b,e)k=0;h=b-a;T(1,1)=h/2*(f(a)+f(b));%第一步d=h;%初始化相邻两次误差while e<=d k=k+1; QH=0; for j=0:2^(k-1)-1 QH=QH+f(a+(2*j+1)*(b-a)/2^k); end T(k+1,1)=T(k,1)/2+(b-a)/2^k*QH;%第二步 for j=1:k T(k+1,j+1)=T(k+1,j)+(T(k+1,j)-T(k,j))/(4^j-1);%第三步 end d=abs(T(k+1,k+1)-T(k,k));%第四步endy=T(k+1,k+1);
龙贝格积分算法算例分析
应用龙贝格算法计算积分
使其精度达到10-5,其精确值为0.25。输入程序代码
f=inline('x^3')[y,T]=romberg(f,0,1,1e-5)
运行结果为
注:公式和代码左右滑动查看完整效果。y = 0.250000000000000T = 0.500000000000000 0 0 0.312500000000000 0.250000000000000 0 0.26562500000000 0.250000000000000 0.250000000000000
-
MATLAB复化辛普森(Simpson)公式和复化梯形公式求积分
2019-12-26 09:36:22包含代码和文档 采用复化梯形公式和复化辛普森公式求积分,并与精确值进行比较得下表。 采用复化梯形公式和复化辛普森公式求积分,并与精确值进行比较得下表。 -
复化辛普森公式求二重积分matlab源码及例题
2020-06-08 17:04:18复化辛普森公式求二重积分matlab源码 直接拷贝到matlab编辑器,傻瓜式操作。具体算法自行探究,网上都有,小编只提供代码。用的好的请加个关注,篱落~~成殇~~再次先行谢过。 %%%%%%%%%% 2020.6.5 %%%%%%%%% %%%%... -
matlab用辛普森公式求积分_如何用Excel公式求最大值对应的行列序号
2020-11-19 18:08:32微信公众号: Excel and Python微信名搜索: 实用办公编程技能如何用Excel公式求最大值对应的行列序号呢?下面,我们来看看来自问题互动栏目的一个具体问题。具体问题:求出哪一天哪一项的收益最高提示:求出最大值... -
Matlab数值积分和数值微分.pdf
2020-07-11 20:48:43一重数值积分的实现方法 变步长辛普森法高斯 - 克朗罗德法梯形积分法 1.1 变步长辛普森法 Matlab 提供了 quad 函数和 quadl 函数用于实现变步长 辛普森法求数值积分 . 调用格式为 : [I,n]=Quad@fname,a,b,tol,trace... -
复合梯形公式matlab代码_MATLAB龙贝格积分算法
2021-01-27 18:24:15由于其采用的是逐次分半计算,后一次计算是对前一次近似结果的修正,因此相对于辛普森和科特斯求积方法精度更高;并且其前一次分割计算的函数值在分半之后还可以被继续使用,因此提升了计算的效率,是一种精度较高的... -
自适应辛普森公式matlab程序_fx-92+ Spéciale Collège 实现复合辛普森法定积分计算...
2020-12-03 05:07:20基本原理 复合辛普森积分法是一种用抛物线近似函数曲线来求定积分数值解的方法。把积分区间等分成若干段,对被积函数在每一段上使用辛普森公式,根据其在每一段的两端和中点处的取值近似为抛物线,逐段积分后加起来... -
【数值分析实验MATLAB】数值积分:梯形公式、辛普森公式、复化梯形公式、复化辛普森公式、龙贝格算法、...
2020-01-21 17:06:50matlab代码 -
辛普森求积公式 和 复合辛普森求积公式 Matlab 实现
2017-11-04 19:46:34辛普森求积公式 和 复合辛普森求积公式 Matlab 实现辛普森求积公式 利用区间[a,b]的端点及中点计算积分 ∫x2x1f(x)dx≈b−a6×(f(a)+4×f(b−a2)−+f(b)) \int_{x_1}^{x_2} f(x)dx \approx \frac{b-a}{6} \times ... -
MATLAB数值积分求值实验报告_计算方法数值实验实验报告
2020-08-06 00:29:31学号 班级 统计1001 姓名 指导教师 易昆南 实验题目 用多种方法计算数值积分 评 分 1设计实习目的 了解MATLAB在实际问题中的应用 通过实践加深对这门语言中M文件的了解 熟悉简单程序结构如循环结构for循环while循环... -
辛普森(Simpson)的3/8规则【含Matlab源码】
2021-01-20 20:03:37Matlab代码为辛普森的三八法则(复合)编码,以进行数值积分。 辛普森的3/8规则(也称为辛普森的第二个规则)是托马斯·辛普森(Thomas Simpson)提出的另一种数值积分方法。它基于三次插值而不是二次插值。... -
复合梯形公式与复合辛普森公式MATLAB
2019-03-18 00:53:51采用复合梯形公式与复合辛普森公式,计算 sin(x)/x 在[0, 1]范围内的积分。采样点数 目为 5、9、17、33。
收藏数
84
精华内容
33