Numerical Integration in MATLAB

Click For Summary
SUMMARY

This discussion focuses on performing numerical integration in MATLAB using the trapezoidal rule. The user encountered matrix dimension mismatches while trying to integrate a function involving multiple variables, specifically with the expression involving Bessel functions. The solution provided involves using a loop to iterate over fixed theta values and applying element-wise multiplication to ensure correct matrix dimensions. The final result is plotted against theta to visualize the integration outcome.

PREREQUISITES
  • Understanding of MATLAB syntax and operations
  • Familiarity with numerical integration techniques, specifically the trapezoidal rule
  • Knowledge of Bessel functions and their usage in MATLAB
  • Experience with plotting functions in MATLAB
NEXT STEPS
  • Learn about MATLAB's element-wise operations and their importance in matrix calculations
  • Explore MATLAB's documentation on the trapz function for numerical integration
  • Investigate advanced numerical integration techniques available in MATLAB
  • Study how to optimize loops in MATLAB for performance improvements
USEFUL FOR

Students, researchers, and engineers working with numerical methods in MATLAB, particularly those involved in physics or engineering applications requiring integration of multi-variable functions.

shabby
Messages
2
Reaction score
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
Waiting for a reply..Please do reply!
 
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 (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:

Similar threads

Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
27
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K