How do I integrate a continuous function in MATLAB

Many students ask me how do I do this or that in MATLAB.  So I thought why not have a small series of my next few blogs do that.  In this blog I show you how to integrate a continuous function.

The MATLAB program link is here.

The HTML version of the MATLAB program is here.

___________________________________________

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I integrate a continuous function?

%% SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://nm.mathforcollege.com/blog/integration.m;
% Last Revised : March 28, 2009;
% Abstract: This program shows you how to integrate a given function.
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to integrate’)
disp(‘   a given function ‘)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/integration.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   March 29, 2009’)
disp(‘ ‘)

%% INPUTS

% Integrate exp(x)*sin(3*x) from x=2.0 to 8.7
% Define x as a symbol
syms x
% Assigning the function to be differentiated
y=exp(x)*sin(3*x);
% Assigning the lower limit
a=2.0;
% Assigning the upper limit
b=8.7;

%% DISPLAYING INPUTS

disp(‘INPUTS’)
func=[‘  The function is to be integrated is ‘ char(y)];
disp(func)
fprintf(‘  Lower limit of integration, a= %g’,a)
fprintf(‘\n  Upper limit of integration, b= %g’,b)
disp(‘  ‘)
disp(‘  ‘)

%% THE CODE

% Finding the integral using the int command
% Argument 1 is the function to be integrated
% Argument 2 is the variable with respect to which the
%    function is to be integrated – the dummy variable
% Argument 3 is the lower limit of integration
% Argument 4 is the upper imit of integration
intvalue=int(y,x,a,b);
intvalue=double(intvalue);

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)
fprintf(‘  Value of integral is = %g’,intvalue)
disp(‘  ‘)

_________________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

How do I differentiate in MATLAB?

Many students ask me how do I do this or that in MATLAB.  So I thought why not have a small series of my next few blogs do that.  In this blog, I show you how to differentiate a function.

The MATLAB program link is here.

The HTML version of the MATLAB program is here.

___________________________________________

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I differentiate a function?

%% SUMMARY
% Language : Matlab 2008a
% Authors : Autar Kaw
% Mfile available at
% http://nm.mathforcollege.com/blog/differentiation.m
% Last Revised : March 21, 2009
% Abstract: This program shows you how to differentiate a given function

%% INTRODUCTION
clc
clear all
disp(‘ABSTRACT’)
disp(‘   This program shows you how to differentiate’)
disp(‘   a given function and then find its value’)
disp(‘   at a given point’)
disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar K Kaw of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/differentiation.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   March 21, 2009’)
disp(‘ ‘)

%% INPUTS

% Differentiate 7 exp(3*x) once and find the value of the
% first derivative at x=0.5
% Define x as a symbol
syms x
% Defining the function to be differentiated
y=7*exp(3*x);
% Defining the point where you want to find the derivative
xx=0.5;

%% DISPLAYING INPUTS
disp(‘INPUTS’)
func=[‘  The function is to be differentiated is ‘ char(y)];
disp(func)
fprintf(‘  Value of x where you want to find the derivative, x= %g’,xx)
disp(‘  ‘)
disp(‘  ‘)

%% THE CODE
% Finding the derivative using the diff command
% Argument 1 is the function to be differentiated
% Argument 2 is the variable with respect to which the
%    function is to be differentiated – the independent variable
% Argument 3 is the order of derivative
dydx=diff(y,x,1);
% subs command substitues the value of x
dydx_val=subs(dydx,x,xx);
%% DISPLAYING OUTPUTS
disp(‘OUTPUTS’)
derivative_func=[‘  The derivative of function ‘ char(y) ‘ is ‘ char(dydx)];
disp(derivative_func)
fprintf(‘  Value of dydx at x=%g is =%g’,xx,dydx_val)
disp(‘  ‘)

______________________________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Numerical Methods YouTube Video Progress

Since January 2009, I have been videotaping numerical methods course lectures in the Educational Outreach studio of University of South Florida, Tampa. Videos are made in 10-minute segments, not just because that is the limit of the length of YouTube videos, but because we firmly believe in making our resources pedagogically neutral. Ten minute videos allow an instructor to pick and choose what, when and how he or she wants the students to learn.

But some people have asked me – Why YouTube – why not put the same 10-minute videos on your own website. We do have the links to the YouTube videos on our own website, but there are many good reasons to go the YouTube route. The list of reasons below may be obvious to some, while others may not have thought about some of them.

1. Use YouTube’s storage space for the videos.

2. Use YouTube’s compression technology to make the videos stream faster on slow connections.

3. Use the power of Google and YouTube to tag and search the videos.

4. Use YouTube’s bandwidth as opposed to that of my school. My school’s IT department most probably would start screaming when the downloads pick up pace.

5. Use YouTube’s editing facilities to add annotations, links, and play lists.

6. Use the ubiquity of YouTube to reach a large audience.

7. Simple one stop process to let others embed the videos on their website.

8. Have open discussion on the videos via comments.

9. Get the videos rated so that we can judge their quality.

10. Use the “insight” tool of YouTube to analyze who is watching the videos.

______________________________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

MATLAB code for the efficient automatic integrator

In the previous post, we discussed why doubling the number of segments in the automatic integrator based on multiple-segment trapezoidal rule is more efficient than increasing the number of segments one at a time. But this advantage involves having to store the individual function values from previous calculations and then having to retrieve them properly. This drawback was circumvented very efficiently by using the formula derived in another previous post where there is no need to store individual function values.

The matlab file for finding a definite integral by directly using the multiple segment trapezoidal rule from this post is given here (matlab file, html file), while the matlab file that uses the more efficient formula from this post is given here (matlab file, html file).  Here are the inputs to the programs.

% a = Lower limit of integration
% b = Upper limit of integration
%  nmax = Maximum number of segments
% tolerance = pre-specified tolerance in percentage
% f = inline function as integrand

a=5.3;
b=10.7;
nmax=200000;
tolerance=0.000005;
f=inline(‘exp(x)*sin(2*x)’)

We ran both the program on a PC and found that the more efficient algorithm (51 seconds) ran in half the time as the other one (82 seconds).  This is expected, as only n function evaluations are made for 2n-segments rule with the efficient formula, while 2n+1 functions evaluations are made for the original formula.

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://www.youtube.com/numericalmethodsguy.

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.