How to set vector limits when doing an integration in Matlab?

In summary: However, it is important to note that arrayfun may not be the most efficient solution for more complicated integrands. In that case, it may be better to use a combination of vectorization and for loops to achieve the desired result.
  • #1
horminal
1
0
How to set "vector limits" when doing an integration in Matlab?

I come across a trouble when I tried to set the limits of the integration as vector:

%__________________________________________%
b=1:3;
c= quadv(@(x) x.^2+b, b, b+20);
%__________________________________________%

Then Matlab says:

? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> quadv>quadstep at 141
if abs(h) < hmin || c == a || c == b

Error in ==> quadv at 75
[Q2,fcnt,warn2] = ...
Although quadv allows the integrand to be a vector, it seems Matlab does not allow the integration LIMITS to be a vector anyway. (In the above example, it means I cannot set the integration limits as (b, b+20) because b is a vector.)

I could have use the "for" loop as an alternative:

%__________________________________________%
b=1:3; c=b;
for i=1:numel(b)
c(i) = quadv(@(x) x.^2+b(i), b(i), b(i)+10);
end
%__________________________________________%

However, this will significantly increase the time needed to run the program because in this case all the integrations are not done simultaneously, and therefore a lot of time is wasted in doing the "for" loop.

Thus, does anyone have any suggestion in setting the integration limits as vectors?

(P.S. The above integrand is a simple example, while I am practically handling a much more complicated integrand.)
 
Physics news on Phys.org
  • #2
The best way to set "vector limits" when doing an integration in Matlab is to use arrayfun. Arrayfun allows you to apply a function to each element of an array, and in this case, it can be used to apply the integration function to each element of the vector.For example:%__________________________________________%b=1:3;c= arrayfun(@(x) quadv(@(x) x.^2+x, x, x+20), b);%__________________________________________%This will calculate the integrals for each element of the b vector and return the results as a vector c. This is much more efficient than using a for loop.
 

1. What is the purpose of setting vector limits when doing an integration in Matlab?

The vector limits in Matlab are used to specify the range over which the integration will be performed. This allows for more accurate and precise calculations, as well as the ability to integrate over specific intervals or regions of a function.

2. How do I set vector limits in Matlab for integration?

To set vector limits in Matlab, you can use the "integral" function and specify the limits as the second and third input arguments. Alternatively, you can use the "int" function and specify the limits as a vector in the third input argument.

3. Can I integrate over multiple dimensions using vector limits in Matlab?

Yes, you can use vector limits to integrate over multiple dimensions in Matlab. You can specify the limits for each dimension by using a vector containing the lower and upper limits for each dimension as the third input argument in the "integral" or "int" functions.

4. What happens if I do not specify vector limits for integration in Matlab?

If you do not specify vector limits for integration in Matlab, the default limits will be used, which is often the full range of the function. However, this may not always be appropriate for your specific integration needs, so it is recommended to specify vector limits for more accurate results.

5. Are there any limitations to setting vector limits for integration in Matlab?

Yes, there are some limitations to setting vector limits for integration in Matlab. The limits must be finite values and cannot include infinite or undefined values. Additionally, the limits must be in ascending order, with the lower limit specified first and the upper limit specified second.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
567
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
937
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Replies
16
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top