定积分Matlab

编程入门 行业动态 更新时间:2024-10-23 23:22:40
本文介绍了定积分Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Matlab中的一个项目上工作,需要找到两条线之间的区域(在[-1,+ 1]区间中与点(xIntersection,yIntersection)相交.所以我们的想法是减去两条并在[-1,xIntersection]和[xIntersection,+1]之间进行积分,对结果求和,如果结果为负,则更改其符号.

I'm working on a project in Matlab and need to find the area between two lines (intersecting in a point (xIntersection,yIntersection) in the interval [-1,+1]. So the idea is to subtract the two lines and integrate between [-1, xIntersection] and [xIntersection, +1], sum the results and if it's negative, change its sign.

有关如何找到两条线的交点的详细信息,请参见链接.

For details on how I find the intersection of the two lines check this link.

我正在使用Matlab's函数 int() ,这里是我的代码段:

I'm using Matlab's function int(), here a snippet of my code:

xIntersection = ((x_1 * y_2 - y_1 * x_2) * (x_3 - x_4) - (x_1 - x_2) * (x_3 * y_4 - y_3 * x_4) ) / ((x_1 - x_2) * (y_3 - y_4) - (y_1 - y_2) * (x_3 - x_4)); syms x; integral = int( line 1 - line 2 expression containing x, x, -1, xIntersection) + int( line 1 - line 2 expression containing x, x, xIntersection, 1) if(integral < 0), integral = integral * -1; end

问题是Matlab不会为整数返回实际值,而是返回包含除法的表达式,即:

The problem is that Matlab doesn't return a real value for the integral but instead an expression containing a division, i.e. :

107813370750829368626584124420059/162259276829213363391578010288128

107813370750829368626584124420059/162259276829213363391578010288128

这使我无法根据集成结果进行进一步的操作.

This prevents me from been able to do further operations with the result of integration.

  • 是否知道为什么这是返回值?
  • 有任何可能的漏洞的想法吗?
  • 推荐答案

    两条曲线之间的面积等于上曲线"和下曲线"之差的整数,因此登录不正确第二个整数.

    The area between two curves is equal to the integral of the difference between the "upper curve" and the "lower curve", so you have an incorrect sign in the second integrand.

    但是,主要问题是您正在使用 symbolic 表达式.这意味着MATLAB将尽力为您提供一个确切答案,而不是一个近似的(数字)答案.

    The main problem is however that you are using symbolic expressions. That means MATLAB will try its very best to give you an exact answer, rather than an approximate one (numerical).

    如果需要数字结果,请使用数字方法:

    If you want numeric outcomes, use numeric methods:

    result = ... quadgk( @(x) line1(x) - line2(x), -1, xIntersection) + ... quadgk( @(x) line2(x) - line1(x), xIntersection, 1 );

    result = ... quadgk(@(x) abs(line1(x) - line2(x)), -1, +1);

    简而言之:)

    我相信integral是新版本的MATLAB(> R2010a)中的首选功能,但是我无法对此进行测试.

    I believe integral is the function of choice in newer versions of MATLAB ( > R2010a), but I can't test this.

    更多推荐

    定积分Matlab

    本文发布于:2023-11-30 02:54:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648482.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:积分   Matlab

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!