Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Regarding MATLAB

  1. Jun 18, 2011 #1
    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!
     
  2. jcsd
  3. Jun 19, 2011 #2
    Waiting for a reply..Please do reply!
     
  4. Jun 20, 2011 #3

    MATLABdude

    User Avatar
    Science Advisor

    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')
    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: May 5, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Regarding MATLAB
  1. Matlab and physics (Replies: 3)

  2. MATLAB problem (Replies: 1)

  3. MATLAB histogram (Replies: 3)

Loading...