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

Integration in Matlab

  1. Mar 15, 2009 #1
    I have a function

    B(m,n) = Integration of this between 1 and 0 (x.^m-1).*((1-x).^n-1)

    For my m file i have

    function Beta = B(m,n)
    x=quad('@B',0,1);
    Beta=(x.^m-1).*((1-x).^n-1);

    however when i run my m-file it doesnt work. Can anybody help me please
     
  2. jcsd
  3. Mar 15, 2009 #2

    MATLABdude

    User Avatar
    Science Advisor

    Two problems:
    1) Your function is defined AFTER you call the quad function
    2) Your function is improperly defined and used. Your function should be in terms of x (that's what you're varying), and not m and n (also, these had better be defined elsewhere, say m=1, n=2--quad will not give you a symbolic answer).

    Go to the Mathworks documentation page for the quad function and carefully look at their example:
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/quad.html
     
  4. Mar 16, 2009 #3
    thanks so much for your help but I still cant get it to work! I really find Matlab difficult and have virtually got every single book the library has to help me!

    Ive tried fiddling around with my m-file but to no luck!

    When i want to find B(2,3) and B(3,4) using ny m-file, i just get an error message.

    I think its because i have no idea what im doing so any guidance is really appreciated
     
  5. Mar 16, 2009 #4

    MATLABdude

    User Avatar
    Science Advisor

    You didn't pay attention to what I said in my last post that you're not only using function handles incorrectly, but also the 'quad' function. To reiterate, hardcode your function (e.g. use x^2 or something, not x^m)! Without getting overly sophisticated, your function should not be in terms of m or n, but in terms of x. That's the only way you can use it in conjunction with the quad function. Quad is substituting in values only for x, not for m and n! Also, you cannot just declare a function mid-way through an m-file.

    I'll take at face value that this isn't a homework problem. The following is how you can get what I think you're trying to achieve by having MATLAB do the substitution for you. Try declaring a more complex function along the lines of the following:

    Code (Text):

    function integral=quadEvaluator(m,n)
    % Evaluates the integral of (x.^m-1).*((1-x).^n-1)
    % over the interval 0<x<1 and with given input values of m and n

    functionString = ['(x.^', num2str(m), '-1).*((1-x).^', num2str(n), '-1)'];
    % Creates the equation substituting in values of m and n
    % Using the square braces concatenates everything together

    fprintf('Evaluating the integral of ')
    fprintf(functionString)
    fprintf(' over the interval 0<x<1)
    % Nice printout to verify the proper function is being evaluated

    integral = quad(functionString, 0, 1);
     
    When you try to evaluate something, it should give you an answer

    >> junkVariable = quadEvaluator(2,3)

    Evaluating the integral of (x.^2-1).*((1-x).^3-1) over the interval 0<x<1
    junkVariable = 0.4333

    In the future, it would also help if you posted what the error message was, instead of just saying that you have an error message.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Integration in Matlab
  1. Integration in Matlab (Replies: 1)

  2. Matlab integral (Replies: 0)

  3. Integration in matlab (Replies: 0)

  4. Integration in matlab (Replies: 3)

Loading...