# Assignment: Type a symbolic code (MATLAB) to verify one of the centroids and are

Assignment: Type a symbolic code (MATLAB) to verify one of the centroids and areas given in the “Centroids of common shapes of areas and lines” table:
EXPLANATION:
Takes three points and solves for the coefficients of a quadratic equation through the three points.
Search Matlab documentation for “Systems of Linear Equations” for methods to solve systems of equations.
Integrate this quadratic equation between two given x values.
Vertical strips are used for the integration
Integrating x times the quadratic equation to get the first moment of area with respect to the y axis.
Compute the x centroid of the area under the curve.
Integrating y/2 times the quadratic equation to get the first moment of area with respect to the x axis.
Compute the y centroid of the area under the curve.
Graph the area under the curve, the three given points and the x location of the centroid.
CODE:
%% This script calculates a quadratic function and it’s x-centroid
% The quadratic function that goes through the three points given
% is solved.
% The quadratic equation is then integrated between two given x values
% along with the integral of x times the function. These integrals
% are then used to calculate the X centroid of the area between
% the function and the x-axis.
% Givens:
% Enter three points as P = [ x1,y1 ; x2,y2; x3,y3 ];
P = [ -1, 2 ; 0, pi ; 3, 1 ];
% Enter the end points for integration, x0 and x1
x0 = 0;
x1 = 3;
% Solution:
% Define symbolic variables
syms x a b c
% Define three equations for a quadratic at the three given points
X = P(:,1); % extract the given x values from P
Y = P(:,2); % extract the given y values from P
Eq = Y == a .* X .* X + b .* X + c; % Eq is an array of equations
% Note the ‘==’ makes an equation, left side equals right side
% Solve for the a,b and c coeficients
sol = solve(Eq,[a,b,c]); % ‘solve’ will solve a system of equations
% The solution is a matlab structure where each variables solution/s
% are accessed with sol.a, sol.b or sol.c
% Define the quadratic equation using the constants
quad = @(x) sol.a .* x .^ 2 + sol.b .* x + sol.c;
% note use of .* and .^ to operate on coresponding array elements
% the @(x) defines a function that takes x as an independent variable
% It is used as quad(independent variable)
% Integrate the quadratic equation times x
% Solve for the centroid
xbar = XArea/Area;
fprintf(“The quadratic equation through the three given points is : “);
% make a text of the equation to use in the plot also
equationText = sprintf(“y = %.3g x^2 %+.3g x %+.3g”,sol.a,sol.b,sol.c);
fprintf(“%s “,equationText);
% When using a script the ‘ ‘ adds a new line
fprintf(“The area between the function and the x axis is %.3g “,Area);
fprintf(“The x value of the areas centroid is %.3g”,xbar);
% Calculate variables to plot between x0 and x1
xplot = linspace(x0,x1,50); % array of x values to plot
yplot = quad(xplot); % array of y values to plot
% Plot the function shading the area between the function and
% the x axis
plot(xplot, yplot,’-‘,”LineWidth”,3);
% Give the plot a title
% Label the axes ‘x’ and ‘y’
xlabel(“x”);
ylabel(“y”);
% prepare the plot for further additions
hold on;
% Shade the area below the curve
area(xplot,yplot,”FaceColor”,’y’);
% Add the three given points to the graph
plot(X,Y,’o’);
% Add a vertical line at the x centroid
yl = ylim(); % get the y limits
xb = [xbar,xbar]; % set to x values for the line
plot(xb,yl,’–‘); % graph the line