Fixing Problems Integrating Function in MATLAB

In summary, the user is having difficulty integrating a function in MATLAB. They ask for help and provide a code block. The code block includes a function, a detailled explanation of what the function does, and a command line for running the function. However, when the user tries to run the function, they get an error. The user then provides a new code block that includes a function and a detailed explanation of what the function does. This new code block works, but the user notes that they may need to change the units of the function and then convert the result to avoid round off errors.
  • #1
Sarah1287
4
0
Hey I'm having problems trying to integrate my function func1 in MATLAB. Really don't know where I am going wrong so would really appreciate if anyone could help. The code I'm using is below, thanks:

Function Mfile:

function [ f1 ] = func1( E, Delta, kB, Temp )
%func1: calculates integrand for eqn 1
% Detailed explanation goes here
f1=(tanh(sqrt(((E*E)+(Delta*Delta))))/(2*kB*Temp))/(2*(sqrt(((E*E)+(Delta*Delta)))));
end

Command line code:

hbar=1.054E-34; Tdebye=420; h=6.626E-34; kB1= 1.38065E-23;
fdebye=(Tdebye*kB1)/h;
Temp1=0.5;
Delta1=0;
a=hbar*fdebye;
Q=quad(@(E1)func1(E1,0,1.38065E-23,0.5),-a,a)
? Undefined function or variable 'kB'.

? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> func1 at 4
f1=(tanh(sqrt(((E*E)+(Delta*Delta))))/(2*kB*Temp))/(2*(sqrt(((E*E)+(Delta*Delta)))));

Error in ==> @(E1)func1(E1,0,1.38065E-23,0.5)


Error in ==> quad at 77
y = f(x, varargin{:});


Thanks
 
Physics news on Phys.org
  • #2


quad needs the function it's integrating to accept a vector argument. In other words, in your anonymous function, E1 will be a vector. E1*E1 will cause an error because you can't multiply to row matrices together. If you want to do it element by element, you need to do E1.*E1 (note the period). Similarly with dividing. If you want to divide element by element, you need to use ./ instead of just /
 
  • #3


Thankyou. I have actually changed that now but I am getting a new error:

? Index exceeds matrix dimensions.

Error in ==> quad at 85
if ~isfinite(y(7))


new function code:

function [ f1 ] = func1( E, Delta, kB, Temp )
%func1: calculates integrand for eqn 1
% Detailed explanation goes here
f1=(tanh(sqrt(((E.*E)+(Delta.*Delta))))/(2.*kB.*Temp))/(2.*(sqrt(((E.*E)+(Delta.*Delta)))));
end


Sorry but you couldn't tell me where I've gone wrong this time could you?

Thankyou very much
 
  • #4


You are still missing one period in dividing:
Code:
f1 = tanh(sqrt(E.*E+Delta*Delta))/(2.*kB.*Temp)./(2.*sqrt(E.*E + Delta*Delta));
I took out all those extra parenthesis you put in. Matlab follows standard order of operations. However, now you will get NaN's. To be sure, this is the function that Matlab thinks you are integrating:
[tex]
\frac{\tanh \sqrt{E^2 + \delta^2}}{2k_BT} \frac{1}{2\sqrt{E^2+\delta^2}}
[/tex]
If that's not right, you need to fix the parenthesis and stuff. The expression you had and the one I put without the extra parenthesis are the same.

If that is right, then perhaps it's because you are dealing with numbers so small (a is of the order 10^-22) and Matlab doesn't really like dealing numbers smaller than 10^-16. It will be better to change units and then convert at the end to avoid round off errors.
 
  • #5


Thanks yes that does fix it. Also the NAN answers are only returned when delta is set to zero since the computer then thinks that when E is zero too it is dividing by zero. If i put an extra thing into avoid that it should work fine. Thanks a lot for your help.
 

1. What is the purpose of integrating functions in MATLAB?

The purpose of integrating functions in MATLAB is to calculate the area under a curve or the volume under a surface. This is useful in many scientific and engineering applications, such as calculating the work done by a force or finding the center of mass of an object.

2. How do I fix errors that occur while integrating functions in MATLAB?

If you encounter errors while integrating functions in MATLAB, you can try using a more accurate integration method, such as the trapezoidal rule or Simpson's rule. You can also check your input values and make sure they are correct. If the errors persist, you may need to revise your code or consult MATLAB's documentation for further assistance.

3. Can I integrate multiple functions at once in MATLAB?

Yes, you can integrate multiple functions at once in MATLAB by using the "arrayfun" function. This allows you to apply the integration function to each element in an array of functions and obtain the results in a single output.

4. How do I plot the results of integrating a function in MATLAB?

To plot the results of integrating a function in MATLAB, you can use the "plot" function and specify the x and y values to be plotted. You can also use the "fplot" function, which automatically generates a plot for a given function over a specified interval.

5. Can I use symbolic integration in MATLAB?

Yes, MATLAB has a Symbolic Math Toolbox that allows you to perform symbolic integration. This means that instead of numerical approximations, the exact solution of the integration problem can be obtained. However, the symbolic integration may be slower and less accurate for complex functions compared to numerical integration methods.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Replies
19
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
16K
  • Advanced Physics Homework Help
Replies
6
Views
4K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
Back
Top