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.

Contents

TOPIC

How do I integrate a mathematical function

SUMMARY

Language : Matlab 2010a; Authors : Autar Kaw and Sri Harsha Garapati; Mfile available at Last Revised : January 23, 2012; Abstract: This program shows you how to integrate a mathematical function

clc
clear all

INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you how to integrate a mathematical expression')

disp(' ')
disp('AUTHOR')
disp('   Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/calc_operations_integration_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 23, 2012')
disp(' ')
ABSTRACT
   This program shows you how to integrate a mathematical expression
 
AUTHOR
   Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com
 
MFILE SOURCE
   http://numericalmethods.eng.usf.edu/blog/calc_operations_integration_blog.m
 
LAST REVISED
   January 23, 2012
 

INPUTS

% Inputs
% x is symbolic
syms x
% enter the equation
eqn1=exp(4*x)*cos(x);
% enter the lower limit of integration
a=2;
% enter the upper limit of integration
b=8;

DISPLAYING INPUTS

disp('  ')
disp('INPUTS')
% convert the eqn to a string
eqn1d=char(eqn1);
% printing the input equation
fprintf('  y = %s\n',eqn1d)
% printing the input number of lower limit of integration
fprintf('  The lower limit of integration is %g\n',a)
% printing the input number of upper limit of integration
fprintf('  The upper limit of integration is %g\n',b)
  
INPUTS
  y = exp(4*x)*cos(x)
  The lower limit of integration is 2
  The upper limit of integration is 8

THE CODE

% definite integration
eint =int(eqn1,x,a,b);
% convert to string for display
eintd=char(vpa(eint,7));

DISPLAYING OUTPUTS AND PLOTS

disp('  ')
disp('OUTPUTS')
% printing the output
fprintf('  The integral of the function from %g to %g is %s\n\n',a,b,eintd)
  
OUTPUTS
  The integral of the function from 2 to 8 is 1892130000000.0