Calculating moment of inertia and area of an irregular shape, MATLAB assignment help
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_fprintf('nttMenun');
_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_