# Regarding MATLAB

1. Jun 18, 2011

### shabby

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!!

2. Jun 19, 2011

### shabby

3. Jun 20, 2011

### MATLABdude

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 (Text):
[ /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')