Numerical Integration in MATLAB: A Guide

In summary, you are trying to integrate an expression over r, but the limits of integration (0:0.01:22.5) are different from the theta (0:pi/100:pi) variation. You need to use a loop for each theta value, and you also need to use element-wise multiplication in your 'expr' step.
  • #1
shabby
2
0
Hello, found this forum very instructive!
I had this doubt of numerical integration in MATLAB. I have to perform this integration in matlab:

r = 0:0.01:22.5; //necessary because they automatically become lower and upper limits of integration!
theta=0:pi/100:pi; // limits are not to be necessarily these
f = 6.09*r*sin(theta);
expr = besselj(0,f)*r * (1-0.7.*r);
int_ans = trapz(expr,r) // integrating wrt r only..theta must remain like const!

now here matrix dimensions are not agreeing! Should i multiply r.*sin(theta) or r*sin(theta) or r' * sin(theta)??
kindly help in this regarding handling num int with multiple variables
btw i have to plot the final answer wrt theta!
please help..would be very obliged!
 
Physics news on Phys.org
  • #2
Waiting for a reply..Please do reply!
 
  • #3
Welcome to PhysicsForums!

What is it that you're trying to do? I think that you're trying to find the integral over r with theta constant, but you can't do it all in one step as you're trying to do.

Trapezoidal integration is only good for single-variable integration:
http://www.mathworks.com/help/techdoc/ref/trapz.html

If that is what you're trying to do, you have to use a loop for each theta value, and you also need to use element-wise multiplication in your 'expr' step: you've got it at the end, but you need to use it elsewhere in that expression as well (otherwise, it'll attempt to multiply your 1x2251 vector with another 1x2251 vector, resulting in your size mismatch):
http://www.mathworks.com/help/techdoc/learn_matlab/f4-1931.html [Broken] (looping)

Since this is a pretty simple task, I've done both below (for future reference, put your code between the
Code:
[ /code] brackets, without the space in the final tag):

[CODE]
r = 0:0.01:22.5; 	%limits of integration
theta=0:pi/100:pi; 	%theta variation
n=prod(size(theta));	%find size of theta vector

for k=1:n
	f = 6.09*r*sin(theta(k));	%find f for fixed theta
	expr = besselj(0,f) .* r .* (1-0.7.*r);
	int_ans(k) = trapz(expr,r); 	%integration of r
end

%Graphing
plot(theta, int_ans)
title('Trapezoid-rule integration of expression','fontweight','b')
xlabel('theta (radians)','fontweight','b')
ylabel('trapz','fontweight','b')

EDIT: Probably gave away a little too much for homework, oh well... By the way, if you're new to MATLAB, you can find the online documentation (including function list) in the link in my signature.

And just in case you put an extra period in your 'expr' expression by mistake, element-wise operations (e.g. .*) vs matrix operations (e.g. *):
http://www.cyclismo.org/tutorial/matlab/operations.html
 
Last edited by a moderator:

1. What is numerical integration?

Numerical integration is a method for approximating the definite integral of a function. It involves dividing the area under the curve into smaller, simpler shapes and using mathematical algorithms to calculate their individual areas, which are then summed up to get an overall approximation of the integral.

2. Why is numerical integration important in MATLAB?

MATLAB is a powerful computational software that allows for efficient numerical calculations. Numerical integration is important in MATLAB as it allows for the efficient and accurate evaluation of integrals, which are commonly used in many scientific and engineering applications.

3. How can I perform numerical integration in MATLAB?

To perform numerical integration in MATLAB, you can use the built-in functions such as "trapz" or "quad". These functions take in the function to be integrated, the limits of integration, and other optional parameters, and return the numerical approximation of the integral.

4. What are the limitations of numerical integration in MATLAB?

One limitation of numerical integration in MATLAB is the dependence on the chosen step size or number of sub-intervals. Choosing too large or too small step sizes can lead to inaccurate results. Additionally, some functions may be difficult to integrate numerically and may require more advanced techniques.

5. How can I improve the accuracy of numerical integration in MATLAB?

To improve the accuracy of numerical integration in MATLAB, you can try using smaller step sizes or increasing the number of sub-intervals. You can also try using more advanced integration techniques, such as Gaussian quadrature, or adjusting the integration method parameters for better results. It is also important to choose appropriate functions and limits of integration for the problem at hand.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
156
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
Replies
2
Views
180
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Replies
7
Views
927
Replies
2
Views
269
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
4
Views
1K
Back
Top