Use Matlab to evaluate the integral ∫x^2dx from 0 to 1 using successive summation.
Use discrete trapezoids to compute integral (do not use automated commands).
clc;clear all
format long
integ=@(x) x.^2;
a=input('Enter the lower limit');
b=input('Enter the upper limit');
n=input('Enter the subdivisions(>1)');
ans1=trapezoidal(integ,a,b,n);
fprintf('Value of the integral from a=%d to b=%d using %d equally
spaced interval is %5.6f\n',a,b,n,ans1)
function answert=trapezoidal(f,a,b,n) %%Function which implements
Trapezoidal rule
I(1)=f(a); I(n+1)=f(b);
h=(b-a)/n;
x=a:h:b;
I(2:n)=2*f(x(2:(length(x)-1)));
answert=0.5*h*sum(I);
end
Get Answers For Free
Most questions answered within 1 hours.