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://numericalmethods.eng.usf.edu/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 https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://numericalmethods.eng.usf.edu/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://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/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.

Author: Autar Kaw

Autar Kaw (http://autarkaw.com) is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare (nm.MathForCollege.com) annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.

One thought on “How do I integrate a continuous function in MATLAB”

Leave a comment