我在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
发布评论