# Calculating moment of inertia and area of an irregular shape, MATLAB assignment help

Science

I already have a code. I just need to add one more shape (with its graph) that is considered to be irregular. I used an I-beam, but apparently that’s not considered irregular.

Here is the code I have

```% Option #4: Calculate geometric properties of arbitrary 2D shapes_x000D_
_x000D_
function GeometricShapes02()_x000D_
while 1_x000D_
_x000D_
clc_x000D_
_x000D_
_x000D_
fprintf('1. Rectangle or parallelogramn');_x000D_
_x000D_
fprintf('2. Hollow Rectangular Sectionn');_x000D_
_x000D_
fprintf('3. Circular Sectionn');_x000D_
_x000D_
fprintf('4. Hollow Circular Sectionn');_x000D_
_x000D_
fprintf('5. Triangle n');_x000D_
_x000D_
fprintf('6. I-beam n')_x000D_
_x000D_
fprintf('7. Exit nn');_x000D_
_x000D_
x = input('Enter your option [1] ');_x000D_
_x000D_
if isempty(x); x=1; end_x000D_
_x000D_
_x000D_
_x000D_
clc_x000D_
_x000D_
switch x_x000D_
_x000D_
case 1 _x000D_
_x000D_
fprintf('nRectangle or paralleogramn');_x000D_
_x000D_
b = input('Enter the width [8] ');_x000D_
_x000D_
if isempty(b); b=8; end_x000D_
_x000D_
d = input('Enter the height [5] ');_x000D_
_x000D_
if isempty(d); d=5; end_x000D_
_x000D_
Ixx = (b*d^3)/12;_x000D_
_x000D_
Iyy = (d*b^3)/12;_x000D_
_x000D_
A = b*d;_x000D_
_x000D_
DrawRectangle(b,d,A,Ixx,Iyy);_x000D_
_x000D_
fprintf('Area = %.2fn',A);_x000D_
_x000D_
fprintf('Moment of Inertia Ixx = %.2fn',Ixx);_x000D_
_x000D_
fprintf('Moment of Inertia Iyy = %.2fn',Iyy);_x000D_
case 2 _x000D_
_x000D_
fprintf('nHollow Rectangular Sectionn');_x000D_
_x000D_
b = input('Enter the outside width [8] ');_x000D_
_x000D_
if isempty(b); b=8; end_x000D_
_x000D_
d = input('Enter the outside height [5] ');_x000D_
_x000D_
if isempty(d); d=5; end_x000D_
_x000D_
b1 = input('Enter the inner width [6] ');_x000D_
_x000D_
if isempty(b1); b1=6; end_x000D_
_x000D_
d1 = input('Enter the inner height [3] ');_x000D_
_x000D_
if isempty(d1); d1=3; end_x000D_
_x000D_
Ixx = (b*d^3)/12-(b1*d1^3)/12;_x000D_
_x000D_
Iyy = (d*b^3)/12-(d1*b1^3)/12;_x000D_
_x000D_
A = b*d-b1*d1;_x000D_
_x000D_
DrawHollowRectangle(b,d,b1,d1,A,Ixx,Iyy)_x000D_
_x000D_
fprintf('Area = %.2fn',A);_x000D_
_x000D_
fprintf('Moment of Inertia Ixx = %.2fn',Ixx);_x000D_
_x000D_
fprintf('Moment of Inertia Iyy = %.2fn',Iyy);_x000D_
case 3 _x000D_
_x000D_
fprintf('nCircular Sectionn');_x000D_
_x000D_
d = input('Enter the diameter [8] ');_x000D_
_x000D_
if isempty(d); d=8; end_x000D_
_x000D_
Ixx = (pi*d^4)/64;_x000D_
_x000D_
Iyy = Ixx;_x000D_
_x000D_
A = (pi*(d/2)^2);_x000D_
_x000D_
DrawCircle(d,A,Ixx,Iyy);_x000D_
_x000D_
fprintf('Area = %.2fn',A);_x000D_
_x000D_
fprintf('Moment of Inertia Ixx = %.2fn',Ixx);_x000D_
_x000D_
fprintf('Moment of Inertia Iyy = %.2fn',Iyy);_x000D_
case 4_x000D_
_x000D_
fprintf('nHollow circular sectionn');_x000D_
_x000D_
d = input('Enter the inner diameter [5] ');_x000D_
_x000D_
if isempty(d); d=5; end_x000D_
_x000D_
D = input('Enter the outside diameter [10] ');_x000D_
_x000D_
if isempty(D); D=10; end_x000D_
_x000D_
Ixx = (pi/64)*(D^4-d^4);_x000D_
_x000D_
Iyy = Ixx;_x000D_
_x000D_
A = pi*((D/2)^2)-((d/2)^2);_x000D_
_x000D_
DrawHollowCircle(d,D,A,Ixx,Iyy);_x000D_
_x000D_
fprintf('Area = %.2fn',A);_x000D_
_x000D_
fprintf('Moment of Inertia Ixx = %.2fn',Ixx);_x000D_
_x000D_
fprintf('Moment of Inertia Iyy = %.2fn',Iyy);_x000D_
case 5 _x000D_
_x000D_
fprintf('nTrianglen');_x000D_
_x000D_
b = input('Enter the width [8] ');_x000D_
_x000D_
if isempty(b); b=8; end_x000D_
_x000D_
h = input('Enter the height [6] ');_x000D_
_x000D_
if isempty(h); h=6; end_x000D_
_x000D_
Ig = (b*h^3)/36;_x000D_
_x000D_
A = (b*h)/2;_x000D_
_x000D_
DrawTriangle(b,h,A,Ig);_x000D_
_x000D_
fprintf('Area = %.2fn',A);_x000D_
_x000D_
fprintf('Moment of Inertia Ig = %.2fn',Ig);_x000D_
case 6_x000D_
_x000D_
fprintf('nI-Sectionn');_x000D_
_x000D_
d = input('Enter the height [12] ');_x000D_
_x000D_
if isempty(d); d=12; end_x000D_
_x000D_
d1 = input('Enter the inner height [9] ');_x000D_
_x000D_
if isempty(d1); d1=9; end_x000D_
_x000D_
b = input('Enter the width [5] ');_x000D_
_x000D_
if isempty(b); b=5; end_x000D_
_x000D_
b1 = input('Enter the inner width [2] ');_x000D_
_x000D_
if isempty(b1); b1=2; end_x000D_
_x000D_
Ixx = (b*d^3)/12-(b1*d1^3)/12;_x000D_
_x000D_
Iyy = (d*b^3)/12-(d1*b1^3)/12;_x000D_
_x000D_
A = b*(d-d1)+(d1*(b-b1));_x000D_
_x000D_
DrawIbeam(d,d1,b,b1,A,Ixx,Iyy);_x000D_
_x000D_
fprintf('Area = %.2fn',A);_x000D_
_x000D_
fprintf('Moment of Inertia Ixx = %.2fn',Ixx);_x000D_
_x000D_
fprintf('Moment of Inertia Iyy = %.2fn',Iyy);_x000D_
case 7_x000D_
_x000D_
break;_x000D_
otherwise_x000D_
_x000D_
fprintf('Invalid optionn' );_x000D_
_x000D_
end_x000D_
_x000D_
end_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% DrawIbeam_x000D_
_x000D_
% #####################_x000D_
_x000D_
function DrawIbeam(d,d1,b,b1,A,Ixx,Iyy)_x000D_
_x000D_
dx=(b-b1)/2;_x000D_
_x000D_
dy=(d-d1)/2;_x000D_
_x000D_
x(1)=-b/2;          y(1)=-d/2;_x000D_
_x000D_
x(2)=x(1)+b;        y(2)=y(1);_x000D_
_x000D_
x(3)=x(2);          y(3)=y(2)+dy;_x000D_
_x000D_
x(4)=x(3)-dx;       y(4)=y(3);_x000D_
_x000D_
x(5)=x(4);          y(5)=y(1)+d-dy;_x000D_
_x000D_
x(6)=x(2);          y(6)=y(5);_x000D_
_x000D_
x(7)=x(2);          y(7)=y(1)+d;_x000D_
_x000D_
x(8)=x(1);          y(8)=y(7);_x000D_
_x000D_
x(9)=x(1);          y(9)=y(5);_x000D_
_x000D_
x(10)=x(9)+dx;      y(10)=y(5);_x000D_
_x000D_
x(11)=x(10);        y(11)=y(3);_x000D_
_x000D_
x(12)=x(1);         y(12)=y(3);_x000D_
_x000D_
x(13)=x(1);         y(13)=y(1);_x000D_
_x000D_
figure(1); clf_x000D_
_x000D_
set (gcf,'Color','w');_x000D_
_x000D_
subplot(1,2,1);_x000D_
_x000D_
fill(x,y,'c'); hold on_x000D_
_x000D_
title('I-beam','FontSize',11);_x000D_
_x000D_
axis([-b/2 b/2 -d/2 d/2]);_x000D_
_x000D_
xx=0.5*(x(1)+x(2));   Lx=x(2)+2;_x000D_
_x000D_
yy=0.5*(y(1)+y(8));   Ly=y(8)+0.5;_x000D_
_x000D_
daspect([1 1 1]);_x000D_
_x000D_
pbaspect([1 1 1]);_x000D_
_x000D_
grid on_x000D_
subplot(1,2,2);_x000D_
_x000D_
plot(1,1);_x000D_
_x000D_
sb=sprintf('Width: %8.2f',b);_x000D_
_x000D_
sb1=sprintf('Inner width: %8.2f',b1);_x000D_
_x000D_
sd=sprintf('Height: %8.2f',d);_x000D_
_x000D_
sd1=sprintf('Inner height: %8.2f',d1);_x000D_
_x000D_
sA=sprintf('Area: %8.2f',A);_x000D_
_x000D_
sIxx=sprintf('Ixx: %8.2f',Ixx);_x000D_
_x000D_
sIyy=sprintf('Iyy: %8.2f',Iyy);_x000D_
_x000D_
axis([0 10 0 2.5]);_x000D_
_x000D_
dpy=0.2; yp = 2-dpy;_x000D_
_x000D_
text(2,yp,sb); yp=yp-dpy;_x000D_
_x000D_
text(2,yp,sb1); yp=yp-dpy;_x000D_
_x000D_
text(2,yp,sd); yp=yp-dpy;_x000D_
_x000D_
text(2,yp,sd1); yp=yp-dpy;_x000D_
_x000D_
text(2,yp,sA); yp=yp-dpy;_x000D_
_x000D_
text(2,yp,sIxx); yp=yp-dpy;_x000D_
_x000D_
text(2,yp,sIyy); yp=yp;_x000D_
_x000D_
grid off_x000D_
_x000D_
axis off_x000D_
_x000D_
return_x000D_
_x000D_
% #####################_x000D_
_x000D_
% DrawTriangle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function DrawTriangle(b,h,A,Ig)_x000D_
_x000D_
x(1)=-b/2;         y(1)=-h/3;_x000D_
_x000D_
x(2)=x(1)+b;       y(2)=y(1);_x000D_
_x000D_
x(3)=x(1)+0.7*b;   y(3)=y(1)+h;_x000D_
_x000D_
x(4)=x(1);         y(4)=y(1);_x000D_
_x000D_
figure(1); clf_x000D_
_x000D_
set (gcf,'Color','w');_x000D_
_x000D_
subplot(1,2,1);_x000D_
_x000D_
fill(x,y,'c'); hold on_x000D_
_x000D_
title('Triangle','FontSize',11);_x000D_
_x000D_
axis([-b/2 b/2 -h/2 h]);_x000D_
_x000D_
grid on_x000D_
subplot(1,2,2);_x000D_
_x000D_
plot(1,1);_x000D_
_x000D_
sb=sprintf('Width: %8.2f',b);_x000D_
_x000D_
sh=sprintf('Height: %8.2f',h);_x000D_
_x000D_
sA=sprintf('Area: %8.2f',A);_x000D_
_x000D_
sIg=sprintf('Ig: %8.2f',Ig);_x000D_
_x000D_
axis([0 10 0 5]);_x000D_
_x000D_
dpy=0.05; yp = 5-dpy;_x000D_
_x000D_
xp=1;_x000D_
_x000D_
text(xp,yp,sb); yp=yp-0.5;_x000D_
_x000D_
text(xp,yp,sh); yp=yp-0.5;_x000D_
_x000D_
text(xp,yp,sA); yp=yp-0.5;_x000D_
_x000D_
text(xp,yp,sIg); yp=yp-0.5;_x000D_
_x000D_
axis off_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% DrawHollowCircle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function DrawHollowCircle(d,D,A,Ixx,Iyy)_x000D_
_x000D_
r1 = d/2;_x000D_
_x000D_
r2 = D/2;_x000D_
_x000D_
a = 0;_x000D_
_x000D_
b = 0;_x000D_
_x000D_
[xp1,yp1,xp2,yp2,ymn,ymx] = CreateHollowCircle(a,b,r1,r2);_x000D_
figure(1); clf_x000D_
_x000D_
set (gcf,'Color','w');_x000D_
_x000D_
subplot(1,2,1);_x000D_
_x000D_
plot(xp1,yp1,'k-','LineWidth',2); hold on_x000D_
_x000D_
plot(xp2,yp2,'k-','LineWidth',2); hold on_x000D_
_x000D_
grid on_x000D_
_x000D_
axis([-D/2 D/2 -D/2 D/2]);_x000D_
_x000D_
daspect([1 1 1]);_x000D_
_x000D_
pbaspect([1 1 1]);_x000D_
_x000D_
title('Hollow Circle','FontSize',11);_x000D_
subplot(1,2,2);_x000D_
_x000D_
plot(1,1);_x000D_
_x000D_
axis([0 15 0 15]);_x000D_
_x000D_
sd = sprintf('Inner diameter: %8.2f',d);_x000D_
_x000D_
sD = sprintf('Outer diameter: %8.2f',D);_x000D_
_x000D_
sA = sprintf('Area: %8.2f',A);_x000D_
_x000D_
Ixx = sprintf('Ixx: %8.2f',Ixx);_x000D_
_x000D_
Iyy = sprintf('Iyy: %8.2f',Iyy);_x000D_
_x000D_
yp = 15-2.5;_x000D_
_x000D_
text(2,yp,sd);  yp=yp-1;_x000D_
_x000D_
text(2,yp,sD);  yp=yp-1;_x000D_
_x000D_
text(2,yp,sA);  yp=yp-1;_x000D_
_x000D_
text(2,yp,Ixx); yp=yp-1;_x000D_
_x000D_
text(2,yp,Iyy);_x000D_
_x000D_
axis off_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% DrawCircle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function DrawCircle(d,A,Ixx,Iyy)_x000D_
_x000D_
r = d/2;_x000D_
_x000D_
a = 0;_x000D_
_x000D_
b = 0;_x000D_
_x000D_
[xp,yp,ymn,ymx] = CreateCircle(a,b,r);_x000D_
_x000D_
figure(1); clf_x000D_
_x000D_
set (gcf,'Color','w');_x000D_
_x000D_
subplot(1,2,1);_x000D_
_x000D_
plot(xp,yp,'k-','LineWidth',2); grid; hold on_x000D_
_x000D_
grid on_x000D_
_x000D_
axis([-r r -r r]);_x000D_
_x000D_
daspect([1 1 1]);_x000D_
_x000D_
pbaspect([1 1 1]);_x000D_
_x000D_
title('Circle','FontSize',11);_x000D_
subplot(1,2,2);_x000D_
_x000D_
plot(1,1);_x000D_
_x000D_
axis([0 15 0 15]);_x000D_
_x000D_
sb = sprintf('Diameter: %8.2f',d);_x000D_
_x000D_
sA = sprintf('Area: %8.2f',A);_x000D_
_x000D_
Ixx = sprintf('Ixx: %8.2f',Ixx);_x000D_
_x000D_
Iyy = sprintf('Iyy: %8.2f',Iyy);_x000D_
_x000D_
yp = 15-3;_x000D_
_x000D_
text(2,yp,sb);  yp=yp-1;_x000D_
_x000D_
text(2,yp,sA);  yp=yp-1;_x000D_
_x000D_
text(2,yp,Ixx); yp=yp-1;_x000D_
_x000D_
text(2,yp,Iyy);_x000D_
_x000D_
axis off_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% DrawRectangle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function DrawRectangle(b,d,A,Ixx,Iyy)_x000D_
_x000D_
x(1)=-b/2;      y(1)=-d/2;_x000D_
_x000D_
x(2)=x(1)+b;    y(2)=y(1);_x000D_
_x000D_
x(3)=x(2);      y(3)=y(1)+d;_x000D_
_x000D_
x(4)=x(1);      y(4)=y(3);_x000D_
_x000D_
x(5)=x(1);      y(5)=y(1);_x000D_
_x000D_
figure(1); clf_x000D_
_x000D_
set (gcf,'Color','w');_x000D_
_x000D_
subplot(1,2,1);_x000D_
_x000D_
fill(x,y,'c'); hold on_x000D_
_x000D_
axis([-b/2 b/2 -d/2 d/2]);_x000D_
_x000D_
title('Rectangle','FontSize',11);_x000D_
_x000D_
grid on_x000D_
subplot(1,2,2);_x000D_
_x000D_
sb = sprintf('Width:  %8.2f',b);_x000D_
_x000D_
sd = sprintf('Height: %8.2f',d);_x000D_
_x000D_
sA = sprintf('Area:  %8.2f',A);_x000D_
_x000D_
Ixx = sprintf('Ixx:   %8.2f',Ixx);_x000D_
_x000D_
Iyy = sprintf('Iyy:   %8.2f',Iyy);_x000D_
_x000D_
axis([0 15 0 5]);_x000D_
_x000D_
yp = 5-0.1;_x000D_
_x000D_
text(2,yp,sb); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,sd); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,sA); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,Ixx); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,Iyy);_x000D_
_x000D_
axis off_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% DrawHollowRectangle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function DrawHollowRectangle(b,d,b1,d1,A,Ixx,Iyy)_x000D_
_x000D_
x(1)=-b/2;      y(1)=-d/2;_x000D_
_x000D_
x(2)=x(1)+b;    y(2)=y(1);_x000D_
_x000D_
x(3)=x(2);      y(3)=y(1)+d;_x000D_
_x000D_
x(4)=x(1);      y(4)=y(3);_x000D_
_x000D_
x(5)=x(1);      y(5)=y(1);_x000D_
dx = (b-b1)/2;_x000D_
_x000D_
dy = (d-d1)/2;_x000D_
_x000D_
xx(1)=-b1/2;      yy(1)=-d1/2;_x000D_
_x000D_
xx(2)=xx(1)+b1;   yy(2)=yy(1);_x000D_
_x000D_
xx(3)=xx(2);      yy(3)=yy(1)+d1;_x000D_
_x000D_
xx(4)=xx(1);      yy(4)=yy(3);_x000D_
_x000D_
xx(5)=xx(1);      yy(5)=yy(1);_x000D_
figure(1); clf_x000D_
_x000D_
set (gcf,'Color','w');_x000D_
_x000D_
subplot(1,2,1);_x000D_
_x000D_
fill(x,y,'c'); hold on_x000D_
_x000D_
fill(xx,yy,'w'); hold on_x000D_
_x000D_
axis([-b/2 b/2 -d/2 d/2]);_x000D_
_x000D_
title('Hollow Rectangle','FontSize',11);_x000D_
_x000D_
grid on_x000D_
subplot(1,2,2);_x000D_
_x000D_
sb = sprintf('Outer width: %8.2f',b);_x000D_
_x000D_
sb1 = sprintf('Inner width:  %8.2f',b1);_x000D_
_x000D_
sd = sprintf('Outer height: %8.2f',d);_x000D_
_x000D_
sd1 = sprintf('Inner height:  %8.2f',d1);_x000D_
_x000D_
sA = sprintf('Area: %8.2f',A);_x000D_
_x000D_
Ixx = sprintf('Ixx: %8.2f',Ixx);_x000D_
_x000D_
Iyy = sprintf('Iyy: %8.2f',Iyy);_x000D_
_x000D_
axis([0 15 0 5]);_x000D_
_x000D_
yp = 5-0.1;_x000D_
_x000D_
text(2,yp,sb); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,sb1); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,sd); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,sd1); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,sA); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,Ixx); yp=yp-0.5;_x000D_
_x000D_
text(2,yp,Iyy); _x000D_
_x000D_
axis off_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% CreateCircle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function [xp,yp,ymn,ymx] = CreateCircle(a,b,r)_x000D_
_x000D_
dx = r/40;_x000D_
_x000D_
x1 = a-r;_x000D_
_x000D_
x2 = a+r;_x000D_
_x000D_
x = [x1:dx:x2]; n=length(x);_x000D_
_x000D_
k=0;_x000D_
_x000D_
for i=1:n_x000D_
_x000D_
k=k+1;_x000D_
_x000D_
xp(k) = x(i);_x000D_
_x000D_
yv = b - abs((r^2 - (xp(k)-a)^2)^0.5);_x000D_
_x000D_
yp(k) = yv;_x000D_
_x000D_
end_x000D_
_x000D_
x = [x2:-dx:x1]; n=length(x);_x000D_
_x000D_
for i=1:n_x000D_
_x000D_
k=k+1;_x000D_
_x000D_
xp(k) = x(i);_x000D_
_x000D_
yv = b + abs((r^2 - (xp(k)-a)^2)^0.5);_x000D_
_x000D_
yp(k) = yv;_x000D_
_x000D_
end_x000D_
_x000D_
ymn = min(yp);_x000D_
_x000D_
ymx = max(yp);_x000D_
_x000D_
return_x000D_
% #####################_x000D_
_x000D_
% CreateHollowCircle_x000D_
_x000D_
% #####################_x000D_
_x000D_
function [xp1,yp1,xp2,yp2,ymn,ymx] = CreateHollowCircle(a,b,r1,r2)_x000D_
_x000D_
for m=1:2_x000D_
_x000D_
if m==1; r=r1; end_x000D_
_x000D_
if m==2; r=r2; end_x000D_
_x000D_
dx = r/40;_x000D_
_x000D_
x1 = a-r;_x000D_
_x000D_
x2 = a+r;_x000D_
_x000D_
x = [x1:dx:x2]; n=length(x);_x000D_
_x000D_
k=0;_x000D_
_x000D_
for i=1:n_x000D_
_x000D_
k=k+1;_x000D_
_x000D_
xp(k) = x(i);_x000D_
_x000D_
yv = b - abs((r^2 - (xp(k)-a)^2)^0.5);_x000D_
_x000D_
yp(k) = yv;_x000D_
_x000D_
end_x000D_
_x000D_
x = [x2:-dx:x1]; n=length(x);_x000D_
_x000D_
for i=1:n_x000D_
_x000D_
k=k+1;_x000D_
_x000D_
xp(k) = x(i);_x000D_
_x000D_
yv = b + abs((r^2 - (xp(k)-a)^2)^0.5);_x000D_
_x000D_
yp(k) = yv;_x000D_
_x000D_
end_x000D_
_x000D_
if m==1_x000D_
_x000D_
xp1=xp;_x000D_
_x000D_
yp1=yp;_x000D_
_x000D_
else_x000D_
_x000D_
ymn = min(yp);_x000D_
_x000D_
ymx = max(yp);_x000D_
_x000D_
xp2=xp;_x000D_
_x000D_
yp2=yp;_x000D_
_x000D_
end_x000D_
_x000D_
end_x000D_
_x000D_
return_x000D_
```