Solving quad() Function in MATLAB

In summary, the individual is asking for help with the quad() function in MATLAB and wants to manipulate the function handle and/or call to work(x) in order to get the value of y as the output instead of a. They also mention using the trapz function as a possible solution.
  • #1
StAndrews
1
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
  • #2
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
 
  • #3


I understand your frustration with the quad() function in MATLAB. It can be tricky to use at times, especially when dealing with multiple outputs from a function. In this case, it seems like you are not getting the desired output because the quad() function is only returning the first output of your work(x) function, which is a.

To get the value of y (the 5th output of work(x)), you can use the following syntax:

[a, b, c, q, y, s] = work(x); % this will assign all the outputs of work(x) to separate variables

Result = quad(@work, min(x), max(x), [], [], [], y); % this will use the y output of work(x) as the integration limits for quad()

Alternatively, you can also use the following syntax to specify the desired output as the integration limits:

Result = quad(@work, min(x), max(x), [], [], [], 5.*x); % this will use the c output of work(x) as the integration limits for quad()

I hope this helps you solve your issue with the quad() function in MATLAB. Keep in mind that there may be other ways to approach this problem, so feel free to explore different options. Good luck!
 

1. How do I use the quad() function in MATLAB?

The quad() function in MATLAB is used to solve definite integrals numerically. To use it, you need to specify the function to be integrated, the lower and upper limits of integration, and any additional parameters. The syntax is: quad(fun, a, b), where fun is the function handle, and a and b are the lower and upper limits, respectively.

2. What is the difference between quad() and quadl() in MATLAB?

The main difference between quad() and quadl() in MATLAB is that quadl() is used for integrands that are either singular or oscillatory at the endpoints. It uses a different algorithm than quad() to handle these types of integrands and can provide more accurate results in these cases. Otherwise, they both work in a similar way and have the same syntax.

3. How do I handle multiple integrals with quad() in MATLAB?

To handle multiple integrals with quad(), you can use nested functions. This means that you can use the result of one quad() function as the input for another quad() function. For example, if you have a double integral, you can use two nested quad() functions to solve it. Make sure to specify the correct order of integration and to use different variables for the inner and outer functions.

4. Can I integrate complex-valued functions with quad() in MATLAB?

Yes, you can integrate complex-valued functions with quad() in MATLAB. However, you need to make sure to use the correct syntax. Instead of using a regular function handle, you need to use an anonymous function to specify the complex-valued function. For example, the syntax would be: quad(@(x) f(x), a, b), where f(x) is the complex-valued function and a and b are the lower and upper limits, respectively.

5. How do I improve the accuracy of quad() in MATLAB?

To improve the accuracy of quad() in MATLAB, you can use the optional tol parameter to specify the desired tolerance level. The default tolerance is 1e-6, but you can adjust it to a smaller value for more accurate results. Additionally, you can also try using the quadl() function instead, which may provide more accurate results for certain types of integrands.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
505
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
Back
Top