MATLAB/Cookbook/symbolicIntegration
< MATLAB < Cookbook
clc;clear all;close all
sectionNumber= input('Enter case you wish to run: ');
switch(sectionNumber)
case 1
syms x real %certain matlab commands don't need semicolon to suppress output
syms a b positive
f=a*x^3*exp(-b*x^2) %symbolically define function to be intetrated
int_f=int(f,x) % *int* integrates f w.r.t. x and shows result
pretty(int_f); %makes result easier to read
case 2
syms x real, syms a b positive
f=a*x^3*exp(-b*x^2); %symbolically define function to be intetrated
int_f=int(f,x,0) % constant of integration allows int_f(0)=0
pretty(int_f); %makes result easier to read
case 3
syms x real %Here we introduce ezplot, which plots symbolic
% functions, but lacks the options available with plot.
f=x^3*exp(-x^2);
int_f=int(f,x)
ezplot(f,[0,3])
title('function (first of two figures)')
% ezplot almost forces us to put the integral on a separate plot.
figure
ezplot(int_f,[0,3])
title('integral (second of two figures)')
case 4
syms x x1 x2 real
syms a b positive
f=x^3*exp(-1*x^2);
int_f=int(f,x,0);
xplot=0:.01:3;
% the next two commands convert symbols to double precision numbers
fplot = double(subs(f, x, xplot));
Sfplot = double(subs(int_f, x, xplot));
plot(xplot, fplot, 'b', xplot, Sfplot, 'r-.');
title(' ');
legend('function','integral');
xlabel('x'); grid;
end
fprintf('To run this code again type *symbolicIntegration*\n' );