MATLAB Numerical Integration in MATLAB

AI Thread Summary
The discussion focuses on performing numerical integration in MATLAB with respect to a variable while keeping another variable constant. The user encounters a matrix dimension mismatch when trying to integrate the expression involving Bessel functions and trigonometric functions. The key advice is that trapezoidal integration is suitable for single-variable integration, and a loop should be used to iterate over each value of theta. Element-wise multiplication is necessary in the expression to avoid dimension errors. The provided solution includes a code snippet that demonstrates how to fix the issue by calculating the function for fixed theta values and then performing the integration. Additionally, guidance is offered on graphing the results. The importance of using element-wise operations versus matrix operations is also highlighted for clarity in MATLAB coding.
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:
Back
Top