clc clear all % in class exercies - post test 2 disp('How to use vectors - Chapter 26') % Exercise 3 % find the dot product disp('Exercise 3') disp('Find the dot product of two equal sized vectors') % input both vectors v1=[3 -7 23]; v2=[-7 0 12]; disp(' The first vector is:') disp(v1) disp(' The second vector is:') disp(v2) % code % check vector sizes n1=length(v1); n2=length(v2); if n1==n2 sump=0; for i=1:1:n1 sump=sump+v1(i)*v2(i); end else disp('Error - Vectors are not the same size') end fprintf(' The vector dot product is %g\n\n',sump) % Exercise 4 % Young's modulus disp('Exercise 4') disp('Find the young''s modulus of a material') disp(' ') % inputs strain=[0.00010 0.00012 0.0010 0.0015 0.0018 0.0022 0.0026]; stress=[19.1 22.81 187 284.2 344.3 417.0 495.0]; % MPa % displaying inputs n=length(stress); disp(' Input data:') for j=1:1:n fprintf(' Strain = %5.4g, Stress = %4.3g MPa\n',strain(j),stress(j)) end % code top=0; bot=0; for i=1:1:n top=top+stress(i)*strain(i); bot=bot+strain(i)^2; end young=top/bot; disp(' ') fprintf(' The young''s modulus is %g GPa\n\n',young*10^(-3))