Defined Matlab Integral

I am working on a project in Matlab and you need to find the area between two lines (intersecting at a point (xIntersection,yIntersection) in the interval [-1, + 1]. So the idea is to subtract two lines and integrate between [- 1, xIntersection] and [xIntersection, +1], summarize the results and, if they are negative, change their sign.

For more information on how to find the intersection of two lines, check out this link.

I use Matlab's int() function, here is a code snippet:

 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 

The problem is that Matlab does not return a valid value for the integral, but instead an expression containing division, that is:

107813370750829368626584124420059/162259276829213363391578010288128

This does not allow me to perform further operations with the integration result.

  • Any idea why this is a return value?
  • Any idea of ​​a possible loophole?
+2
source share
2 answers

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

The main problem is that you are using symbolic expressions. This means that MATLAB will try to give you the most accurate answer, not an approximate (numerical) one.

If you want numerical results, use numerical methods:

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

or

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

to be short:)

I believe that integral is a selection function in newer versions of MATLAB (> R2010a), but I cannot verify this.

+4
source

I do not agree with this @Rody. In general: if you have a chance, why not get the exact result. If this can be resolved, at least you don't have to worry too much about numerical problems.

Just wrap the result with double , as suggested by @thewaywewalk.

+2
source

Source: https://habr.com/ru/post/1501760/


All Articles