Trapezoidal rule to approximate the integral of x^2
Avoid variable names that collide with built-in functions

sum is a built-in function.  Redefining it causes unconventional behavior:


&gt;&gt; sum([1 2 3])

ans =

     6

&gt;&gt; sum = 0.0

sum =

     0

&gt;&gt; sum([1 2 3]) 
Index exceeds matrix dimensions.

&gt;&gt; clear all
&gt;&gt; sum([1 2 3])

ans =

     6



Idiomatic MATLAB

The whole point of using MATLAB, rather than C, is that the language is designed to work on many values at once.  Therefore, you should avoid looping wherever possible, and let MATLAB do its job on vectors.

% Givens
n = 5                                  % number of subdivisions
a = 1.0                                % x coordinate left endpoint
b = 2.0                                % x coordinate right endpoint

x = linspace(a, b, n)                  % x coordinates of samples
y = x .^ 2                             % y coordinates of samples
sum_y = sum(y) - (y(1) + y(end)) ./ 2  % sum y coordinates, taking just half of endpoints
dx = (b - a) / (n - 1)                 % width of each trapezoid
area = sum_y * dx


Reinventing the wheel

For your information, MATLAB has built-in support for numerical integration:


&gt;&gt; integral(@(x) x .^ 2, 1, 2)

ans =

    2.3333