HW2 solutions

# HW2 solutions - 6:26 PM F\NMWMHW2.m l of 1%Homework Number...

This preview shows pages 1–13. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/10/08 6:26 PM F:\NMWMHW2.m l of 1 %Homework Number 2 for Numerical Methods with Matlab %Due September 11, 2008 fprintf('Problem 1 Solution by Greg Jackson\n') fprintf('Part a') A=[—l —4 3 2; 3 2 l 3; fprintf('Part b') a = -4; b = 5; B a + (b—a)*rand(4) fprintf('Part c') C = A*B fprintf('Part d') D = A.*B fprintf('Part e') D(:,2)=C(2,3) fprintf('Part f') D(3,:)=A(l,:) D(:,2)=A(:,3) fprintf('Part'g') E = reshape(D,8,2) fprintf('Part h') F = ones(4)*.2 fprintf('Part i') size(E) fprintf('Part j') a = logspace(0,9,4) fprintf('Part k') b = linspace(100,0,2l) fprintf('Part l\n') fprintf('\n') 4 l 5 4; 2 -2 2 ~1] %Matrix A %Lower bound %Upper bound %Creates random 4x4 matrix between a and b %True matrix multiplication %Element by element multiplication %Changing the second column of D to the 3rd column of C %Changing the 3rd row of D to the lst row of A %Changing the 2nd column of D to the 3rd column of A %Changes D to an 8x2 %Creates a 4x4 matrix of all ones and then multiplies it by 0.2 %Tells you the number of rows and then columns with 4 elements %It will say 1E9*.... %100 to O with 21 components save Grngackson.txt D —ascii %Saves the matrix D to the file Grngackson in ascii format 9/10/08 6:23 PM MATLAB Command Window Problem 1 Solution by Greg Jackson Part a A: -l 3 4 2 Part b B: 3.3325 4.1521 —2.8571 4.2204 Part c C: —20.0716 28.1058 20.0781 —11.5738 Part d D: —3.3325 12.4564 —11.4285 8.4408 Part e D: —3-3325 12.4564 —11.4285 8.4408 Part f D: -3.3325 12.4564 —l.0000 8.4408 NU‘ll—‘w 1.6912 -3.1221 —l.4935 0.9219 8.1606 ‘o.1017 —o.137o 5.7178 -6.7649 -6.2443 —1.4935 -1.8439 -21.6273 34.8452 29.1882 -10.0312 —21.6273 34.8452 —4.0000 -10.0312 “>03 ~21 13. .6840 .9074 .4707 -12 13 13 U) .6176 .6840 .5815 .7353 .6273 34. 29. —10. 8452 1882 0312 8527 .8527 .6840 —12. .4707 9074 .8527 .6840 .0000 .4707 12 NMI—‘KO .6145 .3684 .2025 .7230 .9265 .6137 23. 17. 9469 6203 .2290 .1051 12. .7230 8101 .2290 .1051 .8101 .7230 .2290 .1051 .0000 .7230 1 of 3 9/10/08 6:23 PM MATLAB Command Window 2 of 3 -3.3325 3.0000 13.8527 12.4564 1.0000 4.6840 -1.0000 5.0000 3.0000 8.4408 2.0000 9.4707 Part g E = —3.3325 13.8527 12.4564 4.6840 —1.0000 3.0000 8.4408 9.4707 3.0000 9.2290 1.0000 1.1051 5.0000 2.0000 2.0000 2.7230 Part h F: 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 Part i ans = 8 2 Part j a: 1.0e+009 * 0.0000 0.0000 0.0010 Part k 1): Columns 1 through 15 100 95 90 85 80 Columns 16 through 21 25 20 15 10 5 Part 1 >> fprintf('Part m') Part m>> load Grngackson.txt 9.2290 1.1051 2.0000 2.7230 0.2000 0.2000 0.2000 0.2000 1.0000 75 70 65 60 55 50 45 40 35 30 9/10/08 6:23 PM >> Grngackson Grngackson >> —3 .3325 .4564 .0000 .4408 .0000 .4408 [QUINme .0000 .0000 .0000 .0000 .0000 .0000 13 LOWKDWé MATLAB Command Window .8527 .6840 .0000 .4707 .0000 .4707 NNNNI—Jko .2290 .1051 .0000 .7230 .0000 .7230 3 of 3 s r E l I' I E 9/10/08 6:31 PM F:\NMWMHW2num2.m fid=fopen('sprint.dat', 'rt'); headings = fgetl(fid); headings2 : fgetl(fid); d = fscanf(fid, '%f'); fclose(fid); times = reshape(d,3,11); %Grabs the first line %Grabs the second line %Writes into a column vector %Makes the column vector a 3X11 [x, remain] = strtok(headings); %Separates the headings into x and Lewis Johnson [Lewis, Johnson] = strtok(remain); %Separates Lewis and Johnson tau = 0.739; a = 14.4; t = O:0.2:lO; sprint = a*tau*(t-tau*(1—exp(—t./tau))); %Note the period after the last t x times(1,:,:); %Grabs the first row of the times matrix Lewis = times(2,:,:); %Grabs the second row Johnson times(3;:,:); %Grabs the third row plot(t,sprint,Lewis,x,'b+',Johnson,x,'r*') x1abel('Time (s)') ylabel('Distance (m)') 1egend('Theoretical','Lewis','Johnson', 2) title('Lewis Theoretical versus Lewis and Johnson Real') axis([0 10 0 100]) 1 of 1 Distance (m) Theoretical + Lewis ale Johnson Lewis Theoretical versus Lewis and Johnson Real 9/11/08 7:45AM xg = linspace(—5,5,20); [X,Y] = meshgrid(xg,xg); z = 2—X.‘2-Y.*2; surfl (X,Y, Z); colormap( 'bone' ); title ( 'Surfl ' ); xlabel('x'); ylabel('y'); F: \NMWMHW2num3 . m lofl Mesh Mesh: Surf Surf: Surﬂ Eurﬂ Elana ...
View Full Document

{[ snackBarMessage ]}

### Page1 / 13

HW2 solutions - 6:26 PM F\NMWMHW2.m l of 1%Homework Number...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online