how to calculate centroid of an area of a line and parabola in matlab

To calculate the centroid of an area bounded by a line and a parabola in MATLAB, we need to follow these steps:

  1. Define the line and parabola using their equations. Let's assume the line is given by y = mx + b and the parabola is given by y = ax^2 + bx + c.
main.m
m = 2;          % Slope of the line
b = 5;          % y-intercept of the line
a = -1/4;       % Coefficient of x^2 in parabola equation
b_parabola = 3; % Coefficient of x in parabola equation
c = 2;          % Constant term in parabola equation

% Define the line and parabola as anonymous functions
f_line = @(x) m*x + b;
f_parabola = @(x) a*x.^2 + b_parabola*x + c;
368 chars
10 lines
  1. Find the x-coordinates of the intersection points of the line and parabola. We can do this by solving the equation f_line(x) = f_parabola(x), which gives us a quadratic equation that we can solve using the quadratic formula. In MATLAB, we can do this using the roots function.
main.m
% Find the x-coordinates of the intersection points
a_quad = a;        % Coefficient of x^2 term
b_quad = b_parabola - m; % Coefficient of x term
c_quad = c - b;    % Constant term
x_intersect = roots([a_quad b_quad c_quad]);
226 chars
6 lines
  1. Calculate the areas of the two parts of the bounded region, one above the line and one below.
main.m
% Calculate the areas of the two parts of the bounded region
area_above = quadgk(@(x) f_parabola(x) - f_line(x), x_intersect(1), x_intersect(2));
area_below = quadgk(@(x) f_line(x) - 0, x_intersect(1), x_intersect(2));
219 chars
4 lines

Here, we have used the quadgk function to numerically integrate the functions that describe the areas above and below the line. The limits of integration are the x-coordinates of the intersection points.

  1. Calculate the x-coordinate of the centroid using the formula:

x_c = (1/A) * (integral of x * f(x) dx over the region)

where A is the total area of the region and f(x) is the height of the region at x. In MATLAB, we can do this using the quadgk function again, where we replace the f(x) with the x distance from the y-axis to the curve at x.

main.m
% Calculate the x-coordinate of the centroid
x_dist = @(x) abs(x - 0);  % The distance from the y-axis to the line is always x
x_c_above = (1/area_above) * quadgk(@(x) x_dist(x) * (f_parabola(x) - f_line(x)), x_intersect(1), x_intersect(2));
x_c_below = (1/area_below) * quadgk(@(x) x_dist(x) * f_line(x), x_intersect(1), x_intersect(2));
x_c = (area_above * x_c_above + area_below * x_c_below) / (area_above + area_below);
424 chars
6 lines
  1. Calculate the y-coordinate of the centroid using the formula:

y_c = (1/A) * (integral of (1/2) * [f(x)]^2 dx over the region)

In MATLAB, we can do this using the quadgk function one last time.

main.m
% Calculate the y-coordinate of the centroid
y_c_above = (1/area_above) * quadgk(@(x) ((f_parabola(x) - f_line(x))/2).^2, x_intersect(1), x_intersect(2));
y_c_below = (1/area_below) * quadgk(@(x) (f_line(x)/2).^2, x_intersect(1), x_intersect(2));
y_c = (area_above * y_c_above + area_below * y_c_below) / (area_above + area_below);

% The centroid is (x_c, y_c)
centroid = [x_c, y_c];
385 chars
8 lines

gistlibby LogSnag