在Matlab中构建递归函数来绘制自适应梯形正交 [英] Building a recursive function in Matlab to draw adaptive trapezoidal quadrature

查看:105
本文介绍了在Matlab中构建递归函数来绘制自适应梯形正交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个接受容差水平、上下界和函数的函数,来计算自适应梯形正交,以及绘制一个图形,比如这个:

I want to build a function that takes in the tolerance level, upper and lower bounds, and the function, to calculate the Adaptive Trapezoidal Quadrature, as well as drawing a figure, such as this one:

因为我需要节点值来绘制我的图形,所以我尝试编码如下:

Because I need the node values to draw my figure, I tried coding as follows:

function [node, approx] = aq(f,a,b,tol)
t = (b-a)*(f(b)+f(a))/2;
    if abs((t2 - t)/3) > tol %Since T(2)-T(1)=E(1)-E(2)=3*E(2)
        m = (a+b)/2;
        [node1, approx1] = aq(f,a,m,tol/2);
        [node2, approx2] = aq(f,m,b,tol/2);
        node = [node1(1:end-1) node2];
        approx = approx1+approx2;
    else
        node = [a,b];
        approx = tf;
    end
end

我的代码有两个问题:一个是,显然没有定义 t2.我不知道如何定义它,因为根据正交规则,下一个估计可能包括梯形的两侧或仅包括一个.我很困惑.也许我需要定义一个单独的函数来计算梯形面积.

I have two problems with my code: One is that, obviously, t2 is not defined. I do not know how to define it because, depending on whether the quadrature rule, the next estimation could include both sides of the trapezoid or just one. I'm confused. Maybe I need to define a separate function that calculates trapezoidal area.

第二个问题是,即使我放入一个具有已知整数值的函数(例如,

The second problem is that even if I put in a function with its known integral value (for example,

tol = 10^-2;

tol = 10^-2;

f = @(x) exp(x)*sin(x);

f = @(x) exp(x)*sin(x);

a=0;b=pi;

.5*(exp(pi)+1) %是我们精确的整数值,我们可以把它放到if语句中

.5*(exp(pi)+1) %is our exact integral value, we can put this into the if statement

但是代码进入了无限循环.我不知道如何阻止它,因为理论上它应该收敛.

But the code goes into infinite loop. I don't know how to stop this, because theoretically, it's supposed to converge.

请放轻松,因为我是数值分析课程的数学专业,这是我第一年编码.谢谢!

Please go easy on me as I am a math major in a numerical analysis class, and this is my first year coding. Thanks!

谢谢!让它工作:)

推荐答案

根据上下文,t2 是两段的复合梯形公式,

By context, t2 is the composite trapezoidal formula for two segments,

t2 = (b-a)/2*(f(a)+2*f(m)+f(b))/2

误差是第二次迭代差异,前面有一些因素,

The error is then the second iterated difference, with some factor in front,

(t2-t)/3 = (b-a)/4*(-f(a)+2*f(m)-f(b))

梯形值的理查森外推是辛普森值

and the Richardson extrapolation of the trapezoidal values is the Simpson value

tf = (4*t2-t)/3 = (b-a)/6*(f(a)+4*f(m)+f(b))

你应该引入一些数据结构或其他机制来消除 f 在同一点的多重计算.它并不过分关键,但可能会从计算时间中去除一个重要因素.

You should introduce some data structure or other mechanism to remove the multiple evaluation of f at the same points. It is not overly critical, but might remove a non-trivial factor from the computation time.

这篇关于在Matlab中构建递归函数来绘制自适应梯形正交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆