trapc.m
来自「sareli<matlab科学计算>配套程序」· M 代码 · 共 24 行
M
24 行
function [Itpc]=trapc(a,b,M,f,choice,varargin)%TRAPC Composite two-points numerical integration.% ITPC = TRAPC(A,B,M,FUN,CHOICE) computes an approximation of the integral % of the function FUN via the trapezoidal method (with M equispaced intervals)% if CHOICE=1, via the Gauss composite formula if CHOICE=2.% FUN accepts real scalar input x and returns a real scalar% value. FUN can also be an inline object.H=(b-a)/M;switch choice case 1, x=linspace(a,b,M+1); fpm=feval(f,x,varargin{:}); fpm(2:end-1)=2*fpm(2:end-1); Itpc=0.5*H*sum(fpm);case 2, z=linspace(a,b,M+1); x=z(1:end-1)+H/2*(1-1/sqrt(3)); x=[x, x+H/sqrt(3)]; fpm=feval(f,x,varargin{:}); Itpc=0.5*H*sum(fpm);otherwise disp('Unknown formula');endreturn
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?