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

  • Context: MATLAB 
  • Thread starter Thread starter ruby_duby
  • Start date Start date
  • Tags Tags
    Integration Matlab
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MATLAB function intended to compute an integral using the quad function. Participants are addressing issues related to the function's definition and usage, particularly how to properly implement variable parameters in the integration process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a MATLAB function B(m,n) intended to compute an integral but encounters issues when running the m-file.
  • Another participant identifies two main problems: the function is defined after its call to the quad function, and it is improperly defined in terms of m and n instead of x.
  • A participant expresses frustration with MATLAB and requests further guidance, indicating difficulty in understanding the implementation.
  • Further advice is given to hardcode the function in terms of x rather than m and n, emphasizing that quad substitutes values only for x.
  • A suggested alternative function, quadEvaluator(m,n), is provided, which constructs the function string dynamically using m and n, allowing for proper evaluation of the integral.
  • Participants emphasize the importance of sharing specific error messages to facilitate troubleshooting.

Areas of Agreement / Disagreement

Participants generally agree on the need to redefine the function in terms of x for proper integration. However, there is no consensus on the best approach to resolve the initial issues, as some participants continue to struggle with the implementation.

Contextual Notes

Limitations include the need for clearer definitions of m and n before calling the quad function, as well as the requirement for the function to be defined correctly within the m-file structure.

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
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
9K
Replies
2
Views
3K