⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traptol.m

📁 matlab code for new user to design any project
💻 M
字号:
function [z, n] = traptol(func,a,b,tol)
% Routine for trapezoidal integration to specified tolerance.
%
% USAGE:  [z, n] = traptol('function',a,b,tol)
%
% input:  function = name of external function file.  This file
%             must be able to receive a vector of x data as input 
%	      and produce a vector of y data as output
%         a, b     = limits of integration
%         tol      = absolute error tolerance in answer
% output: z        = value of integral of function between a and b
%         n        = number of intervals required to obtained desired accuracy

% NOTE: in this routine n refers to the number of segments in the
%   interval from a to b

% first integral using 2 segments
n = 2;
h = (b-a)/n;
x = linspace(a,b,n+1);
y = feval(func,x);
z1 = h*trapzeq(y);



abserror = Inf;
% start looping with doubled N
while abserror > tol
  n = 2*n;
  h = (b-a)/n;
  x = linspace(a,b,n+1);
  y = feval(func,x);
  z2 = h*trapzeq(y);
  abserror = abs(z2 - z1);
  z1 = z2; % store this for next loop
end

z = z2;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -