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
AI Thread Summary
The discussion centers around issues with a MATLAB function intended to compute the integral of a specific mathematical expression. The user initially defines the function B(m,n) but encounters errors when attempting to run it. Key problems identified include the incorrect definition of the function, which should be in terms of the variable x rather than m and n, and the improper use of the 'quad' function. Suggestions for resolution emphasize hardcoding the function to use specific values for m and n, and creating a new function, quadEvaluator, that constructs the function string dynamically. This approach allows for the correct evaluation of the integral over the specified interval. The importance of providing error messages for troubleshooting is also highlighted for better assistance in resolving issues.
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.
 
Back
Top