Solving quad() Function in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter StAndrews
  • Start date Start date
  • Tags Tags
    Function Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 25K views
StAndrews
Messages
1
Reaction score
0
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
clc;

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

y = sin(x);

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

end

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;
end

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!
 
Physics news on Phys.org
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)

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html