Difference between revisions of "Numerical differentiation and integration"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m
Line 20: Line 20:
pdf_file          = E-Numerical-integration-08-Nov-2010.pdf
pdf_file          = E-Numerical-integration-08-Nov-2010.pdf
</pdfreflow>
</pdfreflow>
=== Source code used in slides ===
{| class="wikitable"
|-
! MATLAB
! Python
|-
| width="50%" valign="top" |
<syntaxhighlight lang="matlab">
function In = trapezcomp( f, a, b, n )
% INITIALIZATION
h = (b-a)/n;
x = a;
% COMPOSITE RULE
In =f(a);
for k=2:n
    x  = x + h;
    In = In + 2. * f(x);
end
In = ( In + f(b) ) * h * 0.5;
% end of function
</syntaxhighlight>
Then use this function as follows:
<syntaxhighlight lang="matlab">
>> f = @(x)sin(x);
>> I1 = trapezcomp( f, 0, pi/2, 1 )    % 0.7854 
>> I2 = trapezcomp( f, 0, pi/2, 2 )    % 0.9481
</syntaxhighlight>
| width="50%" valign="top" |
<syntaxhighlight lang="python">
</syntaxhighlight>
|}

Revision as of 13:15, 10 November 2010

Part A: Numerical differentiation

<pdfreflow> class_date = 01 to 04 November button_label = Create my course notes! show_page_layout = 1 show_frame_option = 1 pdf_file = E-Numerical-differentiation-01-Nov-2010.pdf </pdfreflow>


Part B: Numerical integration

<pdfreflow> class_date = 08 to 15 November button_label = Create my course notes! show_page_layout = 1 show_frame_option = 1 pdf_file = E-Numerical-integration-08-Nov-2010.pdf </pdfreflow>

Source code used in slides

MATLAB Python
function In = trapezcomp( f, a, b, n )

% INITIALIZATION
h = (b-a)/n;
x = a;

% COMPOSITE RULE
In =f(a);
for k=2:n
    x  = x + h;
    In = In + 2. * f(x);
end
In = ( In + f(b) ) * h * 0.5;
% end of function

Then use this function as follows:

>> f = @(x)sin(x);
>> I1 = trapezcomp( f, 0, pi/2, 1 )    % 0.7854  
>> I2 = trapezcomp( f, 0, pi/2, 2 )    % 0.9481