In: Advanced Math
I need to solve math problem by using freemat or mathlab
program. However, I can't understand how to make a code it.
This is one example for code ( freemat )
format long
f = @(x) (x^2) % define f(x) = x^2
a=0
b=1
N=23
dx = (b-a)/N % dx = delta_x
1. Methods:
(a) Left Rectangular Rule also known as Lower sum.
(b) Right Rectangular Rule also know as upper sum.
(c) Midpoint Rule
(d) Trapezoid Rule
(e) Simpson Rule
2. Evaluate
Integral {4/(x^2 + 1)} (Upper bound 1 Lower bound 0)
1. (a) Left Rectangular Rule : The left rectangular rule is the sum given by
for the partition
where
In case of uniform partition size we have
Create a MATLAB function file called Left_Rectangular_Rule.m and paste the code given below.
function L =
Left_Rectangular_Rule(f,a,b,n)
%LEFT_RECTANGULAR_RULE Approximate the integral value using Left
Rectangle
% Rule
% Input:
% f - Function handle of the integrand f(x)
% a - Lower limit of integeration
% b - Upper limit of integeration
% n - Number of partitions of interval [a,b]
% Output:
% L - Left Rectangle Rule Approximation of integral
% Calculate the step size
delta x
dx = (b-a)/n;
% Calculate the Left
Rectangle sum of the partition of [a,b]
L = dx*sum(f(a:dx:b-dx));
end
(b) Right Rectangular Rule : The Right Rectangular Rule is given by the sum
for the partition
where
In case of uniform partition size we have
Create a MATLAB function file called Right_Rectangular_Rule.m and paste the code given below.
function R =
Right_Rectangular_Rule(f,a,b,n)
%Right_RECTANGULAR_RULE Approximate the integral value using Right
Rectangle
% Rule
% Input:
% f - Function handle of the integrand f(x)
% a - Lower limit of integeration
% b - Upper limit of integeration
% n - Number of partitions of interval [a,b]
% Output:
% R - Right Rectangle Rule Approximation of integral
% Calculate the step size
delta x
dx = (b-a)/n;
% Calculate the Right
Rectangle sum of the partition of [a,b]
R = dx*sum(f(a+dx:dx:b));
end
(c) Midpoint Rule : The mid point rule is the sum given by
where
are the mid points of the partitions of the interval
where
In case of uniform partition size we have
Create a MATLAB function file called MidPoint_Rule.m and paste the code given below.
function M =
MidPoint_Rule(f,a,b,n)
%MIDPOINT_RULE Approximate the integral value using Mid point
% Rule
% Input:
% f - Function handle of the integrand f(x)
% a - Lower limit of integeration
% b - Upper limit of integeration
% n - Number of partitions of interval [a,b]
% Output:
% M - Mid Point Rule Approximation of integral
% Calculate the step size
delta x
dx = (b-a)/n;
% Calculate the mid points
of the partitions of [a,b]
m = ((a:dx:b-dx) + (a+dx:dx:b))/2;
% Calculate the Mid Point
Rule sum of the partition of [a,b]
M = dx*sum(f(m));
end
(d) Trapezoidal Rule : The trapezoidal rule is given by the sum
for the partition
where
In case of uniform partition size we have
Create a MATLAB function file called Trapezoidal_Rule.m and paste the code given below.
function T =
Trapezoidal_Rule(f,a,b,n)
%TRAPEZOIDAL_RULE Approximate the integral value using Trapezoidal
Rule
% Input:
% f - Function handle of the integrand f(x)
% a - Lower limit of integeration
% b - Upper limit of integeration
% n - Number of partitions of interval [a,b]
% Output:
% M - Trapezoidal Rule Approximation of integral
% Calculate the step size
delta x
dx = (b-a)/n;
% Calculate the Trapezoidal
Rule sum of the partition of [a,b]
T = (dx/2)*sum(f(a:dx:b-dx)+ f(a+dx:dx:b));
end
2. We can now approximate the integral value
using the 4 functions we have defined.
The results of the approximations are given below.
The exact value of the integral is