MATLAB Resolve Integration in Matlab with B(m,n) Function | Troubleshooting Tips

  • Thread starter Thread starter ruby_duby
  • Start date Start date
  • Tags Tags
    Integration Matlab
Click For Summary
SUMMARY

The forum discussion addresses issues with integrating a function in MATLAB using the quad function. The user initially defines the function B(m,n) incorrectly, leading to errors when attempting to evaluate it. Key solutions include defining the function in terms of x and using a string to construct the function dynamically with the correct values for m and n. The corrected approach involves creating a new function, quadEvaluator(m,n), which successfully evaluates the integral of (x.^m-1).*((1-x).^n-1) over the interval from 0 to 1.

PREREQUISITES
  • Understanding of MATLAB function syntax
  • Familiarity with numerical integration concepts
  • Knowledge of function handles in MATLAB
  • Basic understanding of MATLAB's quad function
NEXT STEPS
  • Learn about MATLAB function handles and their proper usage
  • Explore MATLAB's quad function documentation for advanced integration techniques
  • Investigate dynamic function creation in MATLAB using strings
  • Practice defining and using nested functions in MATLAB
USEFUL FOR

Mathematics students, MATLAB users, and software developers looking to perform numerical integration and troubleshoot function definitions in MATLAB.

ruby_duby
Messages
46
Reaction score
0
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 doesn't work. Can anybody help me please
 
Physics news on Phys.org
ruby_duby said:
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 doesn't work. Can anybody help me please

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
 
thanks so much for your help but I still can't 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 I am doing so any guidance is really appreciated
 
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:
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.
 

Similar threads

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