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

Area = int(quad,x,x0,x1);

% Integrate the quadratic equation times x

XArea = int(x*quad,x,x0,x1);

% Solve for the centroid

xbar = XArea/Area;

% Answer :

% Print the quadratic equation

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);

% Plot the quadratic equation

% 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

title(“Quadratic Equation through three points”);

% 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

text(xbar,ycenter/2,’Centroid ‘,’HorizontalAlignment’,’right’); % add

label

% Add the quadratic equation to the center of the graph

xcenter = (x0+x1)/2; % the center of the integral

ycenter = (max(yplot) + min(yplot))/2; % the center of the yplot values

text(xcenter,ycenter, equationText,…

‘HorizontalAlignment’,’center’);

% end the graph

hold off;

**DO NOT COPY THIS CODE OR JUST EXPLAIN IT. LOOKING FOR NEW CODE WHICH SOLVES A FORMULA FROM THE TABLE!!!!!!**

- Attachment 1
- Attachment 2