Solving quad() Function in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter StAndrews
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

The discussion focuses on resolving an issue with the quad() function in MATLAB, specifically when attempting to retrieve the fifth output, y, from the work(x) function. The user’s current implementation consistently returns the first output, a, instead of the desired y. Suggestions include using the trapz() function for trapezoidal integration as an alternative to quad(), and a recommendation to directly integrate sin(x) over the interval from 0 to π.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of function handles in MATLAB
  • Knowledge of numerical integration techniques, specifically quad() and trapz()
  • Basic understanding of mathematical functions such as sine and logarithm
NEXT STEPS
  • Learn how to use MATLAB function handles effectively
  • Research the differences between quad() and trapz() for numerical integration
  • Explore MATLAB's documentation on the trapz() function for practical examples
  • Investigate how to manipulate output arguments in MATLAB functions
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those involved in numerical analysis, mathematical modeling, and anyone seeking to improve their understanding of function outputs and integration methods in MATLAB.

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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K