Numerical integration: Difference between revisions
Content added Content deleted
(→{{header|MATLAB}}: Changed the solution to conform with the task specs and added a more detailed example of how to use the solution functions) |
|||
Line 1,102: | Line 1,102: | ||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
For all of the examples given, the function that is passed to the method as parameter f is a function handle. |
|||
f.m (The funtion you are wanting to approximate an integral too) |
|||
<lang MATLAB> |
|||
function f=f(x) |
|||
f=x; |
|||
⚫ | |||
Function for performing left rectangular integration: leftRectIntegration.m |
|||
Numerical_Integration.m |
|||
<lang MATLAB> |
<lang MATLAB>function integral = leftRectIntegration(f,a,b,n) |
||
function Numerical_Integration(a,b,n) |
|||
⚫ | |||
h=(b-a)/n; |
|||
⚫ | |||
LftRecRl=h.*sum(f(x1(1:n))) |
|||
⚫ | |||
⚫ | |||
width = (b-a)/n; %calculate the width of each devision |
|||
RgtRecRl=h.*sum(f(x2(2:n+1))) |
|||
⚫ | |||
integral = width * sum( f(x(1:n-1)) ); |
|||
⚫ | |||
Function for performing right rectangular integration: rightRectIntegration.m |
|||
⚫ | |||
<lang MATLAB>function integral = rightRectIntegration(f,a,b,n) |
|||
⚫ | |||
format long; |
|||
⚫ | |||
width = (b-a)/n; %calculate the width of each devision |
|||
TrpRl=h.*sum((f(x3(1:n))+f(x4(2:n+1)))/2) |
|||
⚫ | |||
integral = width * sum( f(x(2:n)) ); |
|||
⚫ | |||
Function for performing mid-point rectangular integration: midPointRectIntegration.m |
|||
x5=linspace(a,b,n+1); |
|||
<lang MATLAB>function integral = midPointRectIntegration(f,a,b,n) |
|||
SmpRl=h/6.*sum(f(x5(1:n))+4*f((x5(1:n)+x5(2:n+1))/2)+f(x5(2:n+1))) |
|||
⚫ | |||
format long; |
|||
width = (b-a)/n; %calculate the width of each devision |
|||
⚫ | |||
⚫ | |||
end</lang> |
|||
Function for performing trapezoidal integration: trapezoidalIntegration.m |
|||
<lang MATLAB>function integral = trapezoidalIntegration(f,a,b,n) |
|||
format long; |
|||
⚫ | |||
integral = trapz( x,f(x) ); |
|||
end</lang> |
|||
Simpson's rule for numerical integration is already included in MATLAB as "quad()". It is not the same as the above examples, instead of specifying the amount of points to divide the x-axis into, the programmer passes the acceptable error tolerance for the calculation (parameter "tol"). |
|||
<lang MATLAB>integral = quad(f,a,b,tol)</lang> |
|||
{{header|Sample inputs}} |
|||
Using anonymous functions |
|||
<lang MATLAB>trapezoidalIntegration(@(x)( exp(-(x.^2)) ),0,10,100000) |
|||
ans = |
|||
0.886226925452753</lang> |
|||
Using predefined functions |
|||
Built-in MATLAB function sin(x): |
|||
<lang MATLAB>quad(@sin,0,pi,1/1000000000000) |
|||
ans = |
|||
2.000000000000000</lang> |
|||
User defined scripts and functions: |
|||
fermiDirac.m |
|||
<lang MATLAB>function answer = fermiDirac(x) |
|||
k = 8.617343e-5; %Boltazmann's Constant in eV/K |
|||
answer = 1./( 1+exp( (x)/(k*2000) ) ); %Fermi-Dirac distribution with mu = 0 and T = 2000K |
|||
end</lang> |
|||
<lang MATLAB> rightRectIntegration(@fermiDirac,-1,1,1000000) |
|||
ans = |
|||
0.999998006023282</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |