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

MATLAB quad function

  1. Mar 26, 2009 #1
    I need some help with the quad() function in MATLAB. I've simplified my issue to make the following code (and thus my problem) easier to understand.

    function testint

    %%% Formula
    x = 0:0.001:pi;
    x = [zeros(1,40) x zeros(1,40)];

    y = sin(x);

    Result = quad(@work, min(x), max(x))


    function [a b c q y s] = work(x)
    a = log(x);
    b = cos(x);
    c = 5.*x;
    q = 2.^x;
    y = sin(x);
    s = 2./x;

    The value of "Result" always gives me the first output of work(x) (i.e., a). I need "Result" to give me the value of y (the 5th output of work(x)). How can I manipulate the function handle and/or call to work(x) to get this? Also, I am using work(x) in other locations, so I cannot manipulate the inputs / outputs (or even the order... such as, if I move y to the beginning (i.e., function [y a b c q s] = work(x) ... end) it works perfectly).

    Thanks for any suggestions!
  2. jcsd
  3. Mar 28, 2009 #2


    User Avatar
    Science Advisor

    This might be crazy, but why not just take the integral between zero and pi?

    >> quad('sin(x)', 0, pi)

    I have no idea why you're zero padding here, but if you insist on using it, you could use the trapz trapezoidal integration rule. However, because you use the X vector as an input (and it subtracts x_(n-1) from x_n to determine the trapezoid base, the contribution of these will be zero (unless you have some infs or NaNs)

    So it'd come after your function work(x), you'd have to call work(x) to populate the variables, and then you'd do the trapz thing.

    >> trapz(y, x)

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - MATLAB quad function Date
Matlab Coding angular spectrum of waves in a fluid Yesterday at 7:30 AM
How to program this in Matlab Feb 28, 2018
Matlab Making a short test for self-adjointness Feb 23, 2018
MATLAB quad and difficulty with vector input Nov 22, 2010