Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 1-24 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 1-25 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 1-25 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 1.26 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 2-4 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 2-10 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 2-13 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 2-24 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 2-29 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 2-29 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3.2 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-3 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-7 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-8 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-12 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3.12 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-13 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-14 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-15 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-16 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-16 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-17 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-18 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-18 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-19 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-20 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-22 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-24 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-26 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-26 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-27 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-28 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-29 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-29 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-30 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-31 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-32 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-33 continued Continued on next slide Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Problem 3-33 continued Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 x 1 + 2x 2 – 2x 3 = 9 4.1 Solve the following system of equations using the Gauss elimination method 2x 1 + 3x 2 + x 3 = 23 . 3x 1 + 2x 2 – 4x 3 = 11 Solution Step 1: Write the system of equations in matrix form: 1 2 –2 x1 9 = 2 3 1 x2 23 3 2 –4 x3 11 a 21 2 - = --- = 2 . Using the first element of the matrix as a pivot, a 11 = 1 and m 21 = -----a 11 1 Step 2: Multiply the first row by m 21 = 2 , subtract the result from the second row, and replace the second row with the final result: 9 1 2 –2 x1 0 –1 5 x2 = 5 11 3 2 –4 x3 a' 31 --- = 3 , subtract the result from the third row, and - = 3 Step 3: Next, multiply the first row by m 31 = ------a' 11 1 replace the third row with the final result: 1 2 –2 x1 9 0 –1 5 x2 = 5 0 –4 2 x3 – 16 a' 32 –4 - = ------ = 4 , subtract the Step 4: Use the element a' 22 as the pivot. Multiply the second row by m 32 = ------a' 22 result from the third row, and replace the third row with the final result: 1 2 –2 x1 9 0 –1 5 x2 = 5 0 0 – 18 x 3 – 36 Step 4: The matrix is in upper triangular form, apply back-substitution: 36- = 2 -------x3 = – – 18 – x2 + 5 2 = 5 x 2 = 10 – 5 = 5 x1 + 2 5 – 2 2 = 9 x 1 = 9 + 4 – 10 = 3 Thus, the solution to the given set of simultaneous equations is x 1 x 2 x 3 = 3 5 2 . –1 1 4.2 x1 4 2 –4 1 Given the system of equations > a @ > x @ = > b @ , where a = 6 2 – 1 , x = x 2 , and b = 10 , deter–6 –2 6 –2 x3 mine the solution using the Gauss elimination method. Solution Following the same procedure as in Problem 4.1, the row elimination operations proceed as follows: a 21 Step 1: Multiply the first row by m 21 = -------- = 3 , subtract the result from the second row, and replace - = 6 a 11 2 the second row with the final result: 2 –4 1 x1 4 0 14 – 4 x 2 = – 2 –2 6 –2 x3 –6 a' 31 –2 Step 2: Multiply the first row by m 31 = ------- = ------ = – 1 , subtract the result from the third row, and replace a' 11 2 the third row with the final result: 2 –4 1 x1 4 = x 0 14 – 4 2 –2 0 2 –1 x3 –2 a' 32 2- = 1 Step 3: Use the element a' 22 as the pivot. Multiply the second row by m 32 = --------- , subtract the - = ----a' 22 14 7 result from the third row, and replace the third row with the final result: 2 –4 1 x 4 1 0 14 – 4 – 2 x2 = 3 12 0 0 – --- x 3 – -----7 7 Step 4: Solving by back-substitution, 12 x 3 = ------ = 4 , 3 14x 2 – 4 4 = – 2 2x 1 – 4 1 + 4 = 4 or x 1 = 2 . Thus, the solution is x 1 x 2 x 3 = 2 1 4 . . or x2 = 1 , and 1 4.3 0.0003x 1 + 1.566x 2 = 1.569 . 0.3454x 1 – 2.436x 2 = 1.018 Consider the following system of two linear equations: (a) Solve the system with the Gauss elimination method using rounding with four significant figures. (b) Switch the order of the equations, and solve the system with the Gauss elimination method using rounding with four significant figures. Check the answers by substituting the solution back in the equations. Solution (a) First, write the system in matrix form: 0.0003 1.566 x1 0.3454 – 2.436 x 2 = 1.569 . Next, apply Gaussian elimina1.018 a 21 x ---------------- = 1151 , we have 0.0003 1.566 1 = 1.569 . Note that - = 0.3454 tion with rounding. With m 21 = -----a 11 0.0003 0.0001 – 1804 x 2 – 1805 the a' 21 element should be zero but it is not because of the lack of precision. Solving the second equation 1805 - = 1.001 , and 0.0003 x 1 + 1.566 1.001 = 1.569 or x 1 = 3.333 . Substiby back-substitution, x 2 = –-------------– 1804 tuting these results into the original equations, 0.0003 1.566 1.001 = 5.219 which is not at all the 0.3454 – 2.436 3.333 – 7.773 right hand side that was given in the problem. Consequently, x1 x2 = 1.001 is not the correct answer. 3.333 x (b) Switching the order of the equations, we have: 0.3454 – 2.436 1 = 1.018 . Applying Gaussian 0.0003 1.566 x2 1.569 a 21 elimination with rounding to 4 significant figures yields: m 21 = --------------------- = 0.0008686 and - = 0.0003 a 11 0.3454 – 2.436 x 1 = 1.018 . 0.0000 1.568 x 2 1.570 Solving by back-substitution, 0.3454 1.570 x 2 = ------------- = 1.001 1.568 and 3.456 0.3454 x 1 – 2.436 1.001 = 1.018 or x 1 = ---------------- = 10.01 . Checking by substituting these solutions into 0.3454 the original system, 0.3454 – 2.436 10.01 = 1.019 , which is close the original right hand side that was 0.0003 1.566 1.001 1.571 given ( 1.018 ). Thus, the answer of part (b) is closer to the correct answer, while that of part (a) is com1.569 pletely wrong. This demonstrates the need to perform row exchange operations before pivoting when a matrix is poorly conditioned. . 1 4.4 Solve the following system of equations using the Gauss elimination method: 4x 1 + 3x 2 + 2x 3 + x 4 3x 1 + 4x 2 + 3x 3 + 2x 4 2x 1 + 3x 2 + 4x 3 + 3x 4 x 1 + 2x 2 + 3x 3 + 4x 4 = = = = 1 1 –1 –1 Solution The system of equations in matrix form is: 4 3 2 1 3 4 3 2 2 3 4 3 1 2 3 4 x1 x2 x3 x4 1 = 1 –1 –1 a 41 a 31 a 21 1 2 3 - = --- = 0.25 , the first pass with Gauss- = --- = 0.5 , and m 41 = ------ = --- = 0.75 , m 31 = -----With m 21 = -----a 11 a 11 4 a 11 4 4 ian elimination yields: 4 3 2 1 x1 1 0 1.75 1.5 1.25 x 2 = 0.25 0 1.5 3 2.5 x 3 – 1.5 0 1.25 2.5 3.75 x 4 – 1.25 a' 42 a' 32 1.25 1.5 Next, with m 32 = ------- = ---------- , the second pass with Gaussian elimination yields: - = ---------- , m 42 = ------a' 22 1.75 a' 22 1.75 x1 1 4 3 2 1 0 1.75 1.5 1.25 x 2 = 0.25 0 0 1.7143 1.4286 x 3 – 1.7143 – 1.4286 0 0 1.4286 2.8571 x 4 as 43 1.4286 Next, with m 43 = --------- = ---------------- , the third pass with Gaussian elimination yields: as 33 1.7143 x1 4 3 2 1 1 0 1.75 1.5 1.25 x 2 = 0.25 0 0 1.7143 1.4286 x 3 – 1.7143 0 0 0 1.6667 x 4 0 0.25 – 1.25 0 – 1.5 – 1 – 1.7143 – 1.4286 0 Using back substitution, x 4 = 0 , x 3 = ------------------------------------------------------- = – 1 , x 2 = --------------------------------------------------------------------- = 1 , 1.7143 1.75 – 1 0 – 2 –1 – 3 1 - = 0 . Thus, the solution is: x 1 x 2 x 3 x 4 = 0 1 – 1 0 . and x 1 = 1----------------------------------------------------------------------4 . 1 4.5 Solve the following system of equations with the Gauss elimination method. 2x 1 + x 2 + 4x 3 – 2x 4 = 19 – 3x 1 + 4x 2 + 2x 3 – x 4 = 1 3x 1 + 5x 2 – 2x 3 + x 4 = 8 – 2x 1 + 3x 2 + 2x 3 + 4x 4 = 13 Solution The system of equations in matrix form is: 2 –3 3 –2 1 4 5 3 4 2 –2 2 –2 –1 1 4 x1 x2 x3 x4 19 = 1 8 13 a 41 a 21 – 2- = – 1 , the first pass with Gauss– 3- = – 1.5 , m = a 31 With m 21 = ------ = ------- = 1.5 , and m 41 = ----------- = 3 - = ----31 a 11 a 11 2 a 11 2 2 ian elimination yields: 2 0 0 0 1 5.5 3.5 4 4 8 –8 6 –2 –4 4 2 x1 x2 x3 x4 19 = 29.5 – 20.5 32 a' 42 a' 32 4 - , the second pass with Gaussian elimination yields: - = ------------ , m 42 = ------- = 3.5 Next, with m 32 = ------a' 22 a' 22 5.5 2 0 0 0 5.5 x1 1 4 –2 19 x 5.5 8 –4 29.5 2 = 0 – 13.0909 6.5455 x 3 – 39.2727 0 0.1818 4.9091 x 4 10.5455 as 43 0.1818 - = ---------------------- , the third pass with Gaussian elimination yields: Next, with m 43 = --------as 33 – 13.0909 2 0 0 0 x1 19 1 4 –2 x2 5.5 8 –4 29.5 = 0 – 13.0909 6.5455 x 3 – 39.2727 0 0 5 10 x4 29.5 + 4 2 – 8 4 39.2727 – 6.5455 2 - = 4 , x 2 = ------------------------------------------------------- = 1 , and Using back substitution, x 4 = 2 , x 3 = –----------------------------------------------------– 13.0909 5.5 + 2 2 – 4 4 – 1 1 - = 3 . Thus, the solution is: x 1 = 19 ----------------------------------------------------------------------x1 x2 x3 x4 = 3 1 4 2 . 2 1 4.6 Solve the following system of equations using the Gauss–Jordan method: x 1 + 2x 2 – 2x 3 = 9 2x 1 + 3x 2 + x 3 = 23 3x 1 + 2x 2 – 4x 3 = 11 Solution 1 2 –2 9 First, form the augmented matrix, including the right hand side column vector: 2 3 1 23 . 3 2 – 4 11 Step 1: The pivot element a 11 = 1 is already 1. Use the first (pivot) row to eliminate the entries below the pivot element: 1 2 –2 9 2 3 1 23 3 2 – 4 11 m m –2 1 2 –2 9 –3 1 2 –2 9 1 2 –2 9 5 0 – 4 2 – 16 = 0 –1 5 Step 2: Normalize the second row by dividing it by -1: 1 2 –2 9 0 1 –5 –5 0 – 4 2 – 16 The pivot element a 22 is now 1.Use the second (pivot) row to eliminate the entries above and below the pivot element: 1 2 –2 9 0 1 –5 –5 0 – 4 2 – 16 m –2 0 1 –5 –5 1 0 8 19 = 0 1 –5 –5 m – –4 0 1 –5 –5 0 0 – 18 – 36 Step 3: Normalize the third row by dividing it by -18: 1 0 8 19 0 1 –5 –5 0 0 1 2 The pivot element a 33 is now 1.Use the third (pivot) row to eliminate the entries above the pivot element: 1 0 8 19 0 1 –5 –5 0 0 1 2 m m x1 Thus, the solution is 3 x2 = 5 2 x3 . –8 0 0 1 2 – –5 0 0 1 2 = 1 0 0 3 0 1 0 5 0 0 1 2 1 4.7 x1 2 –4 1 4 Given the system of equations > a @ > x @ = > b @ , where a = 6 2 – 1 , x = x 2 , and b = 10 , deter–2 6 –2 –6 x3 mine the solution using the Gauss–Jordan method. Solution 2 –4 1 4 First, form the augmented matrix, including the right hand side column vector: 6 2 – 1 10 . –2 6 –2 –6 Step 1: The pivot element is a 11 = 2 . Normalize the first row by dividing it by 2: 1 – 2 0.5 2 6 2 – 1 10 –2 6 –2 –6 Use the first (pivot) row to eliminate the entries below the pivot element: 1 – 2 0.5 2 6 2 – 1 10 –2 6 –2 –6 m m – 6 1 – 2 0.5 2 – – 2 1 – 2 0.5 2 = 1 – 2 0.5 2 0 14 – 4 – 2 0 2 –1 –2 Step 2: Normalize the second row by dividing it by 14: 1 – 2 0.5 2 0 1 – 0.2857 – 0.1429 0 2 –1 –2 The pivot element a 22 is now 1. Use the second (pivot) row to eliminate the entries above and below the pivot element: 1 – 2 0.5 2 0 1 – 0.2857 – 0.1429 0 2 –1 –2 m – – 2 0 1 – 0.2857 – 0.1429 = m – 2 0 1 – 0.2857 – 0.1429 1 0 – 0.0714 1.7143 0 1 – 0.2857 – 0.1429 0 0 – 0.4286 – 1.7143 Step 3: Normalize the third row by dividing it by -0.4286: 1 0 – 0.0714 1.7143 0 1 – 0.2857 – 0.1429 0 0 1 4 1 0 – 0.0714 1.7143 0 1 – 0.2857 – 0.1429 0 0 – 0.4286 – 1.7143 The pivot element a 33 is now 1. Use the third (pivot) row to eliminate the entries above the pivot element: 1 0 – 0.0714 1.7143 0 1 – 0.2857 – 0.1429 0 0 1 4 m m – – 0.0714 0 0 1 4 – – 0.2857 0 0 1 4 = 1 0 0 2 0 1 0 1 0 0 1 4 2 x1 Thus, the solution is . 2 x2 = 1 4 x3 1 4.8 Solve the following system of equations with the Gauss–Jordan elimination method. 2x 1 + x 2 + 4x 3 – 2x 4 = 19 – 3x 1 + 4x 2 + 2x 3 – x 4 = 1 3x 1 + 5x 2 – 2x 3 + x 4 = 8 – 2x 1 + 3x 2 + 2x 3 + 4x 4 = 13 Solution 2 First, form the augmented matrix, including the right hand side column vector: – 3 3 –2 1 4 5 3 4 2 –2 2 –2 –1 1 4 19 1 8 13 Step 1: The pivot element is a 11 = 2 . Normalize the first row by dividing it by 2: 1 –3 3 –2 0.5 4 5 3 4 2 –2 2 –1 –1 1 4 9.5 1 8 13 Use the first (pivot) row to eliminate the entries below the pivot element: 1 –3 3 –2 0.5 4 5 3 4 2 –2 2 –1 –1 1 4 9.5 1 8 13 m m – – 3 1 0.5 4 – 1 9.5 – 3 1 0.5 4 – 1 9.5 – – 2 1 0.5 4 – 1 9.5 m m = 1 0 0 0 0.5 5.5 3.5 4 2 8 –8 6 – 1 9.5 – 4 29.5 4 – 20.5 2 32 Step 2: Normalize the second row by dividing it by 5.5: 1 0 0 0 0.5 2 –1 9.5 1 1.455 – 0.7273 5.364 3.5 – 8 4 – 20.5 4 6 2 32 The pivot element a 22 is now 1. Use the second (pivot) row to eliminate the entries above and below the pivot element: 1 0 0 0 0.5 2 –1 9.5 1 1.455 – 0.7273 5.364 3.5 – 8 4 – 20.5 4 6 2 32 m – 0.5 0 1 1.455 – 0.7273 5.364 m m m – 3.5 0 1 1.455 – 0.7273 5.364 – 4 0 1 1.455 – 0.7273 5.364 Step 3: Normalize the third row by dividing it by -13.0909: 1 0 0 0 0 1.2727 – 0.6364 6.8182 1 1.4545 – 0.7273 5.3636 0 1 – 0.5 3 0 0.1818 4.9091 10.5455 = 1 0 0 0 0 1 0 0 1.2727 1.4545 – 13.0909 0.1818 – 0.6364 – 0.7273 6.5455 4.9091 6.8182 5.3636 – 39.2727 10.5455 2 The pivot element a 33 is now 1. Use the third (pivot) row to eliminate the entries above and below the pivot element: 1 0 0 0 0 1.2727 – 0.6364 6.8182 1 1.4545 – 0.7273 5.3636 0 1 – 0.5 3 0 0.1818 4.9091 10.5455 m – 1.2727 0 0 1 – 0.5 3 – 1.4545 0 0 1 – 0.5 3 m m m 1 0 0 0 = – 0.1818 0 0 1 – 0.5 3 0 1 0 0 0 0 0 0 1 – 0.5 0 5 3 1 3 10 Step 4: Normalize the fourth row by dividing it by 5: 1 0 0 0 0 1 0 0 0 0 0 0 1 – 0.5 0 1 3 1 3 2 The pivot element a 44 is now 1. Use the third (pivot) row to eliminate the entries above the pivot element: 1 0 0 0 Thus, the solution is 0 1 0 0 0 0 0 0 1 – 0.5 0 1 x1 3 = 1 4 2 x2 x3 x4 . 3 1 3 2 m m m m –0 0 0 0 1 2 –0 0 0 0 1 2 – – 0.5 0 0 0 1 2 = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 3 1 4 2 1 1 2 3 4.9 Determine the LU decomposition of the matrix a = 4 5 6 using the Gauss elimination procedure. 3 2 2 Solution LU decomposition using Gausian elimination transforms the above matrix into a lower triangular matrix > L @ multiplied by an upper triangular matrix > U @ . > U @ is the upper triangular matrix that would normally result after applying Gaussian elimination to the given matrix. > L @ consists of the multipliers that are used in the Gaussian elimination procedure and 1s along the diagonal. Thus, with m 21 = 4 and m 31 = 3 , 1 2 3 1 2 3 o 4 5 6 0 – 3 – 6 . Next, with 3 2 2 0 –4 –7 m 32 = 4 e 3 , 1 2 3 1 2 3 o 0 –3 –6 0 – 3 – 6 . Thus, 0 0 1 0 –4 –7 1 0 0 1 0 0 1 2 3 1 0 0 1 2 3 L = m 21 1 0 = 4 1 0 . Therefore, 4 5 6 = LU = 4 1 0 0 – 3 – 6 m 31 m 32 1 3 4e3 1 3 2 2 3 4e3 1 0 0 1 . 1 2 3 U = 0 – 3 – 6 , and 0 0 1 1 1 2 3 4.10 Determine the LU decomposition of the matrix a = 4 5 6 using Crout’s method. 3 2 2 Solution The LU decomposition is done by following the procedure described in Section 4.5.2. L 11 = a 11 = 1 , L 31 = a 31 = 3 , a 12 - = 2, U 12 = -----L 11 U 13 = 3 , L 21 = a 21 = 4 , a 23 – L 21 U 13 6– 4 3 U 23 = ----------------------------- = ------------------------- = 2 , –3 L 22 L 22 = a 22 – L 21 U 12 = 5 – 4 2 = – 3 , L 32 = a 32 – L 31 U 12 = 2 – 3 2 = – 4 , 1 0 0 L 33 = a 33 – L 31 U 13 – L 32 U 23 = 2 – 3 3 – – 4 2 = 1 . Thus, > L @ = 4 – 3 0 3 –4 1 1 2 3 and > U @ = 0 1 2 . To check the answer, multiply the two matrices to see if the original matrix is obtained: 1 0 0 1 2 3 1 2 3 > L @ > U @ = 4 –3 0 0 1 2 = 4 5 6 = > a @ 3 –4 1 0 0 1 3 2 2 . 0 0 1 1 4.11 Solve the following system with LU decomposition using Crout’s method. 5 –1 0 x 9 –1 5 –1 y = 4 –6 0 –1 5 z Solution First the LU decomposition of the matrix of coefficients is done by following the procedure described in Section 4.5.2. L 11 = a 11 = 5 , a 13 0 - = --- = 0 , U 13 = -----5 L 11 a 12 1 - = – --- , U 12 = -----5 L 11 ------ , ---· = 24 L 22 = a 22 – L 21 U 12 = 5 – – 1 § – 1 © 5¹ 5 L 31 = a 31 = 0 , L 21 = a 21 = – 1 , a 23 – L 21 U 13 5- , – 1 – – 1 0 = – ----U 23 = ----------------------------- = -------------------------------24 24 e 5 L 22 115 5 L 33 = a 33 – L 31 U 13 – L 32 U 23 = 5 – 0 0 – – 1 § – ------· = --------- . © 24¹ 24 1 L 32 = a 32 – L 31 U 12 = – 1 – 0 § – ---· = – 1 , © 5¹ 5 0 0 1 1 – --5 24 - 0 and > U @ = Thus, > L @ = – 1 ----5 0 1 115 -------0 –1 24 0 0 0 5 . To check the answer, multiply the two matrices to see if – -----24 1 the original matrix is obtained: 1 5 0 0 1 – --- 0 5 5 –1 0 24 - 0 = > L @ > U @ = – 1 ----5 5 –1 5 –1 = > a @ 0 1 – -----24 115 0 –1 5 0 – 1 --------24 0 0 1 Next, > L @ and > b @ are substituted in Eq. (4.23): 5 0 0 x' 9 24 – 1 ------ 0 = y' 5 4 115 z' –6 0 – 1 --------24 This equation is solved for > y @ by using forward substitution: 5 29 5 5 29 9 9 x' = --- , y' = 4 + x' ------ = ------ § 4 + ---· = ------ § ------· = ------ , and 24 © 5 ¹ 24 © 24 24 5¹ 5 24 24 115 24 29 z' = – 6 + y' --------- = --------- § – 6 + ------· = --------- § – ---------· = – 1 . 115 115 © 24 ¹ 115 © 24¹ 2 x' Next y' is substituted in Eq. (4.22): z' 1 9 1 – --- 0 --5 5 x 5 y = 29 0 1 – ----------24 z 24 0 0 1 –1 x The last equation is solved for y using back substitution z 29 5 29 5 24 9 1 9 1 10 z = – 1 , y = ------ + ------ z = ------ – ------ = ------ = 1 , and x = --- + --- y = --- + --- = ------ = 2 24 24 24 24 24 5 5 5 5 5 x The solution is y = z 2 1 . –1 Check the answer by substituting into the original system: 5 –1 0 2 9 = –1 5 –1 1 4 0 –1 5 –1 –6 which matches the original right hand side. 1 –4 e 5 –3 e 5 –2 e 5 4.12 Find the inverse of the matrix – 3 e 5 – 6 e 5 – 4 e 5 using the Gauss–Jordan method. –2 e 5 –4 e 5 –6 e 5 Solution First form the augmented matrix with the identity matrix: 4 – --5 3 – --5 2 – --5 3 – --5 6 – --5 4 – --5 2 – --- 1 0 0 5 4 – --- 0 1 0 5 6 – --- 0 0 1 5 Step 1: The pivot element is a 11 = – 4--- . Normalize the first row by dividing it by – 4--- : 5 5 3 1 --4 3 6 – --- – --5 5 2 4 – --- – --5 5 1 --2 4 – --5 6 – --5 5 – --- 0 0 4 0 10 0 01 The pivot element a 11 is now 1. Use the first (pivot) row to eliminate the entries below the pivot element: 3 1 --4 3 6 – --- – --5 5 2 4 – --- – --5 5 1 --2 4 – --5 6 – --5 5 – --- 0 0 4 0 1 0 m 3 1 5 3 - --- – --- 0 0 – § – ---· 1 -4 2 4 © 5¹ 0 0 1 m 3 1 5 2 - --- – --- 0 0 – § – ---· 1 -4 2 4 © 5¹ = 3 1 --4 3 0 – --4 1 0 – --2 1 --2 1 – --2 5 – --- 0 0 4 3 – --- 1 0 4 1 – 1 – --- 0 1 2 Step 2: The pivot element is a 22 = – 3--- . Normalize the second row by dividing it by – 3--- : 4 4 3 1 5 1 --- --- – --4 2 4 2 0 1 --- 1 3 1 1 0 – --- – 1 – --2 2 0 0 4 – --- 0 3 0 1 The pivot element a 22 is now 1. Use the second (pivot) row to eliminate the entries below and above the pivot element: 2 3 1 5 1 --- --- – --4 2 4 2 0 1 --- 1 3 1 1 0 – --- – 1 – --2 2 m 0 0 2 4 3 – § ---· 0 1 --3- 1 – --- 0 © 4¹ 4 4 – --- 0 3 = m 0 1 2 4 1 – § – ---· 0 1 --3- 1 – --- 0 © 2¹ 4 1 0 0 –2 1 0 2 4 0 1 --- 1 – --- 0 3 3 2 2 0 0 – --- 0 – --- 1 3 3 Step 3: The pivot element is a 33 = – 2--- . Normalize the third row by dividing it by – 2--- : 3 3 1 0 0 –2 1 0 2 4 0 1 --- 1 – --- 0 3 3 3 0 0 1 0 1 – --2 The pivot element a 33 is now 1. Use the third (pivot) row to eliminate the entries above the pivot element: 1 0 0 –2 1 0 2 4 0 1 --- 1 – --- 0 3 3 3 0 0 1 0 1 – --2 m m 3 – – 2 0 0 1 0 1 – --2 2 0 0 1 0 1 –3 --– --2 3 The inverse of the matrix is then: –2 1 0 1 –2 1 3 0 1 – --2 = 1 0 0 –2 1 0 0 1 0 1 –2 1 3 0 0 1 0 1 – --2 1 6 3 11 4.13 Given the matrix a = 3 2 7 , determine the inverse of > a @ using the Gauss–Jordan method. 3 2 6 Solution First form the augmented matrix with the identity matrix: 6 3 11 1 0 0 3 2 7 0 1 0 3 2 6 0 0 1 Step 1: The pivot element is a 11 = 6 . Normalize the first row by dividing it by 6 : 1 1 --2 3 2 3 2 11 -----6 7 6 1 --- 0 0 6 0 1 0 0 0 1 The pivot element a 11 is now 1. Use the first (pivot) row to eliminate the entries below the pivot element: 1 1 --2 3 2 3 2 11 -----6 7 6 1 --- 0 0 6 0 1 0 0 0 1 m m 1 – 3 1 --2 1 – 3 1 --2 11 -----6 11 -----6 1 --- 0 0 6 1 --- 0 0 6 1 1 --2 1 0 --2 1 0 --2 = 11 -----6 3 --2 1 --2 1 --- 0 0 6 1 – --- 1 0 2 1 – --- 0 1 2 1 Step 2: The pivot element is a 22 = 1--- . Normalize the second row by dividing it by --- : 2 2 1 1 --2 0 1 1 0 --2 11 -----6 3 1 --2 1 --- 0 0 6 –1 2 0 1 – --- 0 1 2 The pivot element a 22 is now 1. Use the second (pivot) row to eliminate the entries below and above the pivot element 1 1 --2 0 1 1 0 --2 11 -----6 3 1 --2 1 --- 0 0 6 –1 2 0 1 – --- 0 1 2 m 1 – § ---· 0 1 3 – 1 2 0 © 2¹ = m 1 – § ---· 0 1 3 – 1 2 0 © 2¹ 1 1 0 --3 0 1 3 0 0 –1 2 --- – 1 0 3 –1 2 0 0 –1 1 Step 3: The pivot element is a 33 = – 1 . Normalize the third row by dividing it by – 1 : 2 1 1 0 --3 0 1 3 0 0 1 2 --- – 1 0 3 –1 2 0 0 1 –1 The pivot element a 33 is now 1. Use the third (pivot) row to eliminate the entries above the pivot element: 1 1 0 --3 0 1 3 0 0 1 2 --- – 1 0 3 –1 2 0 0 1 –1 m m 1 – § ---· 0 0 1 0 1 – 1 © 3¹ –3 0 0 1 0 1 –1 The inverse of the matrix is then: 2 --3 –1 0 . 4 – --3 –1 1 1 --3 3 –1 = 2 4 1 0 0 --- – --3 3 0 1 0 –1 –1 0 0 1 0 1 1 --3 3 –1 1 4.14 Carry out the first three iterations of the solution of the following system of equations using the Gauss–Seidel iterative method. For the first guess of the solution, take the value of all the unknowns to be zero. 8x 1 + 2x 2 + 3x 3 = 51 2x 1 + 5x 2 + x 3 = 23 – 3x 1 + x 2 + 6x 3 = 20 Solution The essence of the Gauss-Seidel iterative method is given by Eq. (4.51): j=n § 1 a ij x j x i = ----- b i – ¨ a ii ¨j=1 © jzi ¦ · ¸ ¸ ¹ i = 1 2 } n First Iteration: Starting with 1 x3 0 0 0 x1 x2 x3 1 = 0 0 0 , x1 1 23 – 2 0 – 0 51 – 2 0 – 3 0 = ---------------------------------------- = 6.375 , x 2 = -------------------------------- = 4.6 , and 5 8 20 + 3 0 – 0 = -------------------------------- = 3.3333 . 6 Second Iteration: 2 x1 2 23 – 2 6.375 – 3.3333 51 – 2 4.6 – 3 3.3333 = ---------------------------------------------------------- = 3.9750 , x 2 = -------------------------------------------------------- = 6.9167 , and 5 8 1 x3 20 + 3 6.375 – 4.6 = ----------------------------------------------- = 5.7542 . 6 Third Iteration: 3 x1 2 23 – 2 3.9750 – 5.7542 51 – 2 6.9167 – 3 5.7542 = ------------------------------------------------------------------- = 2.488 , x 2 = ----------------------------------------------------------- = 1.8592 , and 5 8 1 x3 20 + 3 3.9750 – 6.9167 = ----------------------------------------------------------- = 4.1681 . 6 1 4.15 Find the condition number of the matrix in Problem 4.13 using the infinity norm. Solution 2 --6 3 11 3 –1 The matrix in Problem 4.13 is > a @ = 3 2 7 . Its inverse is > a @ = –1 3 2 6 0 4 – --3 –1 1 1 --3 . 3 –1 The infinity norm is given by Eq.(4.73), which defines it as the maximum of the values obtained by summing the absolute values of the entries in each row. Thus, > a @ f = max –1 Similarly, > a @ f 6 + 3 + 11 3 + 2 + 7 3 + 2 + 6 2 + –4 --- + 1 ----3 3 3 = max –1 + –1 + 3 0 + 1 + –1 The condition number is > a @ . 20 = max 12 = 20 . 11 –1 f >a @ f 7 --3 = max = 5. 5 2 = 20 5 = 100 . 1 4.16 Find the condition number of the matrix in Problem 4.13 using the 1-norm. Solution 2 --6 3 11 3 –1 The matrix in Problem 4.13 is > a @ = 3 2 7 . Its inverse is > a @ = –1 3 2 6 0 4 – --3 –1 1 1 --3 . 3 –1 The 1-norm is given by Eq.(4.74), which defines it as the maximum of the values obtained by summing the absolute values of the entries in each column. Thus: > a @ 1 = max 6 + 3 + 3 3 + 2 + 2 11 + 7 + 6 Similarly: > a –1 @ 1 = max 12 7 24 = 24 4 1 = max 2 --- + – 1 + 0 – --- + – 1 + 1 --- + 3 + 1 3 3 3 The condition number is: > a @ –1 1 >a @ 1 13 10 13 = max 5 --- ------ ------ = -----3 3 3 3 13 = 24 § ------· = 104 . © 3¹ Note that though the condition number using the 1-norm differs from that obtained using the infinity norm, both give values of the condition number that are comparable in order of magnitude. . 1 4.17 Show that the eigenvalues of the n u n identity matrix is the number 1 repeated n times. Solution The eigenvalues of any matrix are determined from the solution of the characteristic equation, Eq.(4.93): det > a – OI @ = 0 For the case of the identity matrix, > a @ = > I @ , so that Eq.(4.93) reduces to det > I – OI @ = 0 , which can be recognized for an n u n matrix as: 1–O 0 det 0 } } 0 0 0 1–O 0 0 1–O } } } } } 0 } } 0 } 0 0 } 0 0 0 } 0 = 1–O } 0 1–O 0 0 1–O n = 0 which has the repeated roots O 1 = 1 , O 2 = 1 , } , O n = 1 . Thus, the eigenvalues of the n u n identity matrix is the number 1 repeated n times. . 1 4.18 Show that the eigenvalues of the following matrix are 10 2 – 2 . 10 0 0 1 –3 –7 0 1 3 Solution Using Eq.(4.93), the characteristic equation for the given matrix is: det > a – OI @ = det 10 – O 0 0 1 – 3 – O –7 = 0 0 1 3–O Expanding the determinant yields the following characteristic equation: 10 – O > – 3 – O 3 – O – – 7 1 @ = 0 Clearly, O 1 = 10 satisfies the above equation and is therefore a root. Next, – 3 – O 3 – O – – 7 1 = 0 or – 9 + O 2 + 7 = 0 , which reduces to O 2 – 2 = 0 , yielding the roots O 2 = eigenvalues are O 1 = 10 , O 2 = . 2 and O 3 = – 2 . 2 and O 3 = – 2 . Thus, the 1 4.19 The moment of inertia I x , I y , and the product of inertia I xy of the cross-sectional area shown in the figure are: I x = 7523 mm4, I y = 3210 mm4, and I xy = – 2640 mm4 The principal moments of inertia are the eigenvalues of the matrix 7523 – 2640 , and the principal axes are in the direction of the eigen– 2640 3210 vectors. Determine the principal moments of inertia by solving the characteristic equation. Determine the orientation of the principal axes of inertia (unit vectors in the directions of the eigenvectors). Solution The eigenvalues of the matrix 7523 – 2640 are the principal moments of inertia. They are determined – 2640 3210 from the roots of the characteristic equation: 7523 – O 3210 – O – – 2640 – 2640 = 0 which simplifies to 17179230 – 10733O + O 2 = 0 . The equation is solved by using the quadratic formula: 2 10733 r 10733 – 4 1 17179230 O 1 2 = ---------------------------------------------------------------------------------------------- or O 1 = 8775.3256 and O 2 = 1957.6744 2 By definition, the eigenvectors corresponding to each eigenvalue must satisfy: i i 7523 – 2640 u 1 – 2640 3210 u i 2 = Oi u1 i u2 Starting with the first eigenvalue: 1 1 1 7523 – 2640 u 1 – 2640 3210 u 1 2 = O1 u1 = 8775.3256 u1 1 1 u2 u2 These two equations are redundant and yield u 11 = – 2.1081u 21 . Since the eigenvector is a unit vector: u 11 2 1 + u2 2 = 1. Combining the two equations yields: u 21 = 0.4286 and u 11 = – 0.9035 Thus, u 1 the eigenvector associated with the eigenvalue O 1 = 8775.3256 1 1 = u 1 ˆi + u 2 jˆ = – 0.9035iˆ + 0.4286 jˆ . Similarly, the eigenvector corresponding to the second eigenvalue is found from: is 2 2 2 2 7523 – 2640 u 1 – 2640 3210 u 2 2 = O1 u1 2 u2 = 1957.6744 u1 2 u2 These redundant equations yield u 22 = 2.1081u 12 . Using this result along with u 12 2 = 0.4286 and u 2 2 2 2 = u 1 ˆi + u 2 jˆ = 0.4286iˆ + 0.9035 jˆ . u1 u . 2 2 2 + u2 2 = 1 yields = 0.9035 . Thus, the eigenvector associated with the eigenvalue O 2 = 1957.6744 is 1 4.20 Determine the principal moments of inertia of the cross-sectional area in Problem 4.19 by using the QR factorization and iteration method. Carry out the first four iterations. Solution This is done in the MATLAB command window to preserve precision: >> c=[7523;-2640]; e=[1;0]; >> norm_c=sqrt((c(1)^2)+(c(2)^2)); v=c+(norm_c*e); >> vT_v=v'*v; v_vT=v*v'; I=eye(2); H=I-((2/vT_v)*v_vT); >> A=[7523 -2640;-2640 3210]; Q1=H; R1=H*A; >> Q1*R1 ans = 7523 -2640 -2640 3210 >> A2=R1*Q1 A2 = 1.0e+003 * 8.6998 0.7135 0.7135 2.0332 >> c=[8699.8;713.5];e=[1;0];norm_c=sqrt((c(1)^2)+(c(2)^2)); >> v=c+(norm_c*e); >> vT_v=v'*v; v_vT=v*v'; I=eye(2); H2=I-((2/vT_v)*v_vT); >> Q2=H2; R2=H2*A2; >> Q2*R2 ans = 1.0e+003 * 8.6998 0.7135 0.7135 2.0332 >> A3=R2*Q2 A3 = 1.0e+003 * 8.7715 -0.1609 -0.1609 1.9615 >> c=[8771.5;-160.9];e=[1;0];norm_c=sqrt((c(1)^2)+(c(2)^2)); >> v=c+(norm_c*e); >> vT_v=v'*v; v_vT=v*v'; I=eye(2); H3=I-((2/vT_v)*v_vT); >> Q3=H3; R3=H3*A3; >> Q3*R3 ans = 1.0e+003 * 8.7715 -0.1609 -0.1609 1.9615 >> A4=R3*Q3 A4 = 1.0e+003 * 8.7751 0.0359 2 0.0359 1.9579 >> c=[8775.1;35.9]; e=[1;0]; norm_c=sqrt((c(1)^2)+(c(2)^2)); >> v=c+(norm_c*e); >> vT_v=v'*v; v_vT=v*v'; I=eye(2); H4=I-((2/vT_v)*v_vT); >> Q4=H4; R4=H4*A4; >> Q4*R4 ans = 1.0e+003 * 8.7751 0.0359 0.0359 1.9579 >> A5=R4*Q4 A5 = 1.0e+003 * 8.7753 -0.0080 -0.0080 1.9577 >> From the above MATLAB session, it can be seen that the following sequence of matrices is generated, the last one approaching an upper triangular matrix: 7523 – 2640 o 8699.8 713.5 o 8771.5 – 160.9 o 8775.1 35.9 o 8775.3 – 8 – 2640 3210 713.5 2033.2 – 160.9 1961.5 35.9 1957.9 – 8 1957.7 Thus, the approximate values of the eigenvalues are O 1 = 8775.3 and O 2 = 1957.7 , which agrees well with the answers obtained in Problem 4.19. . 1 4.21 The structure of a CO2 molecule may be idealized as three masses connected by two springs, where the masses are the carbon and oxygen atoms and the springs represent the chemical bond between the carbon and oxygen atoms.The equation of motion for each atom (mass) may be written as: 2 d x1 - = – kx 1 + kx 2 m O --------dt 2 2 d x2 - = – 2kx 2 + kx 1 + kx 3 m C --------dt 2 2 d x3 - = kx 2 – kx 3 m O --------dt 2 where k is the restoring force spring constant representing the C–O bonds. Since the molecule is free to iZt vibrate, normal mode (i.e., along the axis) vibrations can be examined by substituting x j = A j e , where A j is the amplitude of the jth mass, i = – 1 , Z is the frequency, and t is time. This results in the following system of equations: k k – Z 2 A 1 = – ------- A 1 + ------- A 2 mO mO 2k k k – Z 2 A 2 = – ------- A 2 + ------- A 1 + ------- A 3 mC mC mC k k – Z 2 A 3 = ------- A 2 – ------- A 3 mO mO (1.1) (a) Rewrite the system of equations in Eq. (4.145) as an eigenvalue problem, and show that the quantity Z 2 is the eigenvalue. (b) Write the characteristic equation and solve (analytically) for the different frequencies. 2 – 27 (c) If k = 14.2 u 10 kg/s2, m O = 16amu and m C = 12amu ( amu = 1.6605 u 10 kg), find the wave8 lengths O = 2Sc --------- (where c = 3 u 10 m/s is the speed of light) that correspond to the frequencies from Z part (b). It turns out that one of the wavelengths ( O = 4.3 Pm) is observed in the absorption spectrum of the CO2 molecule. (d) Determine the eigenvectors corresponding to the eigenvalues found in parts (b) and (c). From the genuflections, deduce the relative motion of the atoms (i.e., are they moving towards, or away from each other.) Solution (a) The system of equations (4.145) can be re-written in the following matrix form: k k ------- – -----mO mO 0 A1 A1 2 2k k -----k - – ------- A 2 = Z A 2 – ------mC mC mC A3 A3 k k 0 – ------- ------mO mO 2 A1 It can be seen from this form that Z 2 is the eigenvalue and A 2 is the eigenvector corresponding to that A3 eigenvalue. (b) The system in part (a) can be re-written as: k – mO Z 2 –k –k 2k – m C Z 0 –k 0 2 A1 –k k – mO Z 2 0 A2 = 0 0 A3 k – mO Z Non-trivial solutions for the amplitudes are possible only if det 2 –k –k 2k – m C Z 0 –k 0 2 = 0 , which –k k – mO Z 2 reduces to: 2 k – m O Z > 2k – m C Z 2 2 k – m O Z ^ 2k – m C Z or k – mO Z 2 k – mO Z 2 k – mO Z 2 2 – k @ + k > –k k – mO Z @ = 0 2 2 – 2k ` = 0 , which simplifies further to: 2 2 – 2km O – m C k + m C m O Z Z = 0 which yields three roots for Z : Z 1 = 0 , Z 2 = (c) 2 k ------- , and Z 3 = mO 2m O k ------- 1 + ---------. mC mO For k = 14.2 u 10 2 kg/s2, m O = 16amu and m C = 12amu ( amu = 1.6605 u 10 –27 kg), the values of the frequencies are: Z 1 = 0 , Z 2 = 2.3119 u 10 14 s –1 , and Z 3 = 4.4269 u 10 14 s –1 . The latter two frequen–6 –6 cies correspond to wavelengths of O 2 = 8.2 u 10 m or O 2 = 8.2 Pm and O 3 = 4.3 u 10 m or O 3 = 4.3 Pm . It turns out that one of the wavelengths ( O = 4.3 Pm) is observed in the absorption spectrum of the CO2 molecule. k k ------- – -----mO mO 0 A1 A1 2 2k k k (d) To find the eigenvectors, the system – ------- ------- – ------- A 2 = Z A 2 must be solved for the amplitudes mC mC mC A3 A3 k k 0 – ------- ------mO mO 3 k –k 0 A1 0 for each of the eigenvalues found in part (c). For Z 1 = 0 , – k 2k – k A 2 = 0 yields A 1 = A 2 = A 3 . 0 –k k A3 0 This represents translation of the entire molecule, with no relative motion between the atoms. For 0 Z2 = k – ------mO 0 A1 0 2k k k k k ------- , – ------- ------- – ------- – ------- A 2 = 0 which yields A 2 = 0 and A 1 = – A 3 . This represents a symmetmO mC mC mO mC 0 A3 k k k – ------- – ------- ------mO mO mO ric vibration where the carbon atom is stationary and the oxygen atoms are moving in opposite directions. k 2k – ------- – ------mC mO For Z 3 = 0 A1 0 2m O 2m O k k k k ------- 1 + ---------, – ------ – ------- – ------- A 2 = 0 which yields A 1 = A 3 and A 2 = – ---------- A 1 . This repmC mO mC mC mO mC 0 A3 k 2k 0 – ------- – ------mO mC resents an asymmetric vibration where the carbon atom’s motion is offset by the two moving oxygen atoms. . 1 4.22 The three-dimensional state of stress at a point is given by the stress tensor: 30 15 20 V ij = 15 22 26 20 26 40 The principal stresses and the principal directions at the point are given by the eigenvalues and the eigenvectors. Use the power method for determining the value of the largest principal stress. Start with a column vector of 1s, and carry out the first three iterations. Solution The calculations are easily carried out in the MATLAB command window: >> sigma=[30 15 20;15 22 26;20 26 40]; >> x1=[1;1;1]; x2=sigma*x1 x2 = 65 63 86 >> x2=x2/86;x3=sigma*x2 x3 = 53.6628 53.4535 74.1628 >> x3=x3/74.1628; x4=sigma*x3 x4 = 52.5188 52.7104 73.2113 >> x4=x4/73.2113; x5=sigma*x4 x5 = 52.3204 52.5999 73.0666 >> It can be seen that the estimate for the largest eigenvalue (i.e. largest principal stress) is 73.0666. The exact answer turns out to be 73.0310. 1 4.23 Modify the user-defined function GaussPivot in Program 4-2 (Example 4-3) such that in each step of the elimination the pivot row is switched with the row that has a pivot element with the largest absolute numerical value. For the function name and arguments use x = GaussPivotLarge(a,b), where a is the matrix of coefficients, b is the right-hand-side column of constants, and x is the solution. (a) Use the GaussPivotLarge function to solve the system of linear equations in Eq. (4.17). (b) Use the GaussPivotLarge function to solve the system: 0 3 8 3 0 3 3 12 0 1 0 0 8 –4 0 0 4 5 –5 8 10 0 –6 0 –1 5 –3 0 0 0 6 –2 7 4 2 –6 x1 x2 x3 x4 x5 x6 34 20 = 45 36 60 28 Solution The listing of the user-defined function GaussPivotLarge is: function x = GaussPivotLarge(a,b) % The function solve a system of linear equations ax=b using the Gauss % elimination method with pivoting. In each step the pivot element has the % largest absolute numerical value. % Input variables: % a The matrix of coefficients. % b A column vector of constants. % Output variable: % x A colum vector with the solution. ab = [a,b]; [R, C] = size(ab); for j = 1:R-1 % Pivoting section starts. pvtemp=ab(j,j); kpvt=j; % Looking for the row with the largest pivot element. for k=j+1:R if ab(k,j)~=0 & abs(ab(k,j)) > abs(pvtemp) pvtemp=ab(k,j); kpvt=k; end end % If a row with a larger pivot element exists, switch the rows. 2 if kpvt~=j abTemp=ab(j,:); ab(j,:)=ab(kpvt,:); ab(kpvt,:)=abTemp; end % Pivoting section ends for i = j+1:R ab(i,j:C) = ab(i,j:C)-ab(i,j)/ab(j,j)*ab(j,j:C); end end x = zeros(R,1); x(R) = ab(R,C)/ab(R,R); for i = R-1:-1:1 x(i)=(ab(i,C)-ab(i,i+1:R)*x(i+1:R))/ab(i,i); end (a) The following program (script file) uses the user-defined GaussPivotLarge function to solve the system of linear equations in Eq. (4.17). % Solution of HW4_23a, script clear all a=[0 0.9231 0 0 0 0 0 0; -1 -0.3846 0 0 0 0 0 0 0 0 0 0 1 0 0.8575 0; 1 0 -0.7809 0 0 0 0 0 0 -0.3846 -0.7809 0 -1 0.3846 0 0; 0 0.9231 0.6247 0 0 -0.9231 0 0 0 0 0.6247 -1 0 0 0 0; 0 0 0 1 0 0 -0.5145 -1]; b=[1690;3625;0;0;0;0;0;0]; x = GaussPivotLarge(a,b) When the program is executed, the following result is displayed in the Command Window. >> format short e x = -4.3291e+003 1.8308e+003 -5.5438e+003 -3.4632e+003 2.8862e+003 -1.9209e+003 -3.3659e+003 -1.7315e+003 (b) The following program (script file) uses the user-defined GaussPivotLarge function to solve the 3 system of linear equations that is given in the problem statement. % Solution of HW4_23b, script clear all a =[0 3 8 -5 -1 6; 3 12 -4 8 5 -2; 8 0 0 10 -3 7 3 1 0 0 0 4; 0 0 4 -6 0 2; 3 0 5 0 0 -6]; b = [34; 20; 45; 36; 60; 28]; x = GaussPivotLarge(a,b) When the program is executed, the following result is displayed in the Command Window. >> format short x = -67.1576 -284.9310 202.6158 168.6108 672.6847 130.6010 . 1 4.24 Write a user-defined MATLAB function that solves a system of n linear equations, > a @ > x @ = > b @ , with the Gauss–Jordan method. The program should include pivoting in which the pivot row is switched with the row that has a pivot element with the largest absolute numerical value. For the function name and arguments use x = GaussJordan(a,b), where a is the matrix of coefficients, b is the right-hand-side column of constants, and x is the solution. (a) Use the GaussJordan function to solve the system: 4x 1 + 3x 2 + 2x 3 + x 4 = 1 x 1 + 4x 2 + 3x 3 + 2x 4 = 1 2x 1 + 3x 2 + 4x 3 + 3x 4 = – 1 x 1 + 2x 2 + 3x 3 + 4x 4 = – 1 (b) Use the GaussJordan function to solve the system: 0 3 8 3 0 3 3 12 0 1 0 0 8 –4 0 0 4 5 –5 8 10 0 –6 0 –1 5 –3 0 0 0 6 –2 7 4 2 –6 x1 x2 x3 x4 x5 x6 34 20 = 45 36 60 28 Solution The listing of the user-defined function GaussJordan is: function x = GaussJordan(a,b) % The function solve a system of linear equations ax=b using the Gauss % elimination method with pivoting. In each step the rows are switched % such that pivot element has the largest absolute numerical value. % Input variables: % a The matrix of coefficients. % b A column vector of constants. % Output variable: % x A colum vector with the solution. ab = [a,b]; [R, C] = size(ab); for j = 1:R % Pivoting section starts pvtemp=ab(j,j); kpvt=j; % Looking for the row with the largest pivot element. for k=j+1:R if ab(k,j)~=0 & abs(ab(k,j)) > abs(pvtemp) 2 pvtemp=ab(k,j); kpvt=k; end end % If a row with a larger pivot element exists, switch the rows. if kpvt~=j abTemp=ab(j,:); ab(j,:)=ab(kpvt,:); ab(kpvt,:)=abTemp; end % Pivoting section ends ab(j,:)= ab(j,:)/ab(j,j); for i = 1:R if i~=j ab(i,j:C) = ab(i,j:C)-ab(i,j)*ab(j,j:C); end end end x=ab(:,C) (a) The following program (script file) uses the user-defined GaussJordan function to solve the system of linear equations. % Solution of HW4_24a, script clear all a=[4 3 2 1; 1 4 3 2; 2 3 4 3; 1 2 3 4]; b=[1; 1; -1;-1]; x = GaussJordan(a,b) When the program is executed, the following result is displayed in the Command Window. x = -0.0000 1.0000 -1.0000 0.0000 (b) The following program (script file) uses the user-defined GaussJordan function to solve the system of linear equations that is given in the problem statement. % Solution of HW4_24b, script 3 clear all %a=[4 3 2 1; 1 4 3 2; 2 3 4 3; 1 2 3 4]; %b=[1; 1; -1;-1]; a=[0 3 8 -5 -1 6; 3 12 -4 8 5 -2; 8 0 0 10 -3 7 3 1 0 0 0 4; 0 0 4 -6 0 2; 3 0 5 0 0 -6]; b=[34; 20; 45; 36; 60; 28]; x = GaussJordan(a,b) When the program is executed, the following result is displayed in the Command Window. x = -67.1576 -284.9310 202.6158 168.6108 672.6847 130.6010 . 1 4.25 Write a user-defined MATLAB function that determines the inverse of a matrix using the Gauss–Jordan method. For the function name and arguments use Ainv = Inverse(A), where A is the matrix to be inverted, and Ainv is the inverse of the matrix. Use the Inverse function to calculate the inverse of the matrix: – 0.04 0.04 0.12 0.56 – 1.56 0.32 – 0.24 1.24 – 0.28 Solution The listing of the user-defined function Inverse is: function Ainv = Inverse(A) % The function solve a system of linear equations ax=b using the Gauss % elimination method. % Input variable: % A The matrix to be inverted. % Output variable: % Ainv the inverse of A. [R, C] = size(A); b=eye(R); ab = [A,b]; CM=2*C; for j = 1:R ab(j,:)= ab(j,:)/ab(j,j); for i = 1:R if i~=j ab(i,j:CM) = ab(i,j:CM)-ab(i,j)*ab(j,j:CM); end end end Ainv=ab(:,R+1:CM); The function is used in the Command Window fir determining the inverse of the given function: >> B = [-0.04 0.04 0.12; 0.56 -1.56 0.32; -0.24 1.24 -0.28] B = -0.0400 0.0400 0.1200 0.5600 -1.5600 0.3200 -0.2400 1.2400 -0.2800 >> Binv = Inverse(B) Binv = 1.0000 4.0000 5.0000 2.0000 1.0000 2.0000 8.0000 1.0000 1.0000 2 To check, B and Binv are multiplied: >> B*Binv ans = 1.0000 0.0000 0 . 0 1.0000 0 -0.0000 0.0000 1.0000 1 4.26 Write a user-defined MATLAB function that calculates the 1-norm of any matrix. For the function name and arguments use N = OneNorm(A), where A is the matrix and N is the value of the norm. Use the function for calculating the 1-norm of: 6 3 11 (a) The matrix A = 3 2 7 3 2 6 (b) The matrix 6 B = 3 3 –5 3 –2 2 7 11 7 –6 1 –1 0 5 –4 2 4 . –3 0 Solution The user-defined function is: function N = OneNorm(A) [R,C]=size(A); for j=1:C Column_sums(j) = sum(abs(A(:,j))); end max = Column_sums(1); for j=1:C-1 if Column_sums(j+1) > max max = Column_sums(j+1); end end N = max; When the user-defined function is executed in the command window for the two given matrices, the following are the results: (a) >> A=[6 3 11;3 2 7;3 2 6]; >> ans=OneNorm(A) ans = 24 (b) >> B=[6 3 11 -1 2;3 -2 7 0 4;3 2 -6 5 -3;-5 7 1 -4 0]; >> ans=OneNorm(B) ans = 25 1 4.27 Write a user-defined MATLAB function that calculates the infinity norm of any matrix. For the function name and arguments use N = InfinityNorm(A), where A is the matrix, and N is the value of the norm. Use the function for calculating the infinity norm of: (a) 6 3 11 The matrix A = 3 2 7 . 3 2 6 (b) 6 The matrix B = 3 3 –5 3 –2 2 7 11 7 –6 1 –1 0 5 –4 2 4 . –3 0 Solution The user-defined function is: function N = InfinityNorm(A) [R,C]=size(A); for i=1:R Row_sums(i) = sum(abs(A(i,:))); end max = Row_sums(1); for i=1:R-1 if Row_sums(i+1) > max max = Row_sums(i+1); end end N = max; When the user-defined function is executed in the command window for the two given matrices, the following are the results: (a) >> A=[6 3 11;3 2 7;3 2 6]; >> ans=InfinityNorm(A) ans = 20 (b) >> B=[6 3 11 -1 2;3 -2 7 0 4;3 2 -6 5 -3;-5 7 1 -4 0]; >> ans=InfinityNorm(B) ans = 23 1 4.28 Write a user-defined MATLAB function that calculates the condition number of an n u n matrix by using the 1-norm. For the function name and arguments use c = CondNumb(A), where A is the matrix and c is the value of the condition number. Within the function, use the user-defined functions Inverse from Problem 4.25 and OneNorm from Problem 4.26. Use the function CondNumb for calculating the condition number of the matrix of the coefficients in Problem 4.25. Solution The listing of the user-defined function CondNumb is: function c=CondNumb(A) Ainv = Inverse(A); One_norm_A = OneNorm(A); One_norm_Ainv = OneNorm(Ainv); c = One_norm_A*One_norm_Ainv; The user-defined function is used in the Command Window for calculating the condition number of the – 0.04 0.04 0.12 matrix: 0.56 – 1.56 0.32 – 0.24 1.24 – 0.28 >> B = [-0.04 0.04 0.12; 0.56 -1.56 0.32; -0.24 1.24 -0.28] B = -0.0400 0.0400 0.1200 0.5600 -1.5600 0.3200 -0.2400 1.2400 -0.2800 >> c=CondNumb(B) c = 31.2400 1 4.29 Write a user-defined MATLAB function that determines the largest eigenvalue of an n u n matrix by using the power method. For the function name and argument use e = MaxEig(A), where A is the matrix and e is the value of the largest eigenvalue. Use the function MaxEig for calculating the largest eigenvalue of the matrix of Problem 4.22. Check the answer by using MATLAB’s built-in function for finding the eigenvalues of a matrix. Solution The listing of the user-defined function MaxEig is: function e = MaxEig(A) [R,C]=size(A); %start with arbitrary non-zero vector transpose([1 1 1]) xx(1:R)=1; x=xx'; xold = x; iter = 1; reltol = 1.e-6; criterion = 1; max_old = 1; while criterion > reltol xnew = A*xold; n = length(xnew); max=xnew(1); for i=1:n-1 if abs(xnew(i+1)) > abs(max) max = xnew(i+1); end end xnew = xnew/max; criterion = (abs(max-max_old))/abs(max); xold = xnew; max_old = max; end e=max; The user defined function MaxEig is used in the Command Window for calculating the largest eigenvalue of the matrix of Problem 4.22: 30 15 20 V ij = 15 22 26 20 26 40 >> Sigma=[30 15 20;15 22 26;20 26 40]; >> e = MaxEig(sigma) e = 73.0310 To check the answer, the eigenvalues of the matrix are calculated with MATLAB’s built-in function eig: >> eMTLB=eig(Sigma) eMTLB = 2 3.4612 15.5078 73.0310 The result shows that the function MaxEig calculated the largest eigenvalue correctly. . 1 4.30 Write a user-defined MATLAB function that determines the smallest eigenvalue of an n u n matrix by using the inverse power method. For the function name and argument use e = MinEig(A), where A is the matrix and e is the value of the smallest eigenvalue. Inside MinEig use the user-defined function Inverse, that was written in Problem 4.25, for calculating the inverse of the matrix A. Use the function MinEig for calculating the smallest eigenvalue of the matrix of Problem 4.22. Check the answer by using MATLAB’s built-in function for finding the eigenvalues of a matrix. Solution The listing of the user-defined function MinEig is: function e = MinEig(A) Ainv = Inverse(A); one_over_e = MaxEig(Ainv); e = 1/one_over_e; The listing of the user defined functions Inverse (from Problem 4.25) and MaxEig (from Problem 4.29) that are being used in MinEig is: function Ainv = Inverse(A) % The function solve a system of linear equations ax=b using the Gauss % elimination method. % Input variable: % A The matrix to be inverted. % Output variable: % Ainv the inverse of A. [R, C] = size(A); b=eye(R); ab = [A,b]; CM=2*C; for j = 1:R ab(j,:)= ab(j,:)/ab(j,j); for i = 1:R if i~=j ab(i,j:CM) = ab(i,j:CM)-ab(i,j)*ab(j,j:CM); end end end Ainv=ab(:,R+1:CM); function e = MaxEig(A) [R,C]=size(A); %start with arbitrary non-zero vector transpose([1 1 1]) xx(1:R)=1; x=xx'; xold = x; 2 iter = 1; reltol = 1.e-6; criterion = 1; max_old = 1; while criterion > reltol xnew = A*xold; n = length(xnew); max=xnew(1); for i=1:n-1 if abs(xnew(i+1)) > abs(max) max = xnew(i+1); end end xnew = xnew/max; criterion = (abs(max-max_old))/abs(max); xold = xnew; max_old = max; end e=max; The user defined function MinEig is used in the Command Window for calculating the smallest eigenvalue of the matrix of Problem 4.22: 30 15 20 V ij = 15 22 26 20 26 40 >> Sigma=[30 15 20;15 22 26;20 26 40]; >> emin = MinEig(Sigma) emin = 3.4612 To check the answer, the eigenvalues of the matrix are calculated with MATLAB’s built-in function eig: >> eMTLB=eig(Sigma) eMTLB = 3.4612 15.5078 73.0310 The result shows that the function MinEig calculated the smallest eigenvalue correctly. 1 4.31 Write a user-defined MATLAB function that determines all the eigenvalues of an n u n matrix by using the QR factorization and iteration method. For the function name and argument use e=AllEig(A), where A is the matrix and e is a vector whose elements are the eigenvalues. Use the function AllEig for calculating the eigenvalues of the matrix of Problem 4.22. Check the answer by using MATLAB’s built-in function for finding the eigenvalues of a matrix. Solution The listing of the user-defined function AllEig is: function e = AllEig(A) % AllEig determines the eigenvalues of a n by n matrix A using the Q R % factorization and iteration method. % Input argument: % A A matrix % Output argument: % e A column vector with the values of the eigenvalues. n = size(A); for k = 1:100 [Q R] = QRFactorization(A); A = R*Q; e = diag(A); DffDiaTol = min(abs(e))*0.001; % Checking if the off-diagonal elements are small enough. con = 0; for i=1:n for j=1:n if i ~= j & abs(A(i,j)) > DffDiaTol con = 1; break end end if con == 1 break end end if con == 0 break end end The user-defined function AllEig uses the user-defined function QRFactorization that was written in Example 4-15 (Program 4-10) for the QR factorization. The iterations are carried out until the absolute value of the largest off-diagonal element is smaller than 0.001 times the absolute value of the smallest 2 eigenvalue. The listing of the user-defined function QRFactorization is: function [Q R] = QRFactorization(R) % The function factors a matrix [A] into an orthogonal matrix [Q] % and an upper-triangular matrix [R]. % Input variables: % A The (square) matrix to be factored. % Output variables: % Q Orthogonal matrix. % R Upper-triangular matrix. nmatrix = size(R); n = nmatrix(1); I = eye(n); Q = I; for j = 1:n-1 c = R(:,j); c(1:j-1) = 0; e(1:n,1)=0; if c(j) > 0 e(j) = 1; else e(j) = -1; end clength = sqrt(c'*c); v = c + clength*e; H = I - 2/(v'*v)*v*v'; Q = Q*H; R = H*R; end The program is used in the Command Window to solve the problem: >> Sig=[30 15 20; 15 22 26; 20 26 40] Sig = 30 15 20 15 22 26 20 26 40 >> PrincipalStresses = AllEig(Sig) PrincipalStresses = 73.0310 15.5078 3.4612 >> % Solving with MATLAB built-in function >> PrincipalStressesMATLAB = eig(Sig) 3 PrincipalStressesMATLAB = 3.4612 15.5078 73.0310 . 1 4.32 The axial force F i in each of the 13 member pin-connected truss, shown in the figure, can be calculated by solving the following system of 13 equations: F 2 + 0.7071F 1 = 0 , – F 2 + F 6 = 0 , F 3 – 2000 = 0 F 4 + 0.6585F 5 – 0.7071F 1 = 0 0.7071F 1 + F 3 + 0.7526F 5 + 1000 = 0 F 7 + 0.6585F 8 – F 4 = 0 , 0.7526F 8 + F 9 + 500 = 0 F 10 – 0.6585F 5 – F 6 = 0 , F 12 + 0.7526F 8 – 2000 = 0 , F 9 + 0.7526F 5 – 4000 = 0 0.7071F 11 – F 7 = 0 , 0.7071F 11 + F 12 + 500 = 0 F 13 + 0.7071F 11 = 0 (a) Solve the system of equations using the user-defined function GaussPivotLarge developed in Problem 4.23. (b) Solve this system using Gauss–Seidel iteration. Does the solution converge for a starting (guess) vector whose elements are all zero? (c) Solve the system of equations using MATLAB left division operation. Solution The listing of the user-defined function GaussPivotLarge is: function x = GaussPivotLarge(a,b) % The function solve a system of linear equations ax=b using the Gauss % elimination method with pivoting. In each step the pivot element has the % largest absolute numerical value. % Input variables: % a The matrix of coefficients. % b A column vector of constants. % Output variable: % x A colum vector with the solution. ab = [a,b]; [R, C] = size(ab); for j = 1:R-1 % Pivoting section starts. pvtemp=ab(j,j); kpvt=j; % Looking for the row with the largest pivot element. for k=j+1:R if ab(k,j)~=0 & abs(ab(k,j)) > abs(pvtemp) pvtemp=ab(k,j); kpvt=k; end end 2 % If a row with a larger pivot element exists, switch the rows. if kpvt~=j abTemp=ab(j,:); ab(j,:)=ab(kpvt,:); ab(kpvt,:)=abTemp; end % Pivoting section ends for i = j+1:R ab(i,j:C) = ab(i,j:C)-ab(i,j)/ab(j,j)*ab(j,j:C); end end x = zeros(R,1); x(R) = ab(R,C)/ab(R,R); for i = R-1:-1:1 x(i)=(ab(i,C)-ab(i,i+1:R)*x(i+1:R))/ab(i,i); end (a) The following program (script file) uses the user-defined GaussPivotLarge function to solve the system of linear equations that is given in the problem statement. % Solution of HW4_32a clear all A=[0.7071 1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -0.7071 0 0 1 0.6585 0 0 0 0 0 0 0 0 0.7071 0 1 0 0.7526 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 1 0.6585 0 0 0 0 0 0 0 0 0 0 0 0 0.7526 1 0 0 0 0 0 0 0 0 -0.6585 -1 0 0 0 1 0 0 0 0 0 0 0 0.7526 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 1 0 0 0 0 0 0 0 0 0.7526 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0.7071 0 1]; b=[0; 0; 2000; 0; -1000; 0; -500; 0; 4000; 0; -500; 2000; 0]; F = GaussPivotLarge(A,b) When the program is executed, the following result is displayed in the Command Window. 3 >> format short e F = -7.2361e+003 5.1167e+003 2.0000e+003 -6.9687e+003 2.8125e+003 5.1167e+003 -4.8833e+003 -3.1668e+003 1.8833e+003 6.9687e+003 -6.9061e+003 4.3833e+003 4.8833e+003 (b) A solution of the system with the Gauss–Seidel iteration method does not converge. (c) The following program (script file) uses MATLAB left division operation to solve the system of linear equations that is given in the problem statement. % Solution of HW4_32c clear all A=[0.7071 1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -0.7071 0 0 1 0.6585 0 0 0 0 0 0 0 0 0.7071 0 1 0 0.7526 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 1 0.6585 0 0 0 0 0 0 0 0 0 0 0 0 0.7526 1 0 0 0 0 0 0 0 0 -0.6585 -1 0 0 0 1 0 0 0 0 0 0 0 0.7526 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 1 0 0 0 0 0 0 0 0 0.7526 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0.7071 0 1]; b=[0; 0; 2000; 0; -1000; 0; -500; 0; 4000; 0; -500; 2000; 0]; xMTLB=A\b When the program is executed, the following result is displayed in the Command Window. xMTLB = -7.2361e+003 4 5.1167e+003 2.0000e+003 -6.9687e+003 2.8125e+003 5.1167e+003 -4.8833e+003 -3.1668e+003 1.8833e+003 6.9687e+003 -6.9061e+003 4.3833e+003 4.8833e+003 . 1 4.33 Mass spectrometry of a sample gives a series of peaks that represent various masses of ions of constituents within the sample. For each peak, the height of the peak I i is influenced by the amounts of the various constituents: N Ij = ¦C n ij j i=1 where C ij is the contribution of ions of species i to the height of peak j and n j is the amount of ions or concentration of species j. The coefficients C ij for each peak are given by: Peak identity Species CH4 C2H4 C2H6 C3H6 C3H8 1 0.2 0.2 0.3 0.2 0.2 2 28 1 0 0 0.1 18 12 2.4 16 0 1 10 2 3 4 10 5 6 18 If a sample produces a mass spectrum with peak heights, I 1 = 3.4 , I 2 = 20.5 , I 3 = 170 , I 4 = 49 , I 5 = 39.8 , I 6 = 96.3. Determine the concentrations of the different species in the sample. Solution This is one of those instances where it is possible to obtain a unique solution despite having more equations than unknowns. The problem is solved by setting up the following matrix equation: 0.2 28 0 0 0 0 0.2 1 18 0 0 0 0.3 0 12 10 0 0 0.2 0 2.4 0 10 0 0.2 0.1 16 1 2 18 n CH4 n C2 H4 n C2 H6 n C3 H6 n C3 H8 3.4 20.5 = 170 49 39.8 96.3 The system can be solved easily using MATLAB’s left division command: >> A=[0.2 0.2 0.3 0.2 0.2;28 1 0 0 0.1;0 18 12 2.4 16;0 0 10 0 1;0 0 0 10 2;0 0 0 0 18]; >> b=[3.4;20.5;170;49;39.8;96.3]; >> x=A\b x = 0.6634 1.3909 4.3650 2.9100 5.3500 2 Thus, the concentrations of the different species in the sample are: n CH4 = 0.6634 , n C2 H4 = 1.3909 , n C2 H6 = 4.3650 , n C3 H6 = 2.9100 , and n C3 H8 = 5.3500 . . 1 4.34 The axial force F i in each of the 21 member pin connected truss, shown in the figure, can be calculated by solving the following system of 21 equations: – F 1 – 0.7071F 3 = 0 , – F 2 + F 6 – 0.7071F 3 = 0 F 5 + 0.7071F 3 = 0 , – F 5 – 0.7071F 7 = 0 – F 4 + F 8 + 0.7071F 7 = 0 , – F 6 + F 10 – 0.7071F 7 = 0 F 9 + 0.7071F 7 = 0 , 0.9806F 14 + 0.7071F 15 – 0.7071F 11 – F 9 = 0 0.1961F 14 + 0.7071F 15 + 0.7071F 11 + F 13 – F 8 = 0 – F 10 – 0.7071F 11 = 0 , F 12 + 0.7071F 11 = 0 – F 12 + F 16 = 0 , F 13 = 0 , 0.9806F 18 – 0.9806F 14 + 0.7433F 19 = 0 , 0.1961F 18 – 0.1961F 14 + F 17 + 0.669F 19 = 8000 F 20 – F 16 – 0.7071F 15 = 0 , – F 17 – 0.7071F 15 = 0 , – F 20 – 0.7433F 19 = – 5000 , 0.9806F 11 = 10000 cos 60q , F 21 – 0.1961F 18 = 10000 sin 60q – F 21 – 0.669F 19 = 0 (a) Solve the system of equations using the user-defined function GaussJordan developed in Problem 4.24. (b) Solve the system of equations using MATLAB’s left division operation. Solution The listing of the user-defined function GaussJordan is: function x = GaussJordan(a,b) % The function solve a system of linear equations ax=b using the Gauss % elimination method with pivoting. In each step the rows are switched % such that pivot element has the largest absolute numerical value. % Input variables: % a The matrix of coefficients. % b A column vector of constants. % Output variable: % x A column vector with the solution. ab = [a,b]; [R, C] = size(ab); for j = 1:R % Pivoting section starts pvtemp=ab(j,j); kpvt=j; % Looking for the row with the largest pivot element. for k=j+1:R if ab(k,j)~=0 & abs(ab(k,j)) > abs(pvtemp) pvtemp=ab(k,j); 2 kpvt=k; end end % If a row with a larger pivot element exists, switch the rows. if kpvt~=j abTemp=ab(j,:); ab(j,:)=ab(kpvt,:); ab(kpvt,:)=abTemp; end % Pivoting section ends ab(j,:)= ab(j,:)/ab(j,j); for i = 1:R if i~=j ab(i,j:C) = ab(i,j:C)-ab(i,j)*ab(j,j:C); end end end x=ab(:,C); (a) The following program (script file) uses the user-defined GaussJordan function to solve the system of linear equations that is given in the problem statement. % Solution of HW4_34a, script file clear all A=[-1 0 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -0.7071 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0.7071 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -0.7071 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -0.7071 0 0 0.9806 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0.7071 0 1 0.1961 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.9806 0 0 0 0.9806 0.7433 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.1961 0 0 1 0.1961 0.669 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.7071 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.7071 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.7433 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.669 0 -1 0 0 0 0 0 0 0 0 0 0 0.9806 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.1961 0 0 1]; b=[0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 8000; 0; 0; -5000; 0; 1000*cosd(60); 10000*sind(60)]; c = GaussJordan(A,b) When the program is executed, the following result is displayed in the Command Window. >> format short e c = -7.3676e+004 -1.4771e+005 1.0419e+005 5.7376e+004 -7.3676e+004 -7.4036e+004 1.0419e+005 -1.6300e+004 -7.3676e+004 -3.6054e+002 5.0989e+002 -3.6054e+002 0 -7.2218e+004 -3.5332e+003 -3.6054e+002 2.4983e+003 -8.0232e+004 1.0573e+004 -2.8589e+003 -7.0733e+003 (b) The following program (script file) uses MATLAB’s left division operation to solve the system of linear equations that is given in the problem statement. % Solution of HW4_34b, script file clear all A=[-1 0 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -0.7071 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0.7071 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0.7071 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -0.7071 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -0.7071 0 0 0.9806 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0.7071 0 1 0.1961 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.9806 0 0 0 0.9806 0.7433 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.1961 0 0 1 0.1961 0.669 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.7071 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.7071 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.7433 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.669 0 -1 0 0 0 0 0 0 0 0 0 0 0.9806 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.1961 0 0 1]; b=[0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 8000; 0; 0; -5000; 0; 1000*cosd(60); 10000*sind(60)]; c=A\b When the program is executed, the following result is displayed in the Command Window. c = -7.3676e+004 -1.4771e+005 1.0419e+005 5.7376e+004 -7.3676e+004 -7.4036e+004 1.0419e+005 -1.6300e+004 -7.3676e+004 -3.6054e+002 5.0989e+002 -3.6054e+002 0 -7.2218e+004 -3.5332e+003 -3.6054e+002 5 2.4983e+003 -8.0232e+004 1.0573e+004 -2.8589e+003 -7.0733e+003 . 1 4.35 A rigid bar ABC is supported by 3 pin-connected bars as shown. A force P = 40 kN is applied to the rigid bar at a distance d from A. The forces in the bars, F AD , F BE , and F CG can be determined from the solution of the following system of three equations: F AD + F BE + F CG = P , 10F BE + 16F CG = d P 10L CG 16L BE 6L AD -F - F + ------------------------------------= 0 F – -----------------E AD A AD AD E BE A BE BE E CG A CG CG where L, E, and A, denote the length, elastic modulus, and cross-sectional area, respectively, of the bars. FL -. Once the force in each of the bars is known, its elongation G can be determined with the formula G = -----EA Write a MATLAB program in a script file that determines the forces in the three bars and their elongation for 0 d d d 16 . The program displays the three forces as a function of d in one plot, and the elongation of the bars as a function of d in a second plot (two plots on the same page). Also given: L AD = 4 m, L BE = 5 m, E CG = 115 GPa, L CG = 4 m, E AD = 70 GPa, E BE = 200 GPa, and A AD = A BE = A CG = 5 10 m2. –5 Solution The following program in a script file solves the problem. % Solution of HW 4-35 (script file) clear all LA=4; LB=5; LC=2; EA=70E9; EB=200E9; EC=115E9; AA=5E-5; AB=5E-5;AC=5E-5; P=40E3; d2=10; d3=16; d1=0:16; n=length(d1); for i=1:n a=[1 1 1; 0 d2 d3; (d3-d2)*LA/(EA*AA) -d3*LB/(EB*AB) d2*LC/(EC*AC)]; c=[P; d1(i)*P; 0]; F=a\c; %S=sum(F) Ftot(:,i)=F; del(1,i)=F(1)*LA/(EA*AA); del(2,i)=F(2)*LB/(EB*AB); del(3,i)=F(3)*LC/(EC*AC); end subplot(2,1,1) plot(d1,Ftot(1,:),d1,Ftot(2,:),d1,Ftot(3,:)) xlabel('d (m)'), ylabel('Force (N)') legend('FAD','FBE','FCG',4) subplot(2,1,2) 2 plot(d1,del(1,:),d1,del(2,:),d1,del(3,:)) xlabel('Elongation (m)'), ylabel('Force (N)') legend('Delta AD','Delta BE','Delta CG') When the program is executed, the following plots are displayed in the Command Window. 4 4 x 10 3 Force (N) 2 1 0 FAD FBE −1 FCG −2 0 2 4 6 8 d (m) 10 12 14 16 0.04 Delta AD Delta BE Delta CG Force (N) 0.03 0.02 0.01 0 −0.01 0 2 4 6 8 10 Elongation (m) 12 14 16 1 4.36 In dairy science and technology, ice cream is made such that it contains a desired amount of milk fat, milk solids–nonfat (msnf), water, sugar, stabilizers, and emulsifiers. Suppose 10 kg of an ice cream mix is to be made containing 18% fat, 9.5% msnf, 15% sucrose, 0.4% stabilizer, and 1% egg yolk, using the following ingredients: cream (containing 35% fat), milk (containing 3.5% fat), skim milk powder (containing 97% msnf), sucrose, stabilizer, and egg yolk. Determine how much (in kg) of each ingredient is used. Assume that 9% of the skim portion of the milk and cream contain msnf. (Hint: Set up a system of three equations where the unknowns are the amount of skim milk powder (x), the amount of milk (y), and the amount of cream (z) using (1) the fact that all the components have to add up to 10 kg, (2) writing an equation for the total content of msnf, and (3) writing a balance equation for the fat.) Solution Determine the amount of sucrose: 15 kg 10 kg of mix u ---------------------------------- = 1.5 kg 100 kg of mix Determine the amount of stabilizer: 0.4 kg 10 kg of mix u ---------------------------------- = 0.04 kg 100 kg of mix Determine the amount of egg yolk: 1 kg 10 kg of mix u ---------------------------------- = 0.1 kg 100 kg of mix Since all the components must add up to 10 kg, the first equation is: x + y + z = 10 – 1.5 + 0.04 + 0.1 = 8.36 The second equation comes from the total content of milk solids-nonfat (msnf): 0.09 100 – 3.5 0.09 100 – 35 0.97 x + ------------------------------------------- y + ----------------------------------------- z = 0.095 10 100 100 or 0.97 x + 0.08685 y + 0.0585 z = 0.95 The third equation comes from the total fat balance: 0.035 y + 0.35 z = 0.18 10 = 1.8 Thus, the system to be solved is: 1 1 1 x 8.36 0.97 0.08685 0.0585 y = 0.95 0 0.035 0.35 z 1.8 This is solved with MATLAB in the Command Window : >> A=[1 1 1;0.97 0.08685 0.0585;0 0.035 0.35]; >> b=[8.36;0.95;1.8]; >> ans=A\b ans = 0.4086 3.1206 4.8308 1 4.37 The currents, i 1 i 2 i 3 i 4 i 5 i 6 , in the circuit that is shown can be determined from the solution of the following system of equations. (Obtained by applying Kirchhoff’s law.) 8i 1 – 4i 2 – i 4 = 20 , – 4i 1 + 11.5i 2 – 2.5i 3 – 5i 5 = – 12 – 2.5i 2 + 4.5i 3 – 2i 6 = 14 , – i 1 + 3i 4 – 2i 5 = 8 – 5i 2 – 2i 4 + 8.5i 5 – 1.5i 6 = – 30 , – 2i 3 – 1.5i 5 + 8i 6 = 0 Solve the system using the following methods. (a) Use the user-defined function GaussJordan that was developed in Problem 4.24. (a) Use MATLAB’s built-in functions. Solution The listing of the user-defined function GaussJordan is: function x = GaussJordan(a,b) % The function solve a system of linear equations ax=b using the Gauss % elimination method with pivoting. In each step the rows are switched % such that pivot element has the largest absolute numerical value. % Input variables: % a The matrix of coefficients. % b A column vector of constants. % Output variable: % x A column vector with the solution. ab = [a,b]; [R, C] = size(ab); for j = 1:R % Pivoting section starts pvtemp=ab(j,j); kpvt=j; % Looking for the row with the largest pivot element. for k=j+1:R if ab(k,j)~=0 & abs(ab(k,j)) > abs(pvtemp) pvtemp=ab(k,j); kpvt=k; end end % If a row with a larger pivot element exists, switch the rows. if kpvt~=j 2 abTemp=ab(j,:); ab(j,:)=ab(kpvt,:); ab(kpvt,:)=abTemp; end % Pivoting section ends %ab ab(j,:)= ab(j,:)/ab(j,j); for i = 1:R if i~=j ab(i,j:C) = ab(i,j:C)-ab(i,j)*ab(j,j:C); end end % ab end x=ab(:,C); (a) and (b) The following program (script file) solves the system of equation with the user-defined function GaussJordan (part (a)) and with MATLAB’s left division operation (part (b)). % Solution of HW4_37a, script file clear all A=[8 -4 0 -1 0 0; -4 11.5 -2.5 0 -5 0; 0 -2.5 4.5 0 0 -2 -1 0 0 3 -2 0; 0 -5 0 -2 8.5 -1.5; 0 0 -2 0 -1.5 8]; b=[20; -12; 14; 8; -30; 0]; disp('Part (a)') i = GaussJordan(A,b) disp('Part (b)') iMATLAB=A\b When the program is executed, the following result is displayed in the Command Window. Part (a) i = 1.0470e+000 -2.7576e+000 1.2689e+000 -5.9397e-001 -5.4144e+000 -6.9798e-001 Part (b) iMATLAB = 1.0470e+000 3 -2.7576e+000 1.2689e+000 -5.9397e-001 -5.4144e+000 -6.9798e-001 . 1 4.38 When balancing the following chemical reaction by conserving the number of atoms of each element between reactants and products: P 2 I 4 + aP 4 + bH 2 O m o cPH 4 I + dH 3 PO 4 the unknown stoichiometric coefficients a, b, c, and d are given by the solution of the following system of equations: –4 0 0 0 0 0 –2 –1 1 1 4 0 1 0 3 4 a 2 b = 4 c 0 0 d Solve for the unknown stoichiometric coefficients using (a) The user-defined function GaussJordan that was developed in Problem 4.24. (b) MATLAB’s left division operation. Solution (a) The user-defined function GaussJordan that was developed in Problem 4.24 is used in the following script file to solve the problem. % Solution of HW4_38a, script file clear all A=[-4 0 1 1; 0 0 1 0; 0 -2 4 3; 0 -1 0 4]; b=[2; 4; 0; 0]; c = GaussJordan(A,b) When the program is executed, the following result is displayed in the Command Window: c = 1.3000 12.8000 4.0000 3.2000 (b) The following script file to solve the problem by using MATLAB’s left division. % Solution of HW4_38b, script file clear all A=[-4 0 1 1; 0 0 1 0; 0 -2 4 3; 0 -1 0 4]; b=[2; 4; 0; 0]; c=A\b When the program is executed, the following result is displayed in the Command Window: c = 2 1.3000 12.8000 4.0000 3.2000 . 1 4.39 When balancing the following chemical reaction by conserving the number of atoms of each element between reactants and products: Cr N 2 H 4 CO 6 4 Cr CN 6 3 + aKMnO 4 + bH 2 SO 4 o cK 2 Cr 2 O 7 + dMnSO 4 + eCO 2 + fKNO 3 + gK 2 SO 4 + hHO 2 the unknown stoichiometric coefficients a through h are given by the solution of the following system of equations: 0 0 0 0 –4 –1 –1 0 0 0 –2 0 –4 0 0 –1 2 0 0 0 7 2 0 0 0 0 0 0 4 0 1 1 0 0 0 1 2 0 0 0 0 1 0 0 3 1 0 0 0 0 0 0 4 2 0 1 0 0 2 0 1 0 0 0 a 7 b 66 c 96 d = 42 24 e 0 f 0 g 0 h Solve for the unknown stoichiometric coefficients using (a) The user-defined function GaussJordan that was developed in Problem 4.24. (b) MATLAB’s left division operation. Solution (a) The listing of the user-defined function GaussJordan is: function x = GaussJordan(a,b) % The function solve a system of linear equations ax=b using the Gauss % elimination method with pivoting. In each step the rows are switched % such that pivot element has the largest absolute numerical value. % Input variables: % a The matrix of coefficients. % b A column vector of constants. % Output variable: % x A column vector with the solution. ab = [a,b]; [R, C] = size(ab); for j = 1:R % Pivoting section starts pvtemp=ab(j,j); kpvt=j; % Looking for the row with the largest pivot element. for k=j+1:R if ab(k,j)~=0 & abs(ab(k,j)) > abs(pvtemp) 2 pvtemp=ab(k,j); kpvt=k; end end % If a row with a larger pivot element exists, switch the rows. if kpvt~=j abTemp=ab(j,:); ab(j,:)=ab(kpvt,:); ab(kpvt,:)=abTemp; end % Pivoting section ends %ab ab(j,:)= ab(j,:)/ab(j,j); for i = 1:R if i~=j ab(i,j:C) = ab(i,j:C)-ab(i,j)*ab(j,j:C); end end % ab end x=ab(:,C); The following program written in a script file uses the user-defined function to solve the problem. % Solution of HW4_39a, script file clear all A=[0 0 2 0 0 0 0 0; 0 0 0 0 0 1 0 0; 0 -2 0 0 0 0 0 2 0 0 0 0 1 0 0 0; -4 -4 7 4 2 3 4 1; -1 0 2 0 0 1 2 0 -1 0 0 1 0 0 0 0; 0 -1 0 1 0 0 1 0]; b=[7; 66; 96; 42; 24; 0; 0; 0]; c = GaussJordan(A,b) When the program is executed the following results are displayed in the Command Window: c = 117.6000 139.9000 3.5000 117.6000 42.0000 66.0000 3 22.3000 187.9000 (b) The following program written in a script file solves the problem with MATLAB’s left division operation. % Solution of HW4_39b, script file clear all A=[0 0 2 0 0 0 0 0; 0 0 0 0 0 1 0 0; 0 -2 0 0 0 0 0 2 0 0 0 0 1 0 0 0; -4 -4 7 4 2 3 4 1; -1 0 2 0 0 1 2 0 -1 0 0 1 0 0 0 0; 0 -1 0 1 0 0 1 0]; b=[7; 66; 96; 42; 24; 0; 0; 0]; c=A\b When the program is executed the following results are displayed in the Command Window: c = 117.6000 139.9000 3.5000 117.6000 42.0000 66.0000 22.3000 187.9000 1 4.40 Write a user-defined MATLAB function that determines the principal stresses and the directions of the principal stresses for a given three-dimensional state of stress. For the function name and arguments, use [Ps Pd] = PrinplStre(S), where S is a 3 u 3 matrix with the values of the stress tensor, Ps is a column vector with the values of the principal stresses, and Pd is a 3 u 3 matrix in which each row lists a unit vector in a principal direction. Use MATLAB built-in functions. Use the function for determining the principal stresses and principal directions for the state of stress given in Problem 4.22: 30 15 20 V ij = 15 22 26 . 20 26 40 Solution The principal stresses are the eigenvalues of the matrix and the directions of the principal stresses are the eigenvalues. The user-defined function PrinplStre first calculates the eigenvalues and then the eigenvectors. The eigenvalues are calculated with a user-defined function named AllEig (see listing below) which uses the QR factorization and iteration method. The eigenvectors are calculated by using the following procedure: Denote the first eigenvalue by O 1 (known) and the corresponding unknown eigenvector by u 1 = > u 11 u 21 u 31 @ .Then: V 11 V 12 V 13 u 11 u 11 V 21 V 22 V 23 u 21 = O u 21 V 31 V 32 V 33 u 31 u 31 or V 11 – O 1 V 12 V 13 u 11 V 21 V 22 – O 1 V 23 u 21 V 31 V 32 V 33 – O 1 u 31 0 = 0 0 By setting u 11 = 1 , the following two equations can be written: V 22 – O 1 u 21 + V 23 u 31 = – V 21 V 32 u 21 + V 33 – O 1 u 31 = – V 31 These two equations can be solved for u 21 and u 31 . The eigenvector is then: u 1 = > 1 u 21 u 31 @ . The eigenvector is then divided by its length to give a unit vector in the direction of the eigenvector. The second eigenvalue and eigenvector are: O 2 (known) and u related by: V 11 – O 2 V 12 V 13 u 12 V 21 V 22 – O 2 V 23 u 22 V 31 V 32 V 33 – O 2 u 32 2 = > u 12 u 22 u 32 @ (unknown). They are 0 = 0 0 By setting u 22 = 1 , the following two equations can be written: V 11 – O 2 u 12 + V 13 u 32 = – V 12 V 31 u 12 + V 33 – O 2 u 32 = – V 32 These two equations can be solved for u 12 and u 32 . The eigenvector is then: u 2 = > u 12 1 u 32 @ . The eigenvector is then divided by its length to give a unit vector in the direction of the eigenvector. 2 The third eigenvalue and eigenvector are: O 3 (known) and u related by: 3 V 11 – O 3 V 12 V 13 u 13 V 21 V 22 – O 3 V 23 u 23 V 31 V 32 V 33 – O 3 u 33 = > u 13 u 23 u 33 @ (unknown). They are 0 = 0 0 By setting u 33 = 1 , the following two equations can be written: V 11 – O 3 u 13 + V 12 u 23 = – V 13 V 21 u 13 + V 22 – O 3 u 23 = – V 23 These two equations can be solved for u 13 and u 23 . The eigenvector is then: u 3 = > u 13 u 23 1 @ . The eigenvector is then divided by its length to give a unit vector in the direction of the eigenvector. The listing of the user-defined function PrinplStre is: function [Ps Pd] = PrinplStre(S) % PrinplStre calculates the principal stresses and principal directions % of a given state of stress. % Input argument: % S A 3 by 3 matrix of the stress tensor. % Output arguments: % Ps A column vector with the values of the principal stresses. % Pd A 3 by 3 matrix in which each row lists a unit vector in a principal % direction. I=eye(3); Ps = AllEig(S); % Once the eigenvalues are found, the eigenvectors are u(:,1) corresponding % to the first eigenvalue, u(:,2) corresponding to the second, and u(:,2) % corresponding to the second. % The first component of the first eigenvector is set to be equal to 1. % The second component of the second eigenvectoe is set to be equal to 1. % The third component of the second eigenvectoe is set to be equal to 1. u(1,1)=1; u(2,2)=1; u(3,3)=1; % The other components of the eigenvectors are determined by using the % above definitions along with [S-Psi*I][u(:,i)]=[0]: M=S-(Ps(1)*I); First=M(2:3,2:3); rhs=-M(2:3,1); u(2:3,1)=First\rhs; M=S-(Ps(2)*I); Second=M([1,3],[1,3]); rhs=-M([1,3],2); u([1,3],2)=Second\rhs; M=S-(Ps(3)*I); Third=M(1:2,1:2); rhs=-M(1:2,3); 3 u([1:2],3)=Third\rhs; % Assign the eigenvectors to Pd. Pd=[u(:,1) u(:,2) u(:,3)]; % Normalize the eigenvectors so they are unit vectors: Pd(:,1)=Pd(:,1)./sqrt(sum(Pd(:,1).^2)); Pd(:,2)=Pd(:,2)./sqrt(sum(Pd(:,2).^2)); Pd(:,3)=Pd(:,3)./sqrt(sum(Pd(:,3).^2)); The user-defined function AllEig that is used in PrinplStre for determining the eigenvalues uses the QR factorization and iteration method for determining the eigenvalues. It uses the user-defined function QRFactorization that was written in Example 4-15 (Program 4-10) for the QR factorization. The iterations are carried out until the absolute value of the largest off-diagonal element is smaller than 0.001 times the absolute value of the smallest eigenvalue. The listing of the user-defined function eigQR is: function e = AllEig(A) % AllEig determines the eigenvalues of a n by n matrix A using the Q R % factorization and iteration method. % Input argument: % A A matrix % Output argument: % e A column vector with the values of the eigenvalues. n = size(A); for k = 1:100 [Q R] = QRFactorization(A); A = R*Q; e = diag(A); DffDiaTol = min(abs(e))*0.001; % Checking if the off-diagonal elements are small enough. con = 0; for i=1:n for j=1:n if i ~= j & abs(A(i,j)) > DffDiaTol con = 1; break end end if con == 1 break end 4 end if con == 0 break end end The listing of the user-defined function QRFactorization that is used inside AllEig (was written in Example 4-15 (Program 4-10)) is: function [Q R] = QRFactorization(R) % The function factors a matrix [A] into an orthogonal matrix [Q] % and an upper-triangular matrix [R]. % Input variables: % A The (square) matrix to be factored. % Output variables: % Q Orthogonal matrix. % R Upper-triangular matrix. nmatrix = size(R); n = nmatrix(1); I = eye(n); Q = I; for j = 1:n-1 c = R(:,j); c(1:j-1) = 0; e(1:n,1)=0; if c(j) > 0 e(j) = 1; else e(j) = -1; end clength = sqrt(c'*c); v = c + clength*e; H = I - 2/(v'*v)*v*v'; Q = Q*H; R = H*R; end The problem is solved in the Command Window: 5 >> S=[30 15 20; 15 22 26; 20 26 40] S = 30 15 20 15 22 26 20 26 40 >> [Ps Pd] = PrinplStre(S) Ps = 73.0310 15.5078 3.4612 Pd = 0.5023 -0.8638 0.0406 0.5052 0.2550 -0.8245 0.7018 0.4346 0.5644 >> % Checking the answer with MATLAB's built-in function eig: >> [v d]=eig(S) v = -0.0406 0.8638 0.5023 0.8245 -0.2550 0.5052 -0.5644 -0.4346 0.7018 d = 3.4612 0 0 0 15.5078 0 0 0 73.0310 Note that the eigenvectors obtained by PrincplStre are either identical to those obtained by MATLAB or are the negative of those obtained by MATLAB. This is not a problem since both answers provide an equally valid set of bases vectors for the eigenvectors of a matrix. In other words, eigenvectors of the given matrix can be written as a linear combination of either the columns of Pd or of the columns of v. The differences in the answers arise from which if the variables is arbitrarily chosen to be 1 (see u(1,1)=1; u(2,2)=1; u(3,3)=1; in the above program). 1 4.41 The structure of the C2H2 (acetylene) molecule may be idealized as four masses connected by two springs (see discussion in Problem 4.21). By applying the equation of motion, the following system of equations can be written for the amplitudes of vibration of each atom: k CH --------- – Z 2 mH k CH – -------mH 0 0 k CH – -------mC k CH + k CC ---------------------------- – Z2 mC k CC – -------mC 0 0 k CC – -------mC k CH + k CC ---------------------------- – Z2 mC k CH – -------mC 0 0 k CH – -------mH k CH -------- – Z2 mH A1 A2 A3 A4 0 = 0 0 0 where Z is the frequency, k CH = 5.92 u 10 2 kg/s2 and k CC = 15.8 u 10 2 kg/s2 are the restoring force spring constants representing the C–H and C–C bonds, respectively, m H = 1amu , and m C = 12amu ( amu = 1.6605 u 10 –27 kg) are the masses of the atoms. --------- (where (a) Determine the eigenvalues (frequencies), and the corresponding wavelengths ( O = 2Sc Z 8 c = 3 u 10 m/s is the speed of light). (b) Determine the eigenvectors corresponding to the eigenvalues found in part (a). From the eigenvectors, deduce the relative motion of the atoms (i.e., are they moving towards, or away from each other?) Solution The following script file solves this problem: % Solution of Problem 4.41. Script file. clear all kCH=5.92e2; kCC=15.8e2; mH=1.6605e-27; mC=12*1.6605e-27; c=3e8; M=zeros(4); M(1,1)=kCH/mH; M(1,2)=-M(1,1); M(2,1)=-kCH/mC; M(2,2)=(kCH+kCC)/mC; M(2,3)=-kCC/mC; M(3,2)=M(2,3); M(3,3)=M(2,2); M(3,4)=M(2,1); M(4,3)=M(1,2); M(4,4)=-M(4,3); [evectors values]=eig(M); eigenvalues=diag(values); temp=sqrt(eigenvalues); icount=1; % Looking for the real eigenvalues and corresponding eigenvectors. for i=1:length(temp) if imag(temp(i))==0 number=temp(i); omega(icount)=number; 2 eigenvectors(:,icount)=evectors(:,i); icount=icount+1; end end disp('The real eigenvalues are:') omega disp('The corresponding wavelengths (except for the zero eigenvalue) in meters are:') for i=2:length(omega) lambda=2*pi*c./omega; end lambda disp('The eigenvectors corresponding to the eigenvalues are:') eigenvectors When the script file is executed, the following output is displayed in the Command Window: The real eigenvalues are: omega = 3.7349e+014 6.3665e+014 6.2147e+014 The corresponding wavelengths (except for the zero meters are: lambda = 5.0469e-006 2.9607e-006 3.0330e-006 The eigenvectors corresponding to the eigenvalues are: eigenvectors = -6.0400e-001 7.0057e-001 7.0466e-001 -3.6768e-001 -9.5905e-002 -5.8722e-002 3.6768e-001 9.5905e-002 -5.8722e-002 6.0400e-001 -7.0057e-001 7.0466e-001 eigenvalue) in (b) The columns of the eigenvector matrix are the individual eigenvectors themselves. They represent amplitudes of the motion or vibration of each atom in the C 2 H 2 molecule. The first column represents the eigenvector for the eigenvalue associated with the symmetric stretch of the molecule where the hydrogen atoms at the end and their neighboring carbon atoms are moving apart. This motion corresponds to a wavelength of O = 5.05 Pm in the infrared portion of the electromagnetic spectrum. The second column indicates the leftmost H atom moving to the right, the leftmost C atom moving to the left, the rightmost C atom moving to the right and the rightmost H atom moving to the left. In other words, the H-C and C-H bonds are vibrating such that they are being compressed and the C atoms in the interior are being stretched apart. This corresponds to a wavelength of O = 2.96 Pm , in the near-infrared. The third column is the eigenvector corresponding to the eigenvalue that represents an asymmetric stretch of the molecule. The two C 3 atoms are moving to the left while both H atoms are moving to the right. Thus, the left side of the molecule is being compressed while the right side of the molecule is being stretched. This corresponds to a wavelength of O = 3.03 Pm , also in the infrared. . 1 5.1 The following data is given: x 2 7 y 5 8 6 10 8 11 9 12 13 14 15 15 (a) Use linear least-squares regression to determine the coefficients m and b in the function y = mx + b that best fits the data. (b) Use Eq. (5.5) to determine the overall error. Solution (a) In the equation y = mx + b , m corresponds to a 1 and b corresponds to a 0 in the discussion of Section 5.2.2. Using Eq.(5.13), n ¦x Sx = i = 2 + 5 + 6 + 8 + 9 + 13 + 15 = 58 i=1 n Sy = ¦y i = 7 + 8 + 10 + 11 + 12 + 14 + 15 = 77 i=1 n S xy = ¦x y = 2 7 + 5 8 + 6 10 + 8 11 + 9 12 + 13 14 + 15 15 = 717 i i i=1 n S xx = ¦x 2 i = 2 2 + 5 2 + 6 2 + 8 2 + 9 2 + 13 2 + 15 2 = 604 i=1 Using Eq.(5.14) with n = 7 : nS xy – S x S y 7 717 – 58 77 = ---------------------------------------- = 0.6400 a 1 = ---------------------------nS xx – S x 2 7 604 – 58 2 S xx S y – S xy S x 604 77 – 717 58 a 0 = ------------------------------ = ---------------------------------------------- = 5.6968 2 nS xx – S x 7 604 – 58 2 The linear function that best fit the data is: y = 0.6400x + 5.6968 . (b) Using Eq.(5.5), the overall error is: n n E = ¦r i=1 2 i = ¦ >y – i a1 xi + a0 @ 2 i=1 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 E = 7 – 6.9768 2 + 8 – 8.8968 + 15 – 15.2968 2 2 + 10 – 9.5368 2 + 11 – 10.8168 2 + 12 – 11.4568 2 + 14 – 14.0168 2 E = 1.4363 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.2 The following data is given: x -7 15 y -5 12 -1 5 0 2 2 0 5 -5 6 -9 (a) Use linear least-squares regression to determine the coefficients m and b in the function y = mx + b that best fits the data. (b) Use Eq. (5.5) to determine the overall error. Solution (a) In the equation y = mx + b , m corresponds to a 1 and b corresponds to a 0 in the discussion of Section 5.2.2. Using Eq.(5.13) n Sx = ¦x = –7–5–1–0+2+5+6 = 0 i i=1 n Sy = ¦y i = 15 + 12 + 5 + 2 + 0 – 5 – 9 = 20 i=1 n S xy = ¦x y = – 7 15 – 5 12 – 5 – 5 5 – 6 9 = – 249 i i i=1 n S xx = ¦x 2 i = 7 2 + 5 2 + 1 2 + 2 2 + 5 2 + 6 2 = 140 i=1 Using Eq.(5.14) with n = 7 : 7 – 249 – 0 20 nS xy – S x S y- = ------------------------------------------------= – 1.7786 a 1 = ---------------------------2 nS xx – S x 2 7 140 – 0 S xx S y – S xy S x 140 20 – – 249 0 a 0 = ------------------------------ = -------------------------------------------------------- = 2.8571 2 nS xx – S x 2 7 140 – 0 The linear function that best fit the data is: y = – 1.7786 x + 2.8571 . (b) Using Eq.(5.5), the overall error is: n n E = ¦ i=1 E = 15 – 15.3071 + – 9 + 7.8145 2 2 + 12 – 11.7500 2 r i2 = ¦ >y – i a1 xi + a0 @ 2 i=1 + 5 – 4.6357 2 + 2 – 2.8571 2 + 0 + 0.7000 2 + – 5 + 6.0359 2 E = 3.9929 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.3 The following data gives the approximate population of the world for selected years from 1850 until 2000. Year Population (billions) 1850 1.3 1900 1.6 1950 3 1980 4.4 2000 6 Assume that the population growth can be modeled with an exponential function p = be mx where x is the year and p is the population in billions. Write the equation in a linear form (Section 5.3), and use linear least-squares regression to determine the constants b and m for which the function best fits the data. Use the equation to estimate the population in the year 1970. Solution For p = be mx , take the natural logarithm of both sides to yield ln p = ln b + mx . This equation is in the form of y = a 1 x + a 0 with ln b corresponds to a 0 and m corresponds to a 1 . First, calculate y = ln p : x (year) y ( ln p ) 1850 20.9856 1900 21.1933 1950 21.8219 1980 2000 22.2049 22.5150 Using Eq.(5.13) n Sx = ¦x i = 1850 + 1900 + 1950 + 1980 + 2000 = 9680 i=1 n Sy = ¦y = 20.9856 + 21.1933 + 21.8219 + 22.2049 + 22.5150 = 108.7207 i i=1 n S xy = ¦x y i i = 1850 20.9856 + 1900 21.1933 + 1950 21.8219 + 1980 22.2049 + 2000 22.5150 i=1 = 210639.037 n S xx = ¦x 2 i 7 = 1850 2 + 1900 2 + 1950 2 + 1980 2 + 2000 2 = 1.8755 u10 i=1 Using Eq.(5.14), nS xy – S x S y 5 210639.037 – 9680 108.7207 = ------------------------------------------------------------------------------------------- = 0.0104 a 1 = ---------------------------2 7 2 nS xx – S x 5 1.8755 u10 – 9680 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 7 S xx S y – S xy S x 1.8755 u10 108.7207 – 210639.037 9680 a 0 = ------------------------------ = --------------------------------------------------------------------------------------------------------------------- = 1.5327 2 nS xx – S x 2 5 18755400 – 9680 Thus, the linear least-squares regression yields y = 0.0104 x + 1.5327 . Substituting y = ln p ln p = 0.0104 x + 1.5327 , or: p = e 0.0104 x + 1.5327 = e 1.5327 e 0.0104 x = 4.6306 e gives 0.0104 x which is of the form p = be mx with b = 4.6306 and m = 0.0104 . The population for the year 1970: p 70 = 4.6306 e 0.0104 1970 = 3.66 billions Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.4 The following data is given: x -0.2 5.2 y -0.1 3 0.2 0.6 0.7 0.4 1.3 0.2 1 Determine the coefficients m and b in the function y = --------------- that best fit the data. (Write the equation in mx + b a linear form (Section 5.3), and use linear least-squares regression to determine the value of the coefficients.) Solution 1 The transforming equations from the nonlinear function y = --------------to the linear form are given in Table mx + b 1 5-2 as Y = --- , X = x , m = a 1 and b = a 0 . The transformation yields y X -0.2 -0.1 0.2 0.7 1.3 0.1923 0.3333 1.6667 2.5000 5.0000 Y Using Eq.(5.13) n Sx = ¦x i = – 0.2 – 0.1 + 0.2 + 0.7 + 1.3 = 1.9000 i=1 n Sy = ¦y i = 0.1923 + 0.3333 + 1.6667 + 2.5000 + 5.0000 = 9.6923 i=1 n S xy = ¦x y i i = – 0.2 0.1923 – 0.1 0.3333 + 0.2 1.6667 + 0.7 2.5000 + 1.3 5.0000 = 8.5115 i=1 n S xx = ¦x 2 i = 0.2 2 + 0.1 2 + 0.2 2 + 0.7 2 + 1.3 2 = 2.2700 i=1 Using Eq.(5.14) for n=5, nS xy – S x S y 5 8.5115 – 1.9000 9.6923 a 1 = ---------------------------= ----------------------------------------------------------------------------- = 3.1192 2 nS xx – S x 2 5 2.2700 – 1.9000 S xx S y – S xy S x 2.2700 9.6923 – 8.5115 1.9000 - = -------------------------------------------------------------------------------------------- = 0.7532 a 0 = -----------------------------2 2 nS xx – S x 5 2.2700 – 1.9000 Thus, the linear least-squares regression yields Y = 3.1192 x + 0.7532 , where m = a 1 = 3.1192 and b = a 0 = 0.7532 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.5 The following data is given: x y 1 2.2 3 5.0 5 5.5 mx Determine the coefficients m and b in the function y = ----------b+x 7 6.1 10 6.6 that best fit the data. (Write the equation in a linear form (Section 5.3), and use linear least-squares regression to determine the value of the coefficients.) Solution mx The transforming equations from the nonlinear function y = ----------b+x to the linear form are given in Table 1 1 b 5-2 as Y = 1--- , X = 1--- , a 1 = --- and a 0 = ---- . Writing m and b in terms of a 1 and a 0 gives m = ----- and y x m m a0 b = a 1 m . The calculations are done by executing the following MATLAB program (script file): clear all; clc; x=[1 3 5 7 10]; y=[2.2 5 5.5 6.1 6.6]; % From Table 5-2 Y=1./y; X=1./x; % Equation 5-13 SX=sum(X); SY=sum(Y); SXY=sum(X.*Y); SXX=sum(X.*X); % Equation 5-14 n=length(X); a1=(n*SXY-SX*SY)/(n*SXX-SX^2) a0=(SXX*SY-SXY*SX)/(n*SXX-SX^2) m=1/a0 b=a1*m Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 When the program is executed, the following values are displayed in the Command Window: a1 = 0.3396 a0 = 0.1097 m = 9.1144 b = 3.0955 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.6 To measure g (the acceleration due to gravity) the following experiment is carried out. A ball is dropped from the top of a 30-m-tall building. As the object is falling down, its speed v is measured at various heights by sensors that are attached to the building. The data measured in the experiment is given in the table. x (m) v (m/s) 0 0 5 9.85 10 14.32 15 17.63 20 19.34 25 22.41 In terms of the coordinates shown in the figure (positive down), the speed of the ball v as a function of the distance x is given by v 2 = 2gx . Using linear regression, determine the experimental value of g. Solution The equation v 2 = 2gx can be transformed into linear form by setting Y = v 2 . The resulting equation, Y = 2gx , is linear in Y and x with m = 2g and b = 0 . Therefore, once m is determined, g can be calculating using g = m ---- . The calculations are done by executing the following MATLAB program (script file): 2 clear all; clc; x=[0 5 10 15 20 25]; y=[0 9.85 14.32 17.63 19.34 22.41]; Y=y.^2; X=x; % Equation 5-13 SX=sum(X); SY=sum(Y); SXY=sum(X.*Y); SXX=sum(X.*X); % Equation 5-14 n=length(X); a1=(n*SXY-SX*SY)/(n*SXX-SX^2) a0=(SXX*SY-SXY*SX)/(n*SXX-SX^2) m=a1 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 b=a0 g=m/2 When the program is executed, the following values are displayed in the Command Window: a1 = 19.7019 a0 = 1.9170 m = 19.7019 b = 1.9170 g = 9.8510 Thus, the measured value of g is 9.8510 m/s2. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.7 The atmospheric pressure, p, as a function of height, h, can be modeled by an exponential function of the form p = be –mh . The following are values of pressure measured at different heights. Using linear regression, determine the constants m and h that best fit the data. Use the equation to estimate the atmospheric pressure at a height of 7000 m. h (m) p (Pa) 0 100,000 5,000 47,500 10,000 22.600 15,000 10,800 20,000 5,100 Solution – mh The nonlinear function p = be is transfored to a linear function Y = a 1 X + a 0 (see Table 5-2) by sub- situting Y = ln y , X = x , a 1 = m and a 0 = ln b . Writing m and b in terms of a 1 and a 0 gives – m = a 1 a and b = e 0 . The calculations are done by executing the following MATLAB program (script file): clear all; clc; h=[0 5000 10000 15000 20000]; p=[100000 47500 22600 10800 5100]; X=h; Y=log(p); % Equation 5-13 SX=sum(X); SY=sum(Y); SXY=sum(X.*Y); SXX=sum(X.*X); % Equation 5-14 n=length(X); a1=(n*SXY-SX*SY)/(n*SXX-SX^2) a0=(SXX*SY-SXY*SX)/(n*SXX-SX^2) m=-a1 b=exp(a0) When the program is executed, the following values are displayed in the Command Window: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 a1 = -1.4866e-004 a0 = 11.5129 m = 1.4866e-004 b = 9.9997e+004 4 –4 Therefore, linear regression yields the relation p = 9.9997 u10 exp – 1.4866 u10 h . An estimate of the atmospheric pressure at h = 7000 m, is determined by substituting the value of h into the equation: p 4 h = 7000 –4 = 9.9997 u10 exp – 1.4866 u10 u 7000 p h = 7000 = 35 322 Pa Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.8 In an electrophoretic fiber making process, the diameter of the fiber, d, is related to the current flow, I. The following is measured during production: I (nA) d (Pm) 300 22 300 26 350 27 400 30 400 34 500 33 500 33.5 650 37 650 42 The relationship between the current and the diameter can be modeled with an equation of the form d = a + b I . Use the data to determine the constants a and b that best fit the data. Solution The equation d = a + b I is linear in I and d . The coefficients a and b are found by setting Y = d and X = I and using the method of linear regression. The calculations are done by executing the following MATLAB program (script file): clear all; clc; I=[300 300 350 400 400 500 500 650 650]; d=[22 26 27 30 34 33 33.5 37 42]; X=sqrt(I); Y=d; % Equation 5-13 SX=sum(X); SY=sum(Y); SXY=sum(X.*Y); SXX=sum(X.*X); % Equation 5-14 n=length(X); a1=(n*SXY-SX*SY)/(n*SXX-SX^2); a0=(SXX*SY-SXY*SX)/(n*SXX-SX^2); b=a1 a=a0 When the program is executed, the following values are displayed in the Command Window: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 b = 1.7998 a = -6.1967 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.9 Determine the coefficients of the polynomial y = a 2 x 2 + a 1 x + a 0 that best fit the data given in Problem 5.5. Solution The data points from Problem 5.5 are: x 1 2.2 y 3 5.0 5 5.5 7 6.1 10 6.6 Curve fitting of these data points with the second-order polynomial is done by polynomial regression. The values of the three coefficients a 2 , a 1 and a 0 are determined by solving a system of three linear equations, which given by Eqs. (5.26)-(5.28): § n 2· § n · xi ¸ a2 = x i¸ a 1 + ¨ na 0 + ¨ ©i=1 ¹ ©i=1 ¹ ¦ ¦ n ¦y § n 3· § n 2· § n · xi ¸ a2 = x i¸ a 0 + ¨ xi ¸ a1 + ¨ ¨ ©i=1 ¹ ©i = 1 ¹ ©i = 1 ¹ ¦ ¦ ¦ § n 4· § n 3· § n 2· xi ¸ a2 = xi ¸ a1 + ¨ xi ¸ a0 + ¨ ¨ ©i=1 ¹ ©i = 1 ¹ ©i = 1 ¹ ¦ ¦ ¦ i i=1 n ¦x y i i i=1 n ¦x y 2 i i i=1 The calculations can be performed using a MATLAB script that performs the following steps: Step 1: Create vectors x and y with the data points. Step 2: Create a vector xsum in which the elements are the summation terms of the powers of x i . Step 3: Set up the system of three linear equations in the form > X @ > a @ = > Y @ , where > X @ is the 3 u 3 matrix with the summation terms of the powers of x i , > a @ is the 3 u 1 vector of the unknowns and > Y @ is the 3 u 1 vector of the summation terms on the right-hand side of Eqs. (5.26)-(5.28). Step 4: Solve the system of three linear equations for > a @ using MATLAB’s left division. The solution is a vector with the coefficients of the second-order polynomial that best fits the data. These steps are performed using the following MATLAB script: clear all; clc; % Step 1 x=[1 3 5 7 10]; y=[2.2 5.0 5.5 6.1 6.6]; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 n=length(x); m=4; % Step 2 for i=1:m xsum(i)=sum(x.^(i)); end % Step 3 % First row of matrix [X] and first element of column vector [Y] X(1,1)=n; Y(1,1)=sum(y); for j=2:(m-1) X(1,j)=xsum(j-1); end % Rows 2 and 3 of matrix [X] and column vector [Y] for i=2:(m-1) for j=1:(m-1) X(i,j)=xsum(j+i-2); end Y(i,1)=sum(x.^(i-1).*y); end % Step 4 a=X\Y When the program is executed, the vector > a @ is displayed in the Command Window: a = 1.3540 1.2148 -0.0704 The second-order polynomial that best fits the data is y = – 0.0704 x 2 + 1.1248x + 1.3540a 0 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.10 Using the least-squares method (section 5.8), fit a linear combination of a straight line, sin x , and e x, to the following data: x 0.1 0.61 y 0.4 0.92 0.5 0.99 0.7 1.52 0.7 1.47 0.9 2.03 Solution In the notation of Eq. (5.91) the approximating function is F x = C 1 f 1 x + C 2 f 2 x + C 3 f 3 x , where x f 1 x = e , f 2 x = sin x and f 3 x = Ax + B . Because A and B are constants, the approximating function can be written as F x = C 1 f 1 x + C 2 f 2 x + C 3 f 3 x + C 4 f 4 x , where f 4 x = 1 . The equation therefore has four terms, so m = 4 , and there are six data points, so n = 6 . Substituting this information into Eq. (5.97) gives the following system of four linear equations for C 1 C 2 C 3 C 4 : 6 ¦ C1 e 2x i 6 + i=1 6 ¦ ¦ x C 2 e i sin x i + x 6 ¦ 6 6 ¦ C 2 sin x i 2 + ¦ xi C 3 x i sin x i + ¦ 6 C 4 sin x i = 6 xi 6 4 i 6 6 ¦ C e + ¦ C sin x + ¦ C x + ¦ C 1 i=1 2 i=1 i 3 i i=1 i=1 i i 6 ¦y x = i i i=1 i=1 i=1 i=1 i=1 ¦ y sin x i=1 6 2 3 i i xi i i=1 6 2 i ¦y e i=1 ¦ C x e + ¦ C x sin x + ¦ C x + ¦ C x 1 i 6 = 6 i=1 6 xi C4 e i=1 6 i=1 i=1 ¦ x C3 xi e i + i=1 i=1 C 1 e i sin x i + 6 6 4 = ¦y i i=1 These equations can be rewritten in matrix form: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 6 ¦ e 2x i i=1 6 ¦ 6 ¦ x 6 ¦ sin x i 2 i=1 i=1 6 xi e xi 6 e xi i=1 xi i=1 ¦y e 6 ¦ x i2 ¦ sin x i C 1 i=1 C2 ¦ i=1 C3 6 ¦ i=1 xi xi i 6 x i sin x i 6 sin x i 6 xi 6 i=1 6 ¦ ¦ e i=1 6 x i sin x i 6 i=1 i=1 6 ¦ xi e i=1 ¦ i=1 i=1 ¦ ¦ i=1 e i sin x i ¦ 6 x e i sin x i C4 ¦ y sin x i = i i=1 6 ¦y x i i i=1 6 xi 1 ¦y i i=1 This system is solved using the following MATLAB script. The script solves the system in the form > X @ > C @ = > Y @ , where > X @ is the 4 u 4 matrix with the summation terms, > C @ is the 4 u 1 vector of the unknowns and > Y @ is the 4 u 1 vector on the right-hand side. clear all; clc; x=[0.1 0.4 0.5 0.7 0.7 0.9]; y=[0.61 0.92 0.99 1.52 1.47 2.03]; n=length(x); % Define the matrix [X] X(1,1)=sum(exp(2.*x)); X(1,2)=sum(exp(x).*sin(x)); X(1,3)=sum(x.*exp(x)); X(1,4)=sum(exp(x)); X(2,1)=X(1,2); X(2,2)=sum((sin(x)).^2); X(2,3)=sum(x.*sin(x)); X(2,4)=sum(sin(x)); X(3,1)=X(1,3); X(3,2)=X(2,3); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 X(3,3)=sum(x.^2); X(3,4)=sum(x); X(4,1)=X(1,4); X(4,2)=X(2,4); X(4,3)=X(3,4); X(4,4)=1; % Define the vector [Y] Y(1,1)=sum(y.*exp(x)); Y(2,1)=sum(y.*sin(x)); Y(3,1)=sum(y.*x); Y(4,1)=sum(y); % Solve for [C] C=X\Y When the program is executed, the vector > C @ is displayed in the Command Window: C = 0.5314 -5.2716 5.4136 0.0001 Thus, the equation that best fits the data is F x = 0.5314e x – 5.2716 sin x + 5.4136x + 0.0001 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.11 The fuel economy of a car (miles per gallon) varies with its speed. In an experiment, the following five measurements are obtained: Speed (mph) 10 12 Fuel economy (mpg) 25 26 40 28 55 30 70 24 Determine the fourth-order polynomial in the Lagrange form that passes through the points. Use the polynomial to calculate the fuel economy at 65 mph. Solution Lagrange polynomials are given by Eq.(5.45): n ¦ f x = n n yi Li x = i=1 x – xj ¦ -----------------x –x yi i=1 j = 1 jzi i j Here, the fuel economy is y and the speed is x. Thus, y x = y1 L1 x + y2 L2 x + y3 L3 x + y4 L4 x + y5 L5 x where x – 25 x – 40 x – 55 x – 70 x – 25 x – 40 x – 55 x – 70 L 1 x = -------------------------------------------------------------------------------------------- = ------------------------------------------------------------------------------1215000 10 – 25 10 – 40 10 – 55 10 – 70 x – 10 x – 40 x – 55 x – 70 x – 10 x – 40 x – 55 x – 70 L 2 x = -------------------------------------------------------------------------------------------- = – ------------------------------------------------------------------------------303750 25 – 10 25 – 40 25 – 55 25 – 70 x – 10 x – 25 x – 55 x – 70 x – 10 x – 25 x – 55 x – 70 L 3 x = -------------------------------------------------------------------------------------------- = ------------------------------------------------------------------------------40 – 10 40 – 25 40 – 55 40 – 70 202500 x – 10 x – 25 x – 40 x – 70 x – 10 x – 25 x – 40 x – 70 L 4 x = -------------------------------------------------------------------------------------------- = – ------------------------------------------------------------------------------303750 55 – 10 55 – 25 55 – 40 55 – 70 x – 10 x – 25 x – 40 x – 55 x – 10 x – 25 x – 40 x – 55 L 5 x = -------------------------------------------------------------------------------------------- = ------------------------------------------------------------------------------1215000 70 – 10 70 – 25 70 – 40 70 – 55 Therefore, the 4th order polynomial is: 12 x – 25 x – 40 x – 55 x – 70 y x = ------------------------------------------------------------------------------------- – 1215000 28 x – 10 x – 25 x – 55 x – 70 + ------------------------------------------------------------------------------------- – 202500 26 x – 10 x – 40 x – 55 x – 70 ------------------------------------------------------------------------------------303750 30 x – 10 x – 25 x – 40 x – 70 ------------------------------------------------------------------------------------303750 24 x – 10 x – 25 x – 40 x – 55 + ------------------------------------------------------------------------------------1215000 Evaluating the polynomial at x = 65 mph : Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 26 65 – 10 65 – 40 65 – 55 65 – 70 12 65 – 25 65 – 40 65 – 55 65 – 70 y 65 = -------------------------------------------------------------------------------------------------- – -------------------------------------------------------------------------------------------------303750 1215000 28 65 – 10 65 – 25 65 – 55 65 – 70 30 65 – 10 65 – 25 65 – 40 65 – 70 + -------------------------------------------------------------------------------------------------- – -------------------------------------------------------------------------------------------------202500 303750 24 65 – 10 65 – 25 65 – 40 65 – 55 + -------------------------------------------------------------------------------------------------- = 28.2 1215000 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.12 Determine the fourth-order Newton’s interpolating polynomial that passes through the data points given in Problem 5.11. Use the polynomial to calculate the fuel economy at 65 mph. Solution Write the data in column form as follows, and use the method of divided differences: x y 10 12 14 26 – 12 ---------------------- = -----15 25 – 10 2 14· § ----- – -----© 15 15¹ ----------------------- = – 0.026667 40 – 10 25 26 –4 0 – – 0.026667 -------------------------------------------- = 5.926 u 10 55 – 10 2 28 – 26 ---------------------- = -----15 40 – 25 2 2- ----§ ----– -· © 15 15¹ ----------------------- = 0 55 – 25 40 28 30 – 28 2 ---------------------- = -----55 – 40 15 –4 –4 –5 – 3.95067 u 10 – 5.926 u 10 --------------------------------------------------------------------------------= – 1.646112 u 10 70 – 10 –4 – 0.017778 – 0 ---------------------------------------- = – 3.95067 u 10 70 – 25 2 6- ----§ – ----– -· © 15 15¹ ---------------------------- = – 0.017778 70 – 40 55 30 24 – 30 6 ---------------------- = – -----70 – 55 15 70 24 Thus, the polynomial is 14 –4 y x = 12 + ------ x – 10 – 0.026667 x – 10 x – 25 + 5.926 u 10 x – 10 x – 25 x – 40 15 – 1.646112 u 10 –5 x – 10 x – 25 x – 40 x – 55 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 Verify that the polynomial is correct by checking the value at x = 70 mph : y 70 = 12 + 0.933333 60 – 0.026667 60 45 + 5.926 u 10 –5 – 1.646112 u 10 60 45 30 15 = 24 mpg –4 60 45 30 Evaluating at x = 65 mph : 14 –4 65 – 10 65 – 25 65 – 40 y 65 = 12 + ------ 65 – 10 – 0.026667 65 – 10 65 – 25 + 5.926 u 10 15 – 1.646112 u 10 –5 65 – 10 65 – 25 65 – 40 65 – 55 = 28.2 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.13 The following data is given: x 1 1 y 2.5 7 2 5 3 8 4 2 5 1 (a) Write the polynomial in Lagrange form that passes through the points, then use it to calculate the interpolated value of y at x = 3.5 . (a) Write the polynomial in Newton’s form that passes through the points, then use it to calculate the interpolated value of y at x = 3.5 . Solution (a) Following the form of Eq. (5.44), the Lagrange polynomial for the six given data points is: x – 2.5 x – 2 x – 3 x – 4 x – 5 x– 1 x– 2 x– 3 x – 4 x – 5 f x = ------------------------------------------------------------------------------------------ 1 + ------------------------------------------------------------------------------------------------------------ 7 1 – 2.5 1 – 2 1 – 3 1 – 4 1 – 5 2.5 – 1 2.5 – 2 2.5 – 3 2.5 – 4 2.5 – 5 x – 1 x – 2.5 x – 2 x – 4 x – 5 x – 1 x – 2.5 x – 3 x – 4 x – 5 + ------------------------------------------------------------------------------------------ 5 + ------------------------------------------------------------------------------------------ 8 3 – 1 3 – 2.5 3 – 2 3 – 4 3 – 5 2 – 1 2 – 2.5 2 – 3 2 – 4 2 – 5 x – 1 x – 2.5 x – 2 x – 3 x – 4 x – 1 x – 2.5 x – 2 x – 3 x – 5 + ------------------------------------------------------------------------------------------ 2 + ------------------------------------------------------------------------------------------ 1 5 – 1 5 – 2.5 5 – 2 5 – 3 5 – 4 4 – 1 4 – 2.5 4 – 2 4 – 3 4 – 5 The interpolated value of y at x = 3.5 is obtained by substituting this value of x into the polynomial: 3.5 – 1 3.5 – 2 3.5 – 3 3.5 – 4 3.5 – 5 3.5 – 2.5 3.5 – 2 3.5 – 3 3.5 – 4 3.5 – 5 f 3.5 = ---------------------------------------------------------------------------------------------------------------- 1 + ------------------------------------------------------------------------------------------------------------ 7 1 – 2.5 1 – 2 1 – 3 1 – 4 1 – 5 2.5 – 1 2.5 – 2 2.5 – 3 2.5 – 4 2.5 – 5 3.5 – 1 3.5 – 2.5 3.5 – 3 3.5 – 4 3.5 – 5 3.5 – 1 3.5 – 2.5 3.5 – 2 3.5 – 4 3.5 – 5 + ---------------------------------------------------------------------------------------------------------------- 5 + ---------------------------------------------------------------------------------------------------------------- 8 2 – 1 2 – 2.5 2 – 3 2 – 4 2 – 5 3 – 1 3 – 2.5 3 – 2 3 – 4 3 – 5 3.5 – 1 3.5 – 2.5 3.5 – 2 3.5 – 3 3.5 – 4 3.5 – 1 3.5 – 2.5 3.5 – 2 3.5 – 3 3.5 – 5 + ---------------------------------------------------------------------------------------------------------------- 2 + ---------------------------------------------------------------------------------------------------------------- 1 5 – 1 5 – 2.5 5 – 2 5 – 3 5 – 4 4 – 1 4 – 2.5 4 – 2 4 – 3 4 – 5 f 3.5 = – 0.0156 – 7.0000 + 1.5625 + 11.2500 + 0.6250 – 0.0156 = 6.4063 (b) Newton’s polynomial for the given points has the form: f x = a 1+ a 2 x – 1 + a 3 x – 1 x – 2.5 + a 4 x – 1 x – 2.5 x – 2 + a 5 x – 1 x – 2.5 x – 2 x – 3 + a 6 x – 1 x – 2.5 x – 2 x – 3 x – 4 The coefficients can be found by constructing a divided difference table: xi 1 yi a1 = 1 a2 = 4 a3 = 0 a4 = –1 a 5 = – 0.2222 a 6 = 0.4556 1 7 – 1--------------= 4 2.5 – 1 2.5 7 4–4 ------------ = 0 2–1 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 –2–0 ---------------- = – 1 3–1 5–7 ---------------- = 4 2 – 2.5 2 3–4 ---------------- = – 2 3 – 2.5 5 –-------------------4.5 + 2= – 1.6667 4 – 2.5 8 – 5----------= 3 3–2 3 8 2 1.6 + 0.2222----------------------------= 0.4556 5–1 2.3333 + 1.6667 --------------------------------------- = 1.6 5 – 2.5 –6–3 ---------------- = – 4.5 4–2 2–8 ------------ = – 6 4–3 4 – 1.6667 + 1 ------------------------------ = – 0.2222 4–1 2.5 + 4.5 --------------------- = 2.3333 5–2 –1+6 ---------------- = 2.5 5–3 1–2 ------------ = – 1 5–4 5 1 With the coefficients determined, the polynomial is f x = 1 + 4 x – 1 – 1 x – 1 x – 2.5 x – 2 – 0.2222 x – 1 x – 2.5 x – 2 x – 3 + 0.4556 x – 1 x – 2.5 x – 2 x – 3 x – 4 The interpolated value of y at x = 3.5 is obtained by substituting this value of x into the polynomial: f 3.5 = 1 + 4 3.5 – 1 – 1 3.5 – 1 3.5 – 2.5 3.5 – 2 – 0.2222 3.5 – 1 3.5 – 2.5 3.5 – 2 3.5 – 3 + 0.4556 3.5 – 1 3.5 – 2.5 3.5 – 2 3.5 – 3 3.5 – 4 f 3.5 = 6.4063 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.14 Use linear splines interpolation with the data in Problem 5.11, to calculate the fuel economy at a speed of (a) 30 mph (b) 65 mph. Solution The data in Problem 5.11 is Speed (mph) Fuel economy (mpg) 10 12 25 26 40 28 55 30 70 24 There are five points and thus four splines. Using Eq.(5.65), the equations of the splines are: x – xi + 1 x – xi - y i + ------------------------y f i x = -----------------------xi – xi + 1 xi + 1 – xi i + 1 (a) To estimate the fuel economy at 30 mph, it is only necessary to calculate the linear spline between the points (25 mph, 26 mpg) and (40 mph, 28 mpg): 28 26 x – 25 x – 40 f x = ---------------------- 26 + ---------------------- 28 = – ------ x – 40 + ------ x – 25 15 15 40 – 25 25 – 40 To find the fuel economy at 30 mph, substitute 30 mph for x in the equation: 26 28 f 30 = – ------ 30 – 40 + ------ 30 – 25 15 15 f 30 = 26.6667 mpg (b) To estimate the fuel economy at 65 mph, calculate the linear spline between the points (55 mph, 30 mpg) and (70 mph, 24 mpg): x – 55 x – 70 f x = ---------------------- 24 + ---------------------- 30 = – 1.6 x – 70 + 2 x – 55 70 – 55 55 – 70 To find the fuel economy at 65 mph, substitute 65 mph for x in the equation: f 65 = – 1.6 65 – 70 + 2 65 – 55 f 65 = 28 mpg Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.15 Use quadratic splines interpolation with the data in Problem 5.11, to calculate the fuel economy at a speed of (a) 30 mph (b) 65 mph. Solution The data in Problem 5.11 is Speed (mph) Fuel economy (mpg) 10 12 25 26 40 28 55 30 70 24 There are five points, and therefore four splines. The quadratic equation for the ith spline is fi x = ai x2 + bi x + ci There are four polynomials with three coefficients each, for a total of 12 coefficients. These coefficients are a 1 b 1 c 1 a 2 b 2 c 2 a 3 b 3 c 3 a 4 b 4 and c 4 . The coefficient a 1 is set to zero (i.e. the second derivative at the first point is set to zero, see condition 3 on p. 213). A system of 11 linear equations can be written to find the other 11 unknowns. Eight equations are obtained by applying Eqs. (5.67) and (5.68) to the endpoints of each interval: i = 1 a 1 x 12 + b 1 x 1 + c 1 = y 1 o b 1 10 + c 1 = 12 a 1 x 22 + b 1 x 2 + c 1 = y 2 o b 1 25 + c 1 = 26 i = 2 a 2 x 22 + b 2 x 2 + c 2 = y 2 o a 2 25 2 + b 2 25 + c 2 = 26 a 2 x 32 + b 2 x 3 + c 2 = y 3 o a 2 40 2 + b 2 40 + c 2 = 28 i = 3 a 3 x 32 + b 3 x 3 + c 3 = y 3 o a 3 40 2 + b 3 40 + c 3 = 28 a 3 x 42 + b 3 x 4 + c 3 = y 3 o a 3 55 2 + b 3 55 + c 3 = 30 i = 4 a 4 x 42 + b 4 x 4 + c 4 = y 4 o a 4 55 2 + b 4 55 + c 4 = 30 a 4 x 52 + b 4 x 5 + c 4 = y 5 o a 4 70 2 + b 4 70 + c 4 = 24 The final three equations are obtained from the condition that at the interior knots the slopes (first derivatives) of the polynomials from adjacent intervals are equal. From Eq. (5.70), i = 2 2a 1 x 2 + b 1 = 2a 2 x 2 + b 2 o b 1 – 2 25 a 2 – b 2 = 0 i = 3 2a 2 x 3 + b 2 = 2a 3 x 3 + b 3 o 2 40 a 2 + b 2 – 2 40 a 3 – b 3 = 0 i = 4 2a 3 x 4 + b 3 = 2a 4 x 4 + b 4 o 2 55 a 3 + b 3 – 2 55 a 4 – b 4 = 0 The system of 11 linear equations can be written in matrix form: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 b1 0 0 c1 0 0 25 25 1 0 0 0 0 0 0 a2 0 0 b2 0 0 c2 10 1 0 25 1 0 2 2 0 0 40 40 1 0 0 0 0 0 0 0 2 0 0 40 40 1 0 2 0 0 0 0 0 0 55 55 1 0 0 0 0 0 0 0 0 55 0 1 0 0 0 –1 1 0 0 0 –1 1 0 0 0 0 0 – 50 80 0 0 0 0 0 0 0 – 80 110 2 2 0 0 a3 b 55 1 3 c 70 1 3 a 0 0 4 0 0 b4 0 70 0 0 0 0 0 – 110 – 1 0 c 4 12 26 26 28 28 = 30 30 24 0 0 0 This system of equations can be solved using the following MATLAB script: clear all; clc; A=[10 1 0 0 0 0 0 0 0 0 0; 25 1 0 0 0 0 0 0 0 0 0; 0 0 25^2 25 1 0 0 0 0 0 0; 0 0 40^2 40 1 0 0 0 0 0 0; 0 0 0 0 0 40^2 40 1 0 0 0; 0 0 0 0 0 55^2 55 1 0 0 0; 0 0 0 0 0 0 0 0 55^2 55 1; 0 0 0 0 0 0 0 0 70^2 70 1; 1 0 -50 -1 0 0 0 0 0 0 0; 0 0 80 1 0 -80 -1 0 0 0 0; 0 0 0 0 0 110 1 0 -110 -1 0]; B=[12; 26; 26; 28; 28; 30; 30; 24; 0; 0; 0]; coefficients=A\B When the program is executed, the coefficients are displayed in the Command Window: coefficients = 0.9333 2.6667 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 -0.0533 3.6000 -30.6667 0.0533 -4.9333 140.0000 -0.0889 10.7111 -290.2222 The coefficients for the quadratic splines are as follows: a 1 = 0 , b 1 = 0.9333 , c 1 = 2.6667 , a 2 = – 0.0533 , b 2 = 3.6000 , c 2 = – 30.6667 , a 3 = 0.0533 , b 3 = – 4.9333 , c 3 = 140.0000 , a 4 = – 0.0889 , b 4 = 10.7111 , c 4 = – 290.2222 . (a) To find the fuel economy at 30 mph, consider the polynomial between the points (25 mph, 26 mpg) and (40 mph, 28 mpg). This corresponds to i = 2 , so this polynomial has the coefficients a 2 b 2 and c 2 : f 2 x = – 0.0533 x 2 + 3.6000x – 30.6667 To calculate the fuel economy at 30 mph, substitute x = 30 mph into the equation: f 2 30 = – 0.0533 30 2 + 3.6000 30 – 30.6667 f 2 30 = 29.3633 (b) To find the fuel economy at 65 mph, consider the polynomial between the points (55 mph, 30 mpg) and (70 mph, 24 mpg). This corresponds to i = 4 , so this polynomial has the coefficients a 4 b 4 and c 4 : f 4 x = – 0.0889 x 2 + 10.7111x – 290.2222 To calculate the fuel economy at 65 mph, substitute x = 65 mph into the equation: f 4 65 = – 0.0889 65 2 + 10.7111 65 – 290.2222 f 4 65 = 30.3968 Note that in both parts the quadratic spline interpolation over estimate the fuel economy. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.16 Use natural cubic splines interpolation (based on Lagrange-form polynomials [Eqs. (5.86)–(5.89)]) with the data in Problem 5.11, to calculate the fuel economy at a speed of (a) 30 mph (b) 65 mph. Solution (a) Given the data Speed (mph) Fuel economy (mpg) 10 12 25 26 40 28 55 30 70 24 we seek to fit the function f x given by Eq.(5.88) for the fuel economy in mpg as a function of the speed x in mph using cubic splines. Since the x values are equally spaced, h i = h = 15 mph Eq.(5.89) can be simplified to: y i + 2 – 2y i + 1 + y i for a i + 4a i + 1 + a i + 2 = 6 ---------------------------------------i = 1 2 3 2 h For natural cubic splines, a 1 = 0 and a 5 = 0 . Thus, the above system of equations reduces to: – 0.32 4 1 0 a2 = 0 1 4 1 a3 – 0.2133 0 1 4 a4 This can be solved in the MATLAB command window as follows: >> A=[4 1 0;1 4 1;0 1 4]; b=[-0.32;0;-0.2133]; >> x=A\b x = -0.0895 0.0381 -0.0628 >> Thus, a 1 a 2 a 3 a 4 a 5 = 0 – 0.0895 0.0381 – 0.0628 0 . These are the values of the second derivatives at each point. The approximating cubic spline functions for each interval are found from Eq.(5.88) for the case h i = h = 15 mph . For 30 mph the interval is between > x 2 x 3 @ : a2 - 40 – x f 2 x = ------------6 15 3 a3 - x – 25 + ------------6 15 3 26 a 2 15 28 a 3 15 + ------ – --------------- x – 25 - 40 – x + ------ – --------------15 15 6 6 or f 2 x = – 9.9444 u 10 –4 40 – x 3 + 4.2333 u 10 –4 x – 25 3 + > 1.9571 @ 40 – x + > 1.7714 @ x – 25 . Substi- Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 tuting x = 30 mph yields f 2 30 = 27.49 mpg . (b) For 65 mph the interval is between > x 4 x 5 @ : a4 - 70 – x f 4 x = ------------6 15 f 4 x = – 6.9778 u 10 f 4 65 = 26.70 mpg . or –4 3 a5 - x – 55 + ------------6 15 70 – x 3 3 30 a 4 15 24 a 5 15 - 70 – x + ------ – --------------+ ------ – --------------- x – 55 6 15 6 15 + > 2.157 @ 70 – x + > 1.6 @ x – 55 . Substituting x = 65 mph yields Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.17 Modify the MATLAB user-defined function LinearRegression in Program 5-1. In addition to determining the constants a 1 and a 0 the modified function should also calculate the overall error E according to Eq. (5.6). For function name use [a, Er] = LinReg(x,y). The input arguments x and y are vectors with the coordinates of the data points. The output argument a is a two-element vector with the values of the constants a 1 and a 0 . The output argument Er is the value of the overall error. (a) Use the function to solve Example 5-1. (b) Use the function to solve Problem 5.2. Solution The listing of the user-defined function LinReg is: function [a,Er] = LinReg(x, y) % LinReg calculates the coefficients a1 and a0 of the linear % equation y = a1*x + a0 that best fits n data points, and the overall % error according to Eq. (5.6). % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % Output variables: % a Two elements vector with the coefficients a1 and a0. % Er The overall error. nx = length(x); ny = length(y); if nx ~= ny disp('ERROR: The number of elements in x must be the same as in y.') a = 'Error'; Er = 'Error'; else Sx = sum(x); Sy = sum(y); Sxy = sum(x.*y); Sxx = sum(x.^2); a1 = (nx*Sxy - Sx*Sy)/(nx*Sxx - Sx^2); a0 = (Sxx*Sy - Sxy*Sx)/(nx*Sxx - Sx^2); a=[a1; a0]; % Eq. (5.6) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 Er=sum((y-(a1.*x+a0)).^2); end (a) The user-defined function LinReg is used in the Command Window to solve Problem 5-1. >> x=[2 5 6 8 9 13 15]; >> y=[7 8 10 11 12 14 15]; >> [a,Er]=LinReg(x,y) a = 0.6400 5.6968 Er = 1.4363 (b) The user-defined function LinReg is used in the Command Window to solve Problem 5-2. >> x=[-7 -5 -1 0 2 5 6]; >> y=[15 12 5 2 0 -5 -9]; >> [a,Er]=LinReg(x,y) a = -1.7786 2.8571 Er = 3.9929 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.18 Write a MATLAB user-defined function that determines the best fit of an exponential function of the – mx form y = be to a given set of data points. Name the function [b m]= ExpoFit(x,y), where the input arguments x and y are vectors with the coordinates of the data points, and the output arguments b and m are the values of the coefficients. The function ExpoFit should use the approach that is described in Section 5.3 for determining the value of the coefficients. Use the function to solve Problem 5.7. Solution The listing of the user-defined function ExpoFit is: function [b,m] = ExpoFit(x, y) % ExpoFit calculates the coefficients b and m of the exponential % equation y = b*exp(-m*x) that best fits n data points. % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % Output variables: % b The coefficient b. % m The coefficient m. nx = length(x); ny = length(y); if nx ~= ny disp('ERROR: The number of elements in x must be the same as in y.') b = 'Error'; m = 'Error'; else Y=log(y); X=x; SX = sum(X); SY = sum(Y); SXY = sum(X.*Y); SXX = sum(X.^2); a1 = (nx*SXY - SX*SY)/(nx*SXX - SX^2); a0 = (SXX*SY - SXY*SX)/(nx*SXX - SX^2); b=exp(a0); m=-a1; end The user-defined function ExpoFit is used to solve Problem 5-7 in the Command Window: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 >> h=[0 5000 10000 15000 20000]; >> p=[100000 47500 22600 10800 5100]; >> [b,m]=ExpoFit(h,p) b = 9.9997e+004 m = 1.4866e-004 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.19 Write a MATLAB user-defined function that determines the best fit of a power function of the form m y = bx to a given set of data points. Name the function [b m]= PowerFit(x,y), where the input arguments x and y are vectors with the coordinates of the data points, and the output arguments b and m are the values of the coefficients. The function PowerFit should use the approach that is described in Section 5.3 for determining the value of the coefficients. Use the function to solve Problem 5.7. Solution The listing of the user-defined function PowerFit is: function [b,m] = PowerFit(x, y) % PowerFit calculates the coefficients b and m of the exponential % equation y = b*x^m that best fits n data points. % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % Output variables: % b The coefficient b. % m The coefficient m. nx = length(x); ny = length(y); if nx ~= ny disp('ERROR: The number of elements in x must be the same as in y.') b = 'Error'; m = 'Error'; else Y=log(y); X=log(x); SX = sum(X); SY = sum(Y); SXY = sum(X.*Y); SXX = sum(X.^2); a1 = (nx*SXY - SX*SY)/(nx*SXX - SX^2); a0 = (SXX*SY - SXY*SX)/(nx*SXX - SX^2); b=exp(a0); m=a1; end The user-defined function PowerFit is used in the Command Window to fit a power function to the data from Problem 5.3: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 >> x=[1850 1900 1950 1980 2000]; >> y=[1.3 1.6 3 4.4 6]*10^9; >> [b,m] = PowerFit(x, y) b = 3.6897e-057 m = 20.0431 The power function that best fits the data from Problem 5.3 is: y = 3.6897 u10 – 57 20.0431 x . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.20 Write a MATLAB user-defined function that determines the coefficients of a quadratic polynomial, f x = a 2 x 2 + a 1 x + a 0 , that best fits a given set of data points. Name the function a = QuadFit(x,y), where the input arguments x and y are vectors with the coordinates of the data points, and the output argument a is a three-element vector with the values of the coefficients a 2 , a 1 and a 0 . (a) Use the function to find the quadratic polynomial that best fits the data in Example 5-2. (b) Write a program in a script file that plots the data points and the curve of the quadratic polynomial that best fits the data. Solution The listing of the user-defined function QuadFit is: function a = QuadFit(x, y) % QuadFit calculates the coefficients a2,a1 and a0 of the quadratic % polynomial that best fits n data points. % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % Output variables: % a A vector with the values of a2,a1 and a0 % The vector [a] is determined by solving the system [X][a]=[Y] nx = length(x); ny = length(y); m = 3; %number of coefficients if nx ~= ny disp('ERROR: The number of elements in x must be the same as in y.') a = 'Error'; else for i=1:(m+1) xsum(i)=sum(x.^(i)); end % First row of matrix [X] and first element of column vector [Y] X(1,1)=nx; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 Y(1,1)=sum(y); for j=2:m X(1,j)=xsum(j-1); end % Rows 2 and 3 of matrix [X] and column vector [Y] for i=2:m for j=1:m X(i,j)=xsum(j+i-2); end Y(i,1)=sum(x.^(i-1).*y); end a=X\Y; end (a) The user-defined function QuadFit is used in the Command Window to find the quadratic polynomial that best fits the data from Example 5-2: >> t=[2 4 6 8 10 12 14 16 18 20 22 24 26 28 30]; >> V=[9.7 8.1 6.6 5.1 4.4 3.7 2.8 2.4 2.0 1.6 1.4 1.1 0.85 0.69 0.6]; >> a = QuadFit(t, V) a = 10.7215 -0.7480 0.0141 The quadratic polynomial that best fits the data from Example 5-2 is: f x = 0.0141x 2 – 0.7480x + 10.7215 (b) The following program in a script file plots the data points and the curve of the quadratic polynomial that best fits the data. clear all; close all; clc; t=[2 4 6 8 10 12 14 16 18 20 22 24 26 28 30]; V=[9.7 8.1 6.6 5.1 4.4 3.7 2.8 2.4 2.0 1.6 1.4 1.1 0.85 0.69 0.6]; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 n=length(t); a = QuadFit(t, V); tt=linspace(t(1),t(n),50); VV=a(3).*tt.^2+a(2).*tt+a(1); plot(t,V,'o',tt,VV,':'); xlabel('t (s)'); ylabel('v_R (V)'); legend('Data','Polynomial fit'); When the program is executed, the following plot is displayed in the Figure Window: 10 Data Polynomial fit 9 8 7 R v (V) 6 5 4 3 2 1 0 0 5 10 15 t (s) 20 25 30 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.21 Write a MATLAB user-defined function that determines the coefficients of a cubic polynomial, f x = a 3 x 3 + a 2 x 2 + a 1 x + a 0 , that best fits a given set of data points. The function should also calculate the overall error E according to Eq. (5.6). Name the function [a,Er] = CubicPolyFit(x,y), where the input arguments x and y are vectors with the coordinates of the data points, and the output argument a is a four-element vector with the values of the coefficients a 3 , a 2 , a 1 and a 0 . The output argument Er is the value of the overall error. (a) Use CubicPolyFit to find the quadratic polynomial that best fits the data in Example 5-3. (b) Write a program in a script file that plots the data points and the curve of the cubic polynomial that best fits the data. Solution The listing of the user-defined function CubicPolyFit is: function [a,Er] = CubicPolyFit(x, y) % CubicPolyFit calculates the coefficients a3,a2,a1 and a0 of the cubic % polynomial that best fits n data points. % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % Output variables: % a A vector with the values of a3,a2,a1 and a0 % Er The overall error % The vector [a] is determined by solving the system [X][a]=[Y] nx = length(x); ny = length(y); m = 4; %number of coefficients if nx ~= ny disp('ERROR: The number of elements in x must be the same as in y.') a = 'Error'; else for i = 1:(2*m-2) xsum(i) = sum(x.^(i)); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 end % First row of matrix [X] and first element of column vector [Y] X(1,1) = nx; Y(1,1) = sum(y); % Remaining rows of [X] and [Y] for j = 2:m X(1,j) = xsum(j - 1); end for i = 2:m for j = 1:m X(i,j) = xsum(j + i - 2); end Y(i,1) = sum(x.^(i - 1).*y); end % Solve for [a] and Er a = (X\Y)'; Er=sum((y-a(4).*x.^3-a(3).*x.^2-a(2).*x-a(1)).^2); end (a) The user-defined function CubicPolyFit can be used in the Command Window to find the cubic polynomial that best fits the data from Example 5-3 and the error associated with that polynomial: >> x=[0 0.4 0.8 1.2 1.6 2 2.4 2.8 3.2 3.6 4 4.4 4.8 5.2 5.6 6]; >> y=[0 3 4.5 5.8 5.9 5.8 6.2 7.4 9.6 15.6 20.7 26.7 31.1 35.6 39.3 41.5]; >> [a,Er] = CubicPolyFit(x,y) a = 2.8930 -1.9882 1.8030 -0.0541 Er = 69.9256 The cubic polynomial which provides the best fit with the data is: f x = – 0.0541 x 3 + 1.8030x 2 – 1.9882x + 2.8930 (b) The following program in a script file plots the data points and the curve of the cubic polynomial that Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 best fits the data. clear all; close all; clc; x=[0 0.4 0.8 1.2 1.6 2 2.4 2.8 3.2 3.6 4 4.4 4.8 5.2 5.6 6]; y=[0 3 4.5 5.8 5.9 5.8 6.2 7.4 9.6 15.6 20.7 26.7 31.1 35.6 39.3 41.5]; n=length(x); [a,Er] = CubicPolyFit(x,y) xx=linspace(x(1),x(n),50); yy=a(4).*xx.^3+a(3).*xx.^2+a(2).*xx+a(1); plot(x,y,'o',xx,yy); xlabel('Strain \epsilon'); ylabel('Stress \sigma (MPa)'); legend('Data','Polynomial fit',2); When the program is executed, the following plot is displayed in the Figure Window: 45 Data Polynomial fit 40 35 Stress σ (MPa) 30 25 20 15 10 5 0 0 1 2 3 Strain ε 4 5 6 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.22 Write a MATLAB user-defined function for interpolation with natural cubic splines. Name the function Yint = CubicSplines(x,y,xint), where the input arguments x and y are vectors with the coordinates of the data points, and xint is the x coordinate of the interpolated point. The output argument Yint is the y value of the interpolated point. (a) Use the function with the data in Example 5-8 for calculating the interpolated value at x = 12.7 . (b) Use the function with the data in Problem 5.27 for calculating the enthalpy per unit mass at T = 14000 K and at T = 24000 K. Solution The listing of the user-defined function CubicSplines is: function Yint=CubicSplines(x,y,xint) % CubicSplines fits a set of cubic splines to n data points and then % returns the interpolated value Yint at the desired coordinate Xint. % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % xint The x-value where interpolation is desired % Output variables: % Yint The interpolated y-value at x=xint n=length(x); interval=1; if n ~= length(y) disp('ERROR: x and y do not have the same number of points'); stop end % calculate h_i for i = 1:n-1 h(i) = x(i+1)-x(i); end %Start Thomas Algorithm for i=2:n-2 b(i)=h(i); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 end b(1)=0; for i=1:n-3 a(i)=h(i+1); end a(n-2)=0; for i=1:n-2 d(i)=2*(h(i)+h(i+1)); r(i)=6*(((y(i+2)-y(i+1))/h(i+1))-((y(i+1)-y(i))/ h(i))); end A=zeros(n-2,n-2); for i=2:n-3 A(i,i)=d(i); A(i,i+1)=a(i); A(i,i-1)=b(i); end A(1,2)=a(1); A(1,1)=d(1); A(n-2,n-3)=b(n-2); A(n-2,n-2)=d(n-2); a(1)=a(1)/d(1); r(1)=r(1)/d(1); for i=2:n-3 denom=d(i)-b(i)*a(i-1); if(denom==0), error('zero in denominator'), end a(i)=a(i)/denom; r(i)=(r(i)-b(i)*r(i-1))/denom; end r(n-2)=(r(n-2)-b(n-2)*r(n-3))/(d(n-2)-b(n-2)*a(n-3)); ans(n-2)=r(n-2); for i=n-3:-1:1 ans(i) = r(i) - a(i)*ans(i+1); end acoeff(1)=0; acoeff(n)=0; for i=2:n-1 acoeff(i)=ans(i-1); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 end for j=1:n-1 if xint >= x(j) & xint <= x(j+1) interval=j; end end i=interval; YintA=(acoeff(i)*((x(i+1)-xint)^3)/6/h(i)); YintB=(acoeff(i+1)*((xint-x(i))^3)/6/h(i)); YintC=((y(i)/h(i))-(acoeff(i)*h(i)/6))*(x(i+1)-xint); YintD=((y(i+1)/h(i))-(acoeff(i+1)*h(i)/6))*(xint-x(i)); Yint=YintA+YintB+YintC+YintD; (a) The user-defined function CubicSplines is used in the Command Window to calculate the interpolated value at x = 12.7 for the data in Example 5-8: >> x=[8 11 15 18 22]; >> y=[5 9 10 8 7]; >> Yint=CubicSplines(x,y,12.7) Yint = 10.1189 >> (b) The user-defined function is used in the Command Window to calculate the enthalpy per unit mass at T = 14000 K and at T = 24000 K using the data from Problem 5.27: >> x=[5 7.5 10 12.5 15 17.5 20 22.5 25 27.5 30]*10^3; >> y=[3.3 7.5 41.8 51.8 61 101.1 132.9 145.5 171.4 225.8 260.9]; >> Yint=CubicSplines(x,y,14000) Yint = 54.1160 >> Yint=CubicSplines(x,y,24000) Yint = 157.0608 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 >> Thus, at T = 14000 K, the enthalpy per unit mass is h = 54.1160 MJ/kg. At T = 24000 K, the enthalpy per unit mass is h = 157.0608 MJ/kg. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.23 In a uniaxial tension test, a dog-bone-shaped specimen is pulled in a machine. During the test, the force applied to the specimen, F, and the length of a gage section, L, are measured. The true stress, V t , and the true strain, H t , are defined by: F L V t = ----- ----A0 L0 L H t = ln ----L0 and where A 0 and L 0 are the initial cross-sectional area and gage length, respectively. The true stress–strain curve in the region beyond yielding is often modeled by: m V t = KH t The following are values of F and L measured in an experiment. Use the approach from Section 5.3 for determining the value of the coefficients K and m that best fit the data. The initial cross-sectional area and gage length are A 0 = 1.25 u 10 –4 m2, and L 0 = 0.0125 m. F (kN) L (mm) 24.6 12.58 29.3 12.82 31.5 12.91 33.3 12.95 34.8 13.05 35.7 13.21 36.6 13.35 37.5 13.49 38.8 14.08 39.6 14.21 40.4 14.48 Solution To solve the problem the equation V t = KH mt is written in the form: ln V t = m ln H t + ln K Then, linear least-squares regression is used for finding the coefficients m and K that best fit the data. (See equations in the first row of Table 5-2.) The following program written in a script file determines the constants and plot the data points and the curve that best fit the data. % Solution of HW5_23 script file clear all A0= 1.25e-4; L0=0.0125; F=[24.6 29.3 31.5 33.3 34.8 35.7 36.6 37.5 38.8 39.6 40.4]*1000 L=[12.58 12.82 12.91 12.95 13.05 13.21 13.35 13.49 14.08 14.21 14.48]*0.001 n=length(F) St=F.*L/(A0*L0) EPt=log(L/L0) Y=log(St); X=log(EPt); p=polyfit(X,Y,1) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 m=p(1) K=exp(p(2)) x=linspace(EPt(1),EPt(n),30) y=K*x.^m plot(x,y) hold on plot(EPt,St,'*r') xlabel('True Strain'), ylabel('True Stress (Pa)') When the program is executed, the following values of m and K are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. m = 0.2085 K = 5.4948e+008 8 3.8 x 10 3.6 3.4 True Stress (Pa) 3.2 3 2.8 2.6 2.4 2.2 2 1.8 Best fit curve Data points 0 0.02 0.04 0.06 0.08 0.1 True Strain 0.12 0.14 0.16 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.24 The stress concentration factor k is the ratio between the maximum stress V max and the average stress V ave , k = V max e V ave . For a plate of width D with a central hole of diameter d loaded with an axial force F (see figure), the maximum stress is at the edge of the hole, and the average stress is given by V ave = F e > t D – d @ , where t is the thickness of the plate. The stress concentration factor measured in five tests with plates with different ratios of d e D is shown in the table. d/D k 0.05 2.91 0.25 2.40 0.45 2.17 0.65 2.11 0.85 2.03 (a) Use an exponential function k = be m d e D to model the relationship between k and d e D . Determine the values of b and m that best-fit the data. (b) Plot the data points and the curve-fitted model. (c) Use the model to predict the stress concentration factor for d e D = 0.15 . Solution To solve the problem the equation k = be m deD is written in the form: ln k = m d e D + ln b Then, linear least-squares regression is used for finding the coefficients m and K that best fit the data. (See equations in the second row of Table 5-2.) The following program written in a script file determines the constants and plot the data points and the curve that best fit the data. % Solution of HW5_24 script file clear all dD=[0.05 0.25 0.45 0.65 0.85]; k=[2.91 2.4 2.17 2.11 2.03]; n=length(k); Y=log(k); p=polyfit(dD,Y,1); m=p(1) b=exp(p(2)) x=linspace(dD(1),dD(n),30); y=b*exp(m*x); plot(x,y) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 hold on plot(dD,k,'*r') xlabel('d/D'), ylabel('k') legend('Best fit curve','Data points') hold off % Model prediction for d/D = 0.15 kFor015=b*exp(m*0.15) When the program is executed, the values of m and K and the prediction of k for d e D = 0.15 are displayed in the Command Window.The figure that follows is displayed in the Figure Window. m = -0.4245 b = 2.7889 kFor015 = 2.6168 3 Best fit curve Data points 2.8 k 2.6 2.4 2.2 2 1.8 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 d/D Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.25 A hot-wire anemometer is a device for measuring flow velocity, by measuring the cooling effect of the flow on the resistance of a hot wire. The following data are obtained in calibration tests. u (ft/s) V (Volt) u (ft/s) V (Volt) 4.72 7.18 12.49 7.3 20.03 7.37 28.33 7.42 37.47 7.47 41.43 7.5 48.38 7.53 55.06 7.55 66.77 7.58 59.16 7.56 54.45 7.55 47.21 7.53 42.75 7.51 32.71 7.47 25.43 7.44 8.18 7.28 Determine the coefficients of the exponential function u = Ae BV that best fit the data, (a) using the user-defined function ExpoFit developed in Problem 5-18. (b) using MATLAB built-in functions. In each part plot the data points and the fitting function. Solution (a) The user-defined function ExpoFit that was developed in Problem 5-18 fits an exponential function – mx of the form y = be to a given set of data points. The following program written in a script file uses ExpoFit to determine the constants and plot the data points and the curve that best fit the data. % Solution of HW5_25a script file clear all u=[4.72 12.49 20.03 28.33 37.47 41.43 48.38 55.06 ... 66.77 59.16 54.45 47.21 42.75 32.71 25.43 8.18]; V=[7.18 7.3 7.37 7.42 7.47 7.5 7.53 7.55 7.58 7.56 ... 7.55 7.53 7.51 7.47 7.44 7.28]; [A,B] = ExpoFit(V, u) x=linspace(min(V),max(V),30); y=A*exp(-B*x); plot(x,y) hold on plot(V,u,'*r') xlabel('V (Volt)'), ylabel('u (ft/s)') legend('Best fit curve','Data points',2) text(7.45,10,'Problem 5.25 Part (a)') hold off Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The listing of the user-defined function ExpoFit is: function [b,m] = ExpoFit(x, y) % ExpoFit calculates the coefficients b and m of the exponential % equation y = b*exp(-m*x) that best fits n data points. % Input variables: % x A vector with the coordinates x of the data points. % y A vector with the coordinates y of the data points. % Output variables: % b The coefficient b. % m The coefficient m. nx = length(x); ny = length(y); if nx ~= ny disp('ERROR: The number of elements in x must be the same as in y.') b = 'Error'; m = 'Error'; else Y=log(y); X=x; SX = sum(X); SY = sum(Y); SXY = sum(X.*Y); SXX = sum(X.^2); a1 = (nx*SXY - SX*SY)/(nx*SXX - SX^2); a0 = (SXX*SY - SXY*SX)/(nx*SXX - SX^2); b=exp(a0); m=-a1; end When the program is executed, the following values of B and A are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. A = 2.4825e-020 B = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 -6.5133 70 Best fit curve Data points 60 u (ft/s) 50 40 30 20 Problem 5.25 Part (a) 10 0 7.15 7.2 7.25 7.3 7.35 7.4 7.45 V (Volt) 7.5 7.55 7.6 (b) To solve the problem the equation u = Ae BV is written in the form: ln u = B d e D + ln A Then, linear least-squares regression is used for finding the coefficients B and A that best fit the data. (See equations in the second row of Table 5-2.) The following program written in a script file uses MATLAB built-in functions to determine the constants and plot the data points and the curve that best fit the data. % Solution of HW5_25b script file clear all u=[4.72 12.49 20.03 28.33 37.47 41.43 48.38 55.06 ... 66.77 59.16 54.45 47.21 42.75 32.71 25.43 8.18]; V=[7.18 7.3 7.37 7.42 7.47 7.5 7.53 7.55 7.58 7.56 ... 7.55 7.53 7.51 7.47 7.44 7.28]; n=length(u); Y=log(u); p=polyfit(V,Y,1); B=p(1) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 A=exp(p(2)) x=linspace(min(V),max(V),30); y=A*exp(B*x); plot(x,y) hold on plot(V,u,'*r') xlabel('V (Volt)'), ylabel('u (ft/s)') legend('Best fit curve','Data points',2) text(7.45,10,'Problem 5.25 Part (b)') hold off When the program is executed, the following values of B and A are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. B = 6.5133 A = 2.4825e-020 70 Best fit curve Data points 60 u (ft/s) 50 40 30 20 Problem 5.25 Part (b) 10 0 7.15 7.2 7.25 7.3 7.35 7.4 7.45 V (Volt) 7.5 7.55 7.6 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.26 The yield stress of many metals, V y , varies with the size of the grains. Often, the relationship between the grain size, d, and the yield stress is modeled with the Hall–Petch equation: V y = V 0 + kd §–1 --- · © 2¹ The following are results from measurements of average grain size and yield stress. d (mm) V y (MPa) 0.006 0.011 0.017 0.025 0.039 0.060 0.081 0.105 334 276 249 235 216 197 194 182 (a) Determine the constants V 0 and d such that the Hall–Petch equation will best-fit the data. Plot the data points (circle markers) and the Hall–Petch equation as a solid line. Use the Hall–Petch equation to estimate the yield stress of a specimen with a grain size of 0.05 mm. (b) Use the user-defined function QuadFit from Problem 5.20 to find the quadratic function that best fits the data. Plot the data points (circle markers) and the quadratic equation as a solid line. Use the quadratic equation to estimate the yield stress of a specimen with a grain size of 0.05 mm. Solution (a) The coefficients V 0 and k that best fit the data in the equation V y = V 0 + kd §–1 --- · © 2¹ are determined by §–1 --- · © 2¹ using linear least-squares regression with d as the independent variable and V y as the dependent variable. The following program written in a script file uses MATLAB built-in functions to determine the constants, plot the data points and the curve that best fit the data, and estimating the yield stress for grain size of 0.05 mm. % Solution of HW5_26a script file clear all d=[0.006 0.011 0.017 0.025 0.039 0.06 0.081 0.105]; Sy=[334 276 249 235 216 197 194 182]; n=length(d); X=d.^(-0.5); p=polyfit(X,Sy,1); k=p(1) S0=p(2) x=linspace(min(d),max(d),30); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 y=S0+k*x.^(-0.5); plot(x,y) hold on plot(d,Sy,'or') xlabel('Grain size (mm)'), ylabel('Yield Stress (MPa)') legend('Best fit curve','Data points') hold off % Model prediction for d = 0.05 SigmayFor05=S0+k*0.05^(-0.5) When the program is executed, the values V 0 and k and the prediction for grain size of 0.05 mm are displayed in the Command Window. The figure that follows is displayed in the Figure Window. k = 14.9410 S0 = 137.9135 SigmayFor05 = 204.7318 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 340 Best fit curve Data points 320 Yield Stress (MPa) 300 280 260 240 Problem 5.26 Part (a) 220 200 180 0 0.02 0.04 0.06 Grain size (mm) 0.08 0.1 0.12 (b) Quadratic function that fits the data has the form: 2 Vy = a2 d + a1 d + a0 The following program written in a script file uses the user-defined function QuadFit from Problem 5.20 to find the quadratic function that best fits the data. It also plots the data points and the curve that best fit the data, and estimates the yield stress for grain size of 0.05 mm. % Solution of HW5_26b script file clear all d=[0.006 0.011 0.017 0.025 0.039 0.06 0.081 0.105]; Sy=[334 276 249 235 216 197 194 182]; n=length(d); a = QuadFit(d, Sy) x=linspace(min(d),max(d),30); y=a(3)*x.^2+a(2)*x+a(1); plot(x,y) hold on plot(d,Sy,'or') xlabel('Grain size (mm)'), ylabel('Yield Stress (MPa)') legend('Best fit curve','Data points') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 text(0.08,220,'Problem 5.26 Part (b)') hold off % Model prediction for d = 0.05 SigmayFor05=a(3)*0.05^2+a(2)*0.05+a(1) When the program is executed, the values a 0 , a 1 , and a 2 , and the prediction for grain size of 0.05 mm are displayed in the Command Window. The figure that follows is displayed in the Figure Window. >> format short e a = 3.2301e+002 -3.5624e+003 2.2052e+004 SigmayFor05 = 2.0002e+002 2 The quadratic equation is: V y = 22052d – 356.24d + 323.01 340 Best fit curve Data points 320 300 Yield Stress (MPa) 280 260 240 Problem 5.26 Part (b) 220 200 180 160 0 0.02 0.04 0.06 Grain size (mm) 0.08 0.1 0.12 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.27 Values of enthalpy per unit mass, h, of an equilibrium Argon plasma (Ar, Ar+, A++, A+++ ions and electrons) versus temperature are: T u 10 (K) 5 7.5 10 12.5 15 17.5 h (MJ/kg) 3.3 7.5 41.8 51.8 61 101.1 132.9 145.5 171.4 225.8 260.9 3 20 22.5 25 27.5 30 Write a program in a script file that uses interpolation to calculate h at temperatures ranging from 5000 K to 30000 K in increments of 500 K. The program should generate a plot that shows the interpolated points, and the data points from the table (use an asterisk marker). (a) For interpolation use the user-defined function CubicSplines from Problem 5.22. (b) For interpolation use MATLAB’s built-in function interp1 with the spline option. Solution (a) The following program in a script file solves the problem using the user-defined function CubicSplines from Problem 5.22. % Solution of HW5_27a script file clear all T=[5:2.5:30]*1000; h=[3.3 7.5 41.8 51.8 61 101.1 132.9 145.5 171.4 225.8 260.9]; Tint=5000:500:30000; n=length(Tint); for i=1:n hint(i)=CubicSplines(T,h,Tint(i)); end plot(Tint,hint) hold on plot(T,h,'*r') xlabel('Temperature (K)') ylabel('Enthalpy (MJ/kg)') legend('Interpolation','Data points',2) text(20000,20,'Problem 5.27 Part (a)') hold off Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 When the script file is executed, the following plot is generated: 300 Interpolation Data points 250 Enthalpy (MJ/kg) 200 150 100 50 Problem 5.27 Part (a) 0 0.5 1 1.5 2 Temperature (K) 2.5 3 x 10 4 (b) The following program in a script file solves the problem using MATLAB’s built-in function interp1 with the spline option. % Solution of HW5_27b script file clear all T=[5:2.5:30]*1000; h=[3.3 7.5 41.8 51.8 61 101.1 132.9 145.5 171.4 225.8 260.9]; Tint=5000:500:30000; n=length(Tint); hint=interp1(T,h,Tint,'spline'); plot(Tint,hint) hold on plot(T,h,'*r') xlabel('Temperature (K)') ylabel('Enthalpy (MJ/kg)') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 legend('Interpolation','Data points',2) hold off When the script file is executed, the following plot is generated: 300 Interpolation Data points 250 Enthalpy (MJ/kg) 200 150 100 50 Problem 5.27 Part (b) 0 −50 0.5 1 1.5 2 Temperature (K) 2.5 3 x 10 4 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 5.28 The following are measurements of the rate coefficient, k, for the reaction CH 4 + O o CH 3 + OH at different temperatures, T. T (K) k u 10 20 (m3/s) 595 2.12 623 3.12 761 14.4 849 30.6 989 80.3 1076 131 1146 186 1202 240 1382 489 1445 604 1562 868 (a) Use the method of least-squares to best-fit a function of the form ln k = C + b ln T – D ---- to the data. T Determine the constants C, b, and D by curve fitting a linear combination of the functions f 1 T = 1 , 1 f 2 T = ln T , and f 3 T = – ------ to the given data (Section 5.8). T – E e RT (b) Usually, the rate coefficient is expressed in the form of an Arrhenius equation k = AT b e a , where A and b are constants, R = 8.314 J/mole/K is the universal gas constant, and E a is the activation energy for the reaction. Having determined the constants C, b, and D in part (a), deduce the values of A (m3/s) and E a (J/mole) in the Arrhenius expression. Solution (a) Using Eq. (5.97), the following system of three linear equations for the unknowns C, b, and D can be written: 11 11 ¦ C 11 + b ln T i + D i=1 i=1 11 C ¦ 11 ln T i + b i=1 11 C ¦ ¦ i=1 –1 ------ + b Ti 2 > ln T i @ + D i=1 11 ¦ i=1 ¦ 11 ¦ i=1 –1 ------ ln T i + D Ti –1 ------ = Ti 11 ¦ ln k –1 ------ ln T i = Ti 11 ¦ i=1 –1 · § ----© Ti ¹ i i=1 11 ¦ ln k 11 2 = i ln T i i=1 ¦ ln k i=1 i –1 -----Ti In the program listed below, the system of equations is solved. In the program the unknowns (solution) is a three element vector named Cnst such that C = Cnst 1 , b = Cnst 2 , and D = Cnst 3 . The program generates also a plot of ln k versus T. The plot shows the data points and line of the function D C + b ln T – ---- . T Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 (b) b ---- can be rewritten in the form k = AT e The equation ln k = C + b ln T – D – E a e RT T : b –D e T k = eCT e The constants A, b, and E a are related to the constants C, b, and D that were determined in Part (a) by: A = eC , E b = b , and D = -----a R The computer program calculates the constants and then makes a plot of k versus T. The plot shows the data points and the line predicted by the equation. % HW5_28 solution. Script file. clear all T = [595 623 761 849 989 1076 1146 1202 1382 1445 1562]; k = [2.12 3.12 14.4 30.6 80.3 131 186 240 489 604 868]*1e-20; n=length(k); a(1,1) = n; a(1,2) = sum(log(T)); a(1,3) = sum(-1./T); a(2,1) = a(1,2); a(2,2) = sum(log(T).^2); a(2,3) = sum(-log(T)./T); a(3,1) = a(1,3); a(3,2) = a(2,3); a(3,3) = sum(1./(T.^2)); b(1,1) = sum(log(k)); b(2,1) = sum(log(k).*log(T)); b(3,1) = sum(-log(k)./T); Cnst = a\b Tfit = 595:25:1575; LogkFit = Cnst(1)+ Cnst(2)*log(Tfit)-Cnst(3)./Tfit; plot(T,log(k),'or',Tfit,LogkFit) xlabel('T (K)'), ylabel('log(k)') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 legend('Data points','Equation',2) % Part b R=8.314; A=exp(Cnst(1)) bb = Cnst(2) Ea=Cnst(3)*R kfit=A*Tfit.^bb.*exp(-Ea./(R*Tfit)); figure plot(T,k,'or',Tfit,kfit) xlabel('T (K)'), ylabel('k (m^3/s)') legend('Data points','Equation',2) When the program is executed, the values of the constants is displayed in the Command Window, and the following two figures are displayed. Command Window: >> format short e Cnst = -5.2443e+001 2.1216e+000 3.8153e+003 A = 1.6765e-023 bb = 2.1216e+000 Ea = 3.1721e+004 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 −17 −39 1 Data points Equation x 10 0.9 Data points Equation −40 0.8 0.7 0.6 −42 k (m3/s) log(k) −41 −43 0.5 0.4 0.3 −44 0.2 −45 0.1 −46 400 600 800 Part (a) 1000 T (K) 1200 1400 1600 0 400 600 800 1000 T (K) 1200 1400 1600 Part (b) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.1 Given the following data, x f x 0.398 0.399 0.400 0.401 0.402 0.408591 0.409671 0.410752 0.411834 0.412915 find the first derivative f c x at the point x = 0.399 . (a) Use the three-point forward difference formula. (b) Use the two-point central difference formula. Solution (a) The three point forward difference formula is given by (see Table 6-1): – 3f x i + 4f x i + 1 – f x i + 2 f c x i = -------------------------------------------------------------------2h Setting x i = 0.399 implies that x i + 1 = 0.4 and x i + 2 = 0.401 . Correspondingly, f x i = 0.409671 , f x i + 1 = 0.410752 , and f x i + 2 = 0.411834 . In this problem, h = 0.001 . Substituting into the above for- mula yields: – 3 0.409671 + 4 0.410752 – 0.411834 = 1.0805 f c 0.399 = -------------------------------------------------------------------------------------------------------2 0.001 (b) The two-point central difference formula is given by (see Table 6-1): Again, setting x i = 0.399 f x i – 1 = 0.408591 , and f x i + 1 f xi + 1 – f xi – 1 f c x i = --------------------------------------2h implies that x i – 1 = 0.398 and x i + 1 = 0.400 . Correspondingly, = 0.410752 . Substituting into the above formula yields: – 0.408591- = 1.0805 -------------------------------------------------f c 0.399 = 0.410752 2 0.001 In this particular problem, the answers to parts (a) and (b) are identical. In general, different difference schemes will yield slightly different answers. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.2 The following data shows the population of Nepal in selected years between 1980 and 2005. Year 1980 1985 1990 1995 2000 2005 Population (millions) 15 17 19.3 22 24.5 27.1 Calculate the rate of growth of the population in millions per year for 2005. (a) Use two-point backward difference formula. (b) Use three-point backward difference formula. (c) Using the slope in 2005 from part (b), apply the two-point central difference formula to extrapolate and predict the population in the year 2010. Solution (a) From Table 6-1, the two-point backward difference formula is: Here, x i = 2005 , h = 5 , f x i into the above formula yields: f xi – f xi – 1 f c x i = --------------------------------h = 27.1 (million), and f x i – 1 = 24.5 (million). Substituting these values million – 24.5 f c 2005 = 27.1 --------------------------- = 0.52 ------------------year 5 Thus, the rate of growth of the population in 2005 is 0.52 million/year or 520,000 per year. (b) From Table 6-1, three-point backward difference formula is: f x i – 2 – 4f x i – 1 + 3f x i f c x i = --------------------------------------------------------------2h Substituting x i = 2005 , h = 5 , f x i = 27.1 (million), f x i – 1 = 24.5 (million), and f x i – 2 = 22 (mil- lion) into the above expression yields: 22 – 4 24.5 + 3 27.1 = 0.53 million ------------------f c 2005 = ------------------------------------------------------year 2 5 Thus, the rate of growth of the population in 2005 is 0.53 million/year or 530,000 per year. Note how this answer differs from that of part (a). (c) The central difference formula is: Substituting x i = 2005 , h = 5 , f c x i f xi + 1 – f xi – 1 f c x i = --------------------------------------2h million = 0.53 ------------------- from part (b), and f x i – 1 = 24.5 (million) into the year Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 above equation, and solving for f x i + 1 yields: f x i + 1 = f 2010 = 2hf c x i + f x i – 1 = 2 5 0.53 + 24.5 = 29.8 million Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.3 The following data is given for the stopping distance of a car versus the speed at which it begins braking. Stopping distance (ft) 20 35 80 110 150 Speed (mph) 20 30 40 50 60 (a) Calculate the rate of change of the stopping distance at a speed of 60 mph using (i) the two-point backward difference formula, and (ii) the three-point backward difference formula. (b) Calculate an estimate for the stopping distance at 70 mph by using the results from part (a) for the slope and the two-point central difference formula applied at the speed of 60 mph. Solution (a) (i) The two-point backward difference formula for the rate of change of stopping distance with respect to speed is given by (see Table 6-1): x vi – x vi – 1 xc v i = ----------------------------------h = 50 mph , x v i = 150 ft , and x v i – 1 = 110 ft , the change in stop- For v i = 60 mph , h = 10 mph , v i – 1 ping distance with respect to speed, at 60 mph is: 150 – 110 xc 60 mph = ------------------------ = 4 ft/mph 10 (ii) With the three-point backward difference formula, the change in stopping distance with respect to speed, at 60 mph is: 80 – 4 110 + 3 150 xc 60 mph = ---------------------------------------------------- = 4.5 ft/mph 2 10 which is close but not equal to the answer obtained in part (i). (b) Using the slopes at 60 mph from part (a), the two-point central difference formula can be applied to extrapolate the data to find the stopping distance at 70 mph: x v i + 1 = 2hxc v i + x v i – 1 For the slope of 4 ft/mph obtained in part (a) (i), the extrapolated value is x 70 mph = 2 10 4 + 110 = 190 ft . For the slope of 4.5 obtained in part (a) (ii), the extrapolated value is x 70 mph = 2 10 4.5 + 110 = 200 ft . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.4 Given three unequally spaced points x i y i , x i + 1 y i + 1 , and x i + 2 y i + 2 , use Taylor series expansion to develop a finite difference formula to evaluate the first derivative dy / dx at the point x = x i . Verify that when the spacing between these points is equal, the three-point forward difference formula is obtained. The answer should involve y i , y i + 1 , and y i + 2 . Solution First write down the Taylor series expansions for y i + 1 and y i + 2 assuming the value y i is known: 2 yi + 1 = y i + x i + 1 – x i dy -----dx xi + 1 – xi d2 y + ---------------------------------2 2 x = xi dx 2 y i + 2 = y i + x i + 2 – x i dy -----dx xi + 2 – xi d2 y + ---------------------------------2 2 x = xi dx 3 xi + 1 – xi d3 y + ---------------------------------3 6 dx x = xi x = [i 3 xi + 2 – xi d3 y + ---------------------------------3 6 dx x = xi x = Ki The next step is to combine the above two equations in such a way that we retain the first derivative (which we want to solve for) and lose the second derivative in order to obtain a smaller truncation error. This is done by multiplying the first equation by x i + 2 – x i ing the result: 2 2 xi + 2 – xi yi + 1 = xi + 2 – xi yi + xi + 2 – xi 2 2 and the second equation by x i + 1 – x i x i + 1 – x i dy -----dx 2 2 2 3 2 2 2 3 2 and subtract- xi + 2 – xi xi + 1 – xi d2 y - -------+ ------------------------------------------------------2 2 x = xi dx xi + 2 – xi xi + 1 – xi d3 y - -------+ ------------------------------------------------------3 6 dx 2 2 xi + 1 – xi yi + 2 = xi + 1 – xi yi + xi + 1 – xi 2 x i + 2 – x i dy -----dx xi + 1 – xi xi + 2 – xi d2 y - -------+ ------------------------------------------------------2 2 x = xi dx xi + 1 – xi xi + 2 – xi d y - -------+ ------------------------------------------------------3 6 dx x = xi x = [i x = xi 3 x = Ki Note that the third term on the right hand sides of these equations is identical. Subtracting, 2 2 2 2 xi + 1 – xi yi + 2 – xi + 2 – xi yi + 1 = xi + 1 – xi yi – xi + 2 – xi yi + xi + 1 – xi 2 – xi + 2 – xi 2 x i + 1 – x i dy -----dx 3 xi + 1 – xi xi + 2 – xi d3 y - -------+ ------------------------------------------------------3 6 x = xi dx 2 2 x i + 2 – x i dy -----dx x = xi 3 xi + 2 – xi xi + 1 – xi d3 y - -------– ------------------------------------------------------3 6 dx x = Ki x = [i Combining the last two terms which constitute the truncation error, and solving for the first derivative Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 yields: 2 dy -----dx x = xi 2 2 2 x i + 1 – x i y i + 2 – x i + 2 – x i y i + 1 + > x i + 2 – x i – x i + 1 – x i @y i = ---------------------------------------------------------------------------------------------------------------------------------------------------------------- + TE 2 2 xi + 1 – xi xi + 2 – xi – xi + 2 – xi xi + 1 – xi where TE is the truncation error. When the spacing between the points is equal, i.e. x i + 1 – x i = x i + 2 – x i + 1 = h and x i + 2 – x i = 2h , the above expression reduces to: 2 dy -----dx x = xi 2 2 2 h y i + 2 – 4h y i + 1 + > 4h – h @y 2 = -----------------------------------------------------------------------------i + O h 3 3 2h – 4h which simplifies to: dy -----dx x = xi – y i + 2 + 4y i + 1 – 3y i 2 = ----------------------------------------------+O h 2h which is identical to the three-point forward difference formula given in Table 6-3. . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.5 Using a 4-term Taylor series expansion, derive a four-point backward difference formula for evaluating the first derivative of a function given by a set of unequally spaced points. The formula should give the derivative at point x = x i , in terms of x i , x i – 1 , x i – 2 , x i – 3 , f x i , f x i – 1 , f x i – 2 , and f x i – 3 . Solution A second-order accurate four-point backward difference formulae is obtained by using Taylor series expansion and eliminating the second-derivative terms. The procedure is analogous for both. First express f x i – 1 , f x i – 2 , and f x i – 3 in Taylor series expansions about x = x i : 2 df f x i – 1 = f x i – x i – x i – 1 ----dx xi – xi – 1 d2 f - -------+ -------------------------2 2 x = xi dx 2 df f x i – 2 = f x i – x i – x i – 2 ----dx xi – xi – 2 d2 f - -------+ -------------------------2 2 x = xi dx df f x i – 3 = f x i – x i – x i – 3 ----dx xi – xi – 3 d2 f - -------+ -------------------------2 2 x = xi dx 2 4 3 xi – xi – 1 d4 f - -------+ -------------------------4 24 dx x = xi xi – xi – 1 d3 f - -------– -------------------------3 6 dx x = xi 3 (A) x = [i 4 xi – xi – 2 d4 f - -------+ -------------------------4 24 dx x = xi xi – xi – 2 d3 f - -------– -------------------------3 6 dx x = xi 3 (B) x = Ki 4 xi – xi – 3 d4 f - -------+ -------------------------4 24 dx x = xi xi – xi – 3 d3 f - -------– -------------------------3 6 dx x = xi (C) x = ]i The next step is to combine the above three equations in such a way that we retain the first derivative (which we want to solve for) and lose the second derivative and perhaps the third derivative in order to obtain a smaller truncation error. First, let us derive the lower order formula. By inspection, we can see that the second derivative is eliminated if the first equation (A) is multiplied by xi – xi – 1 2 xi – xi – 2 2 2 x i – x i – 3 , and the third equation (C) by 2 x i – x i – 1 2 x i – x i – 3 , the second equation (B) by 2 xi – xi – 2 2 and adding the first two equa- tions and subtracting the third from the result: xi – xi – 2 2 2 2 xi – xi – 3 f xi – 1 = xi – xi – 2 2 2 2 2 xi – xi – 3 f xi – xi – xi – 2 xi – xi – 2 xi – xi – 3 xi – xi – 1 d2 f - -------+ ---------------------------------------------------------------------------------2 2 dx 2 2 2 2 xi – xi – 3 f xi – 2 = xi – xi – 1 2 df x i – x i – 1 ----dx x = xi 3 2 x = xi 4 2 2 xi – xi – 3 xi – xi – 2 xi – xi – 3 xi – xi – 1 d3 f - -------– ---------------------------------------------------------------------------------3 6 dx x = xi xi – xi – 2 xi – xi – 3 xi – xi – 1 d4 f - -------+ ---------------------------------------------------------------------------------4 24 dx xi – xi – 1 2 2 (A’) x = [i xi – xi – 3 f xi – xi – xi – 1 2 xi – xi – 3 2 df x i – x i – 2 ----dx x = xi Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 2 2 2 2 xi – xi – 1 xi – xi – 3 xi – xi – 2 d2 f - -------+ ---------------------------------------------------------------------------------2 2 dx 2 2 2 xi – xi – 2 f xi – 3 = 2 xi – xi – 1 2 + xi – xi – 1 2 xi – xi – 2 2 2 (B’) x = Ki 2 xi – xi – 2 f xi – 2 xi – xi – 1 2 2 xi – xi – 2 df x i – x i – 3 ----dx 2 2 x = xi 3 2 xi – xi – 1 xi – xi – 2 xi – xi – 3 d3 f - -------– ---------------------------------------------------------------------------------3 3 dx x = xi 2 2d f x i – x i – 3 ------2 dx x = xi 4 xi – xi – 1 xi – xi – 3 xi – xi – 2 d4 f - -------+ ---------------------------------------------------------------------------------4 24 dx 2 xi – xi – 1 3 2 xi – xi – 1 xi – xi – 3 xi – xi – 2 d3 f - -------– ---------------------------------------------------------------------------------3 6 dx x = xi x = xi 4 2 xi – xi – 1 xi – xi – 2 xi – xi – 3 d4 f - -------+ ---------------------------------------------------------------------------------4 12 dx (C’) x = ]i Combining these equations (A’)+(B’)-(C’) and lumping the truncation error terms together as TE, we have: xi – xi – 2 2 2 xi – xi – 3 f xi – 1 + xi – xi – 1 = xi – xi – 2 2 2 2 xi – xi – 2 2 df x i – x i – 3 ----dx – xi – xi – 1 dx 2 xi – xi – 3 f xi – 2 – 2 xi – xi – 1 xi – xi – 3 f xi + xi – xi – 1 + 2 xi – xi – 1 df Solving for ----- 2 2 2 2 xi – xi – 3 f xi – 2 xi – xi – 1 x = xi xi – xi – 3 2 – xi – xi – 2 xi – xi – 2 df----dx 2 xi – xi – 3 2 2 xi – xi – 2 f xi – 3 2 2 xi – xi – 2 f xi 2 df x i – x i – 1 ----dx x = xi + TE x = xi yields: x = xi df -----dx 2 2 x = xi 2 2 2 2 xi – xi – 2 xi – xi – 3 f xi – 1 + xi – xi – 1 xi – xi – 3 f xi – 2 – 2 xi – xi – 1 xi – xi – 2 f xi – 3 = -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2 2 2 2 2 2 2 xi – xi – 1 xi – xi – 2 xi – xi – 3 – xi – xi – 2 xi – xi – 3 xi – xi – 1 – xi – xi – 1 xi – xi – 3 xi – xi – 2 2 2 2 2 2 2 > 2 x i – x i – 1 x i – x i – 2 – x i – x i – 1 x i – x i – 3 – x i – x i – 2 x i – x i – 3 @f x i + -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2 2 2 2 2 2 2 xi – xi – 1 xi – xi – 2 xi – xi – 3 – xi – xi – 2 xi – xi – 3 xi – xi – 1 – xi – xi – 1 xi – xi – 3 xi – xi – 2 + TE For equally spaced points, i.e. x i – x i – 1 = x i – 1 – x i – 2 = x i – 3 – x i – 2 = h , this yields the following second order accurate formula: df -----dx x = xi 37f x i + 36f x i – 1 – 9f x i – 2 + 8f x i – 3 = ---------------------------------------------------------------------------------------------------30h Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.6 Derive a finite difference approximation formula for f cc x using three points x i – 1, x i , and x i + 1, where the spacing is such that x i – x i – 1 = h and x i + 1 – x i = 2h . Solution Expand f x i + 1 and f x i – 1 in terms of a Taylor series about x = x i : df f x i + 1 = f x i + 2h ----dx 2 2 d f + 2h ------2 x = xi dx dff x i – 1 = f x i – h ----dx 2 2 h d f + ----- ------2 2 x = xi dx 3 4h d 3 f + -------- ------3 3 dx x = xi 3 x = [i 3 h d f – ----- ------6 3 dx x = xi x = Ki Since we want a difference formula for the second derivative in terms of the values of the function, we seek to eliminate the first derivative from the above expressions. This can be done by multiplying the second equation by 2 and adding it to the first equation: 2 2 d f f x i + 1 + 2f x i – 1 = 3f x i + 3h ------2 dx +O h 3 x = xi Solving for the second derivative yields: 2 d f -------2 dx x = xi f x i + 1 + 2f x i – 1 – 3f x i = ---------------------------------------------------------------+O h 2 3h This result can also be obtained by using a Lagrange interpolating polynomial over the given three points and differentiating it twice and evaluating at x = x i . . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.7 A particular finite difference formula for the first derivative of a function is: – f x i + 3 + 9f x i + 1 – 8f x i f c x i = -------------------------------------------------------------------6h where the points x i , x i + 1, x i + 2 , and x i + 3 are all equally spaced with step size h. What is the order of the truncation or discretization error? Solution The Taylor expansions are: dff x i + 1 = f x i + h ----dx df f x i + 3 = f x i + 3h ----dx 2 h d2 f + ----- ------2 2 x = xi dx 2 2 9h d f + -------- ------2 2 x = xi dx 3 h d3 f + ----- ------6 3 dx x = xi 3 x = [i 3 9h d f + -------- ------3 2 dx x = xi x = Ki Substituting into the given difference formula yields: – f x i + 3 + 9f x i + 1 – 8f x i df f c x i = -------------------------------------------------------------------- = ----6h dx +O h 2 x = xi 2 Thus, the truncation error is of order h . . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.8 The following data shows the number of female and male physicians in the U.S. for various years. Year 1970 1980 1990 2000 2002 2003 2004 # males 308,627 413,395 511,227 618,182 638,182 646,493 647,347 # females 25, 401 54,284 104,194 195,537 215,005 225,042 235,627 (a) Calculate the rate of change in the number of male and female physicians in 2002 by using the threepoint backward difference formula for the derivative, with unequally spaced points, Eq. (6.37). (b) Use the result from part (a) and the three-point central difference formula for the derivative with unequally spaced points, Eq. (6.36) to calculate (predict) the number of male and female physicians in 2003. Solution (a) Eq.(6.37) is: xi + 2 – xi + 1 xi + 2 – xi 2x i + 2 – x i – x i + 1 - y - y f c x i + 2 = ----------------------------------------------+ ----------------------------------------------------y + --------------------------------------------------------xi – xi + 1 xi – xi + 2 i xi + 1 – xi xi + 1 – xi + 2 i + 1 xi + 2– xi xi + 2– xi + 1 i + 2 For this problem, x i + 2 = 2002 , x i + 1 = 2000 , and x i = 1990 . Applying the above formula to the number of male physicians yields: 2 12 14 f ' male 2002 = ---------------------------- 511227 + ---------------------- 618182 + ------------------- 638182 – 10 – 12 10 – 2 12 2 = 8520.45 – 370909.2 + 372272.83 = 9884.1 male physicians per year Applying the above formula to the number of female physicians yields: 2 12 14 f ' female 2002 = ---------------------------- 104194 + ---------------------- 195537 + ------------------- 215005 – 10 – 12 10 – 2 12 2 = 1736.57 – 117322.2 + 125419.58 = 9833.95 female physicians per year (b) Equation (6.36) is: xi + 1 – xi + 2 2x i + 1 – x i – x i + 2 xi + 1 – xi - y - y f c x i + 1 = ----------------------------------------------+ ----------------------------------------------------y + --------------------------------------------------------xi – xi + 1 xi – xi + 2 i xi + 1 – xi xi + 1 – xi + 2 i + 1 xi + 2 – xi xi + 2 – xi + 1 i + 2 Now, x i + 1 = 2002 , x i = 2000 , and x i + 2 = 2003 . Applying this formula to the male physicians and using the result from part (a), –1 1 2 f c male 2002 = 9884.1 = ---------------------- 618182 + ------------------- 638182 + ---------------- y i + 2 –2 –3 2 –1 3 1 2 = – 103030.33 – 319091 + --- y i + 2 3 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 Solving for y i + 2 yields y i + 2 = 648008 . Comparing with the exact value of 646,493, this is in error by 0.23%. Applying the three-point ventral difference formula to the female physicians and using the result from part (a), –1 1 2 f c female 2002 = 9833.95 = ---------------------- 195537 + ------------------- 215005 + ---------------- y i + 2 –2 –3 2 –1 3 1 2 = – 32589.5 – 107502.5 + --- y i + 2 3 Solving for y i + 2 yields y i + 2 = 224889 . Comparing with the exact value of 225,042, this is in error by less than 0.07%. . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.9 Use the data from Problem 6.8 and the four-point backward difference formula that was derived in Problem 6.5 for evaluating the first derivative of a function specified at unequally spaced points to calculate the following quantities. (a) Evaluate the rate of change in the number of male and female physicians in 2004. (b) Use the data from 2003, 2004, together with the slopes in 2004 from part (a) to estimate the year in which the number of female and male physicians will be equal. Use the three-point central difference formula for the derivative (Eq. (6.36)) of a function specified at unequally spaced points. Solution (a) Using the formula for the first derivative derived in Problem 6.5: df -----dx 2 2 x = xi 2 2 2 2 xi – xi – 2 xi – xi – 3 f xi – 1 + xi – xi – 1 xi – xi – 3 f xi – 2 – 2 xi – xi – 1 xi – xi – 2 f xi – 3 = -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2 2 2 2 2 2 2 xi – xi – 1 xi – xi – 2 xi – xi – 3 – xi – xi – 2 xi – xi – 3 xi – xi – 1 – xi – xi – 1 xi – xi – 3 xi – xi – 2 2 2 2 2 2 2 > 2 x i – x i – 1 x i – x i – 2 – x i – x i – 1 x i – x i – 3 – x i – x i – 2 x i – x i – 3 @f x i + -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2 2 2 2 2 2 2 xi – xi – 1 xi – xi – 2 xi – xi – 3 – xi – xi – 2 xi – xi – 3 xi – xi – 1 – xi – xi – 1 xi – xi – 3 xi – xi – 2 In this problem, x i = 2004 , x i – 1 = 2003 , x i – 2 = 2002 , and x i – 3 = 2000 . For male physicians, the rate of change is: 32024 > 2 4 – 16 – 4 16 @ 647347 4 16 646493 + 16 638182 – 2 4 618182 = ---------------------------------------------------------------------------------------------------------------------- + ----------------------------------------------------------------------- = --------------– 64 2 4 4 – 4 16 – 16 2 2 4 4 – 4 16 – 16 2 or -500.375 male physicians per year. Note that the calculated slope in the year 2004 is negative even though the numbers are increasing. For female physicians, the rate of change is: > 2 4 – 16 – 4 16 @ 235627 - = –-------------------16 225042 + 16 215005 – 2 4 195537 - + ---------------------------------------------------------------------686672= 4--------------------------------------------------------------------------------------------------------------------2 4 4 – 4 16 – 16 2 2 4 4 – 4 16 – 16 2 – 64 or 10729.25 female physicians per year. (b) Eq.(6.36) is: xi + 1 – xi + 2 2x i + 1 – x i – x i + 2 xi + 1 – xi - y i + 1 + ----------------------------------------------------- y f c x i + 1 = ----------------------------------------------y i + --------------------------------------------------------xi – xi + 1 xi – xi + 2 xi + 1 – xi xi + 1 – xi + 2 xi + 2– xi xi + 2– xi + 1 i + 2 But now, x i = 2003 , x i + 1 = 2004 , and x i + 2 is the unknown we seek. The quantity y i + 2 must be the same for male and female physicians. Therefore, the last two terms will be common for both: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 2005 – x i + 2 male x i + 2 – 2004 male - yi - y + -------------------------------f c male 2004 + -------------------------------x i + 2 – 2003 x i + 2 – 2004 i + 1 x i + 2 – 2004 female 2005 – x i + 2 female -y - y = f c female 2004 + -------------------------------+ -------------------------------x i + 2 – 2003 i x i + 2 – 2004 i + 1 For the slope based on the formula from Problem 6.5, this equation reduces to: 2 – 11229.625 x i + 2 – 2003 x i + 2 – 2004 + x i + 2 – 2004 421451 + 2005 – x i + 2 x i + 2 – 2003 411720 = 0 This is just a quadratic equation for x i + 2 which can be solved by hand. There are two real roots, x i + 2 = 2017.52 and x i + 2 = 1982.64 . The latter root is clearly non-physical (backward in time). So the answer is that between the years of 2017 and 2018, the number of male and female physicians will be equal. . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.10 Use Lagrange interpolation polynomials to find the finite difference formula for the second derivative at the point x = x i using the unequally spaced points x i , x i + 1, and x i + 2 . What is the second derivative at x = x i + 1 and at x = x i + 2 ? Solution The Lagrange interpolation polynomial for these three points is given by Eq.(6.33) in Chapter 6: x – xi + 1 x – xi + 2 x – xi x – xi + 2 x – xi x – xi + 1 - y - y f x = ----------------------------------------------+ ----------------------------------------------------y + --------------------------------------------------------xi – xi + 1 xi – xi + 2 i xi + 1 – xi xi + 1 – xi + 2 i + 1 xi + 2 – xi xi + 2 – xi + 1 i + 2 Expanding the products: 2 2 2 x – x i + 1 + x i + 2 x + x i + 1x i + 2 x – x i + x i + 2 x + x ix i + 2 x – x i + x i + 1 x + x ix i + 1 - y i + --------------------------------------------------------------- y i + 1 + --------------------------------------------------------------- yi + 2 f x = ---------------------------------------------------------------------------xi – xi + 1 xi – xi + 2 xi + 1 – xi xi + 1 – xi + 2 xi + 2 – xi xi + 2 – xi + 1 The second derivative can be found by simply differentiating this function twice to yield: 2 2y i 2y i + 2 2y i + 1 d f + ---------------------------------------------------------------2- = ----------------------------------------------- + ----------------------------------------------------xi + 2 – xi xi + 2 – xi + 1 xi + 1 – xi xi + 1 – xi + 2 xi – xi + 1 xi – xi + 2 dx Note that the second derivative does not depend on x since f x was quadratic to begin with. Thus the second derivative is a constant and has the same value given by the above expression both at x = x i + 1 and at x = xi + 2 . . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 2x – 1 - find the value of the first derivative at x = 2 . 6.11 Given the function f x = --------------------------------------------1e4 x 4 sin x + x + 1 (a) Use analytical differentiation by hand. (b) Use four-point central difference formula with x i – 2 = 1.96 , x i – 1 = 1.98 , x i + 1 = 2.02 , and x i + 2 = 2.04 . (Write a MATLAB program in a script file to carry out the calculations.) Solution (a) The derivative is found by using the relations from calculus summarized in Chapter 2: d 2 df- = --------------------------------------------- + 2x – 1 ------ x 4 sin x + x + 1 ----1e4 dx dx 4 x sin x + x + 1 –1 e 4 Applying the chain rule to the second term yields: 3 4 2 2x – 1 > 4x sin x + x cos x + 1 @df- = ------------------------------------------------- – ---------------------------------------------------------------------------5e4 1 e 4 dx 4 x 4 sin x + x + 1 x 4 sin x + x + 1 Evaluating at x = 2 , df -----dx x=2 3 > 32 sin 2 + 16 cos 2 + 1 @ = 0.9772 – 0.4894 = 0.487730 2 = -------------------------------------– --------------------------------------------------------------1e4 5e4 16 sin 2 + 3 4 16 sin 2 + 3 (b) The four-point central difference formula is given in Table 6-1: f x i – 2 – 8f x i – 1 + 8f x i + 1 – f x i + 2 f c x i = --------------------------------------------------------------------------------------------12h The following script file calculates the derivative using this formula with h = 0.02 : clear all; close all; format compact; f=inline('(2*x-1)/((((x^4)*sin(x))+x+1)^(1/4))','x'); xi_minus_2=1.96; xi_minus_1=1.98; xi_plus_1=2.02; xi_plus_2=2.04; fi_minus_2=f(xi_minus_2); fi_minus_1=f(xi_minus_1); fi_plus_1=f(xi_plus_1); fi_plus_2=f(xi_plus_2); h=0.02; first_deriv=(fi_minus_2-(8*fi_minus_1)+(8*fi_plus_1)-fi_plus_2)/12/h When executed in the command window, this script file produces the following output: first_deriv = 0.4877 >> As can be seen, the answer is in good agreement with the exact answer obtained in part (a). Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.12 For the function given in Problem 6.11, find the value of the second derivative at x = 2 . (a) Use analytical differentiation by hand. (b) Use five-point central difference formula with x i – 2 = 1.96 , x i – 1 = 1.98 , x i = 2 , x i + 1 = 2.02 , and x i + 2 = 2.04 . (Write a MATLAB program in a script file to carry out the calculations.) Solution (a) Differentiating the expression from Problem 6.11 one more time yields: 2 3 4 3 4 2 d f > 4x sin x + x cos x + 1 @ 5 2x – 1 > 4x sin x + x cos x + 1 @ -------- = – -------------------------------------------------------- + ---------------------------------------------------------------------------------2 5e4 9e4 dx 16 x 4 sin x + x + 1 x 4 sin x + x + 1 2 3 4 2x – 1 > 12x sin x + 8x cos x – x sin x @– ----------------------------------------------------------------------------------------------4 x 4 sin x + x + 1 5e4 Evaluating at x = 2 : 2 2 d f -------2 dx x=2 > 32 sin 2 + 16 cos 2 + 1 @ 15 > 32 sin 2 + 16 cos 2 + 1 @ 3 > 48 sin 2 + 64 cos 2 – 16 sin 2 @ = – -------------------------------------------------------- + ---------------------------------------------------------------- – ------------------------------------------------------------------------5e4 9e4 5e4 16 sin 2 + 3 16 16 sin 2 + 3 4 16 sin 2 + 3 which reduces to: 2 d f -------2dx 515.0574 1.8481 = – 23.4392 ------------------- + ---------------------- – ------------------- = 0.1131 35.9177 630.3103 35.9177 x=2 (b) The five-point central difference formula for the second derivative is given in Table 6-1: – f x i – 2 + 16f x i – 1 – 30f x i + 16f x i + 1 – f x i + 2 f cc x i = ------------------------------------------------------------------------------------------------------------------------------2 12h The following script file calculates the derivative using this formula with h = 0.02 : clear all; close all; format compact; f=inline('(2*x-1)/((((x^4)*sin(x))+x+1)^(1/4))','x'); xi_minus_2=1.96; xi_minus_1=1.98; xi=2; xi_plus_1=2.02; xi_plus_2=2.04; fi_minus_2=f(xi_minus_2); fi_minus_1=f(xi_minus_1); fi=f(xi); fi_plus_1=f(xi_plus_1); fi_plus_2=f(xi_plus_2); h=0.02; second_deriv=(-fi_minus_2+(16*fi_minus_1)-(30*fi)+(16*fi_plus_1)-fi_plus_2)/ 12/h/h When executed in the command window, this script file produces the following output: second_deriv = 0.1131 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 >> Note that the numerical answer and exact answer are identical to 4 decimal places, displaying the accuracy of the five-point central difference formula. . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.13 The following data for the velocity component in the x-direction, u, are obtained as a function of the two coordinates x and y. y = 0 y = 1 y = 2 y = 3 y = 4 x = 0 x = 1 x = 2 x = 3 x = 4 0 3 14 7 5 2 7 8 9 7 8 10 14 12 10 13 15 22 16 9 15 18 22 17 14 2 w u----------Use the four-point central difference formula for wywx to evaluate this derivative at the point 2 3 . Solution The four-point central difference formula for the mixed partial derivative is given by Eq.(6.65): 2 w u ----------wx wy > u x i +1 y i +1 – u x i – 1 y i +1 @ – > u x i + 1 y i – 1 – u x i – 1 y i – 1 @ = -----------------------------------------------------------------------------------------------------------------------------------------2h x 2h y x = xi y = yi In this problem, h x = h y = 1 . For x i y i = 2 3 , u x i +1 y i +1 = u 3 4 = 9 , u x i – 1 y i +1 = u 1 4 = 7 , u x i +1 y i – 1 = u 3 2 = 22 , and u x i – 1 y i – 1 = u 1 2 = 8 . Substituting these values into the difference formula yields: 2 w u ----------wx wy 9 – 7 @ – > 22 – 8 @ = – 12 = – 3 = >----------------------------------------------4 2 1 2 1 x = 2 y = 3 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.14 Write a MATLAB user-defined function that determines the first derivative of a function that is given by a set of points with constant spacing. For function name use yd = FirstDeriv(x,y). The input arguments x and y are vectors with the coordinates of the points, and the output argument yd is a vector with the values of the derivative at each point. At the first and last points, the function should calculate the derivative with the three-point forward and backward difference formulas, respectively. At all the other points FirstDeriv should use the two-point central difference formula. Use FirstDeriv to calculate the derivative of the function that is given in Problem 6.1. Solution The following user-defined MATLAB function solves this problem: function yd = FirstDeriv(x,y) n=length(x); h=x(2)-x(1); for i=2:n-1 yd(i)=(y(i+1)-y(i-1))/2/h; end yd(1)=(-3*y(1)+4*y(2)-y(3))/2/h; yd(n)=(y(n-2)-4*y(n-1)+3*y(n))/2/h; The user-defined function FirstDeriv is next used in the Command Window for calculating the derivative of the function that is given in Problem 6.1. >> format long >> x=[0.398 0.399 0.4 0.401 0.402]; >> y=[0.408591 0.409671 0.410752 0.411834 0.412915]; >> yd = FirstDeriv(x,y) yd = 1.07950000000001 1.08050000000001 1.08149999999998 1.08149999999998 1.08050000000004 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.15 Write a MATLAB user-defined function that calculates the second derivative of a function that is given by a set of data points with constant spacing. For function name and arguments use ydd=SecDeriv(x,y), where the input arguments x and y are vectors with the coordinates of the points, and ydd is a vector with the values of the second derivative at each point. For calculating the derivative, the function SecDeriv should use the finite difference formulas that have a truncation error of 2 O h . Use SecDeriv for calculating the derivative of the function that is given the following set of points. -1 x f x -0.5 -3.632 -0.3935 0 1 0.5 1 0.6487 -1.282 1.5 2 2.5 3 3.5 4 4.5 -4.518 -8.611 -12.82 -15.91 -15.88 -9.402 9.017 Solution From Table 6-1, the second derivative at the interior points can be calculated using the three-point central difference formula: f x i – 1 – 2f x i + f x i + 1 f cc x i = -----------------------------------------------------------2 h At the first point, to obtain second order accuracy, the second derivative must be evaluated using the fourpoint forward difference formula: 2f x i – 5f x i + 1 + 4f x i + 2 – f x i + 3 f cc x i = ------------------------------------------------------------------------------------------2 h At the last point, to obtain second order accuracy, the second derivative must be evaluated using the fourpoint forward difference formula: – f x i – 3 + 4f x i – 2 – 5f x i – 1 + 2f x i f cc x i = ----------------------------------------------------------------------------------------------2 h The following user-defined MATLAB function accomplishes this task: function ydd=SecDeriv(x,y) n=length(x); h=x(2)-x(1); for i=2:n-1 ydd(i)=(y(i-1)-2*y(i)+y(i+1))/h/h; end ydd(1)=(2*y(1)-5*y(2)+4*y(3)-y(4))/h/h; ydd(n)=(-y(n-3)+4*y(n-2)-5*y(n-1)+2*y(n))/h/h; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The user-defined function SecDeriv is next used in the Command Window for calculating the derivative of the function that is given. >> format long >> x=-1:0.5:4.5; >> y=[-3.632 -0.3935 1 0.6487 -1.282 -4.518 -8.611 -12.82 -15.91 -15.88 -9.402 9.017]; >> ydd=SecDeriv(x,y) ydd = Columns 1 through 3 -7.78080000000000 -7.38000000000000 -6.97920000000000 Columns 4 through 6 -6.31760000000000 -5.22120000000000 -3.42800000000000 Columns 7 through 9 -0.46400000000000 4.47600000000000 12.48000000000000 Columns 10 through 12 25.79200000000001 47.76399999999999 69.73599999999999 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.16 Write a MATLAB user-defined function that determines the first and second derivative of a function that is given by a set of points with constant spacing. For function name use [yd,ydd] = FrstScndDeriv(x,y). The input arguments x and y are vectors with the coordinates of the points, and the output arguments yd and ydd are vectors with the values of the first and second derivatives, respectively, at each point. For calculating both derivatives, the function should use the finite difference formulas that have a truncation error of O h 2 . (a) Use the function FrstScndDeriv to calculate the derivative of the function that is given by the data in Problem 6.15. (b) Modify the function (rename it FrstScndDerivPt) such that it also creates three plots (on the same page in a column). The top plot should be of the function, the second plot of the first derivative, and the third of the second derivative. Apply the function FrstScndDerivPt to the data in Problem 6.15. Solution (a) The user-defined function FrstScndDeriv is listed below. It uses the equations used in Problems 2.14 and 2.15 for calculating the derivatives. function [yd,ydd] = FrstScndDeriv(x,y) n=length(x); h=x(2)-x(1); for i=2:n-1 yd(i)=(y(i+1)-y(i-1))/2/h; ydd(i)=(y(i-1)-2*y(i)+y(i+1))/h/h; end yd(1)=(-3*y(1)+4*y(2)-y(3))/2/h; yd(n)=(y(n-2)-4*y(n-1)+3*y(n))/2/h; ydd(1)=(2*y(1)-5*y(2)+4*y(3)-y(4))/h/h; ydd(n)=(-y(n-3)+4*y(n-2)-5*y(n-1)+2*y(n))/h/h; end The user-defined function FrstScndDeriv is next used in the Command Window for calculating the derivative of the function that is given in Problem 6.15. >> format long >> x=-1:0.5:4.5; >> y=[-3.632 -0.3935 1 0.6487 -1.282 -4.518 -8.611 -12.82 -15.91 -15.88 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 -9.402 9.017]; >> [yd,ydd] = FrstScndDeriv(x,y) yd = Columns 1 through 4 8.32200000000000 4.63200000000000 Columns 5 through 8 -5.16670000000000 -7.32900000000000 Columns 9 through 12 -3.06000000000000 6.50800000000000 ydd = Columns 1 through 4 -7.78080000000000 -7.38000000000000 Columns 5 through 8 -5.22120000000000 -3.42800000000000 Columns 9 through 12 12.48000000000000 25.79200000000001 1.04220000000000 -2.28200000000000 -8.30200000000000 -7.29900000000000 24.89700000000000 48.77900000000000 -6.97920000000000 -6.31760000000000 -0.46400000000000 4.47600000000000 47.76399999999999 69.73599999999999 (b) The function from part (a) is modified such that it also creates three plots (on the same page in a column). The top plot should be of the function, the second plot of the first derivative, and the third of the second derivative. The listing of the function is: function [yd,ydd] = FrstScndDerivPt(x,y) n=length(x); h=x(2)-x(1); for i=2:n-1 yd(i)=(y(i+1)-y(i-1))/2/h; ydd(i)=(y(i-1)-2*y(i)+y(i+1))/h/h; end yd(1)=(-3*y(1)+4*y(2)-y(3))/2/h; yd(n)=(y(n-2)-4*y(n-1)+3*y(n))/2/h; ydd(1)=(2*y(1)-5*y(2)+4*y(3)-y(4))/h/h; ydd(n)=(-y(n-3)+4*y(n-2)-5*y(n-1)+2*y(n))/h/h; subplot(3,1,1) plot(x,y); xlabel('x'); ylabel('f(x)'); subplot(3,1,2) plot(x,yd); xlabel('x'); ylabel('df/dx'); subplot(3,1,3) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 plot(x,ydd); xlabel('x'); ylabel('d2f/dx2'); When the function is used for calculating the derivative of the function given in Problem 6.15, the display is the same as in Part (a). In addition, the figure on the right is displayed in the Figure Window: f(x) 1 −1 − − x df/dx 5 −5 − x d2f/dx2 1 5 −5 − x . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.17 Write a MATLAB user-defined function for differentiation of a function that is given in an analytical form. For function name and arguments use dfx = DiffAnaly(‘FunName’,xi). ‘FunName’ is a string with the name of a function file that calculates the value of the function to be differentiated at a given value of x, and xi is the value of x where the derivative is calculated. The function should calculate the derivative by using the two-point central difference formula. In the formula, the values of x i + 1 and x i – 1 should be taken to be 5% higher and 5% lower than the value of x i , respectively. x (a) Use the function to calculate the first derivative of f x = 2----- at x = 2 . x (b) Use the function to calculate the first derivative of the function that is given in Problem 6.11 at x = 2 . Solution The listing of the user-defined function DiffAnaly is: function dfx = DiffAnaly(FunName,xi) h=0.05*xi; dfx=(feval(FunName,(xi+h))-feval(FunName,(xi-h)))/(2*h); x (a) To calculate the value of the function, f x = 2----- , the following user-defined function is created: x function f = Fun6_17a(x) f=2^x/x; The user-defined function DiffAnaly is then used in the Command Window for calculating the derivax tive of f x = 2----- at x = 2 : x >> format long >> dfx = DiffAnaly('Fun6_17a',2) dfx = 0.38596639895570 This value can be checked by evaluating the derivative analytically: x x 2 df ln 2 2 ------ = – ----- + -----------------2 dx x x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 df----dx x=2 4 ln 2 = – 1 + 2 ln 2 = 0.3862943611 --- + ---------------= –4 4 2 Thus, the numerical answer agrees with the exact answer to within 0.08%. 2x – 1 (b) The function given in Problem 6.11 is f x = --------------------------------------------- . The function is written as a user1e4 x 4 sin x + x + 1 defined function: function f = Fun6_17b(x) f=(2*x-1)/((x^4*sin(x)+x+1)^(1/4)); The user-defined function DiffAnaly is then used in the Command Window for calculating the deriva2x – 1 tive of f x = --------------------------------------------- at x = 2 : 1e4 x 4 sin x + x + 1 >> format long >> dfx = DiffAnaly('Fun6_17b',2) dfx = 0.49000101457311 In Problem 6.11 the value that was obtain by using analytical differentiation was 0.4878, which is in agreement to within 0.45%. . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.18 Modify the MATLAB user-defined function in Problem 6.17 to include Richardson’s extrapolation. The function should calculate a first estimate for the derivative as described in Problem 6.17, and a second estimate by taking the values of x i + 1 and x i – 1 to be 2.5% higher and 2.5% lower than the value of x i , respectively. The two estimates should then be used with Richardson’s extrapolation for calculating the derivative. For function name and arguments use dfx=DiffRichardson(‘FunName’,xi). x (a) Use the function to calculate the derivative of f x = 2----- at x = 2 . x (b) Use the function to calculate the first derivative of the function that is given in Problem 6.11 at x = 2 . Solution Richardson’s extrapolation for numerical differentiation is given by Eq.(6.45): 1 h 4 D = --- § 4D § --- · – D h · + O h ¹ 3© © 2 ¹ The listing of the user-defined function DiffRichardson is: function dfx = DiffRichardson(FunName,xi) h1=0.05*xi; dfxh1=(feval(FunName,(xi+h1))-feval(FunName,(xi-h1)))/(2*h1); h2=0.025*xi; dfxh2=(feval(FunName,(xi+h2))-feval(FunName,(xi-h2)))/(2*h2); dfx=(4*dfxh2-dfxh1)/3; (a) The user-defined function DiffRichardson is then used in the Command Window for calculating x the derivative of f x = 2----- at x = 2 : x >> format long >> dfx = DiffRichardson('Fun6_18a',2) dfx = 0.38629474015719 This value agrees with the analytical answer to 6 decimal places. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 df----dx x=2 4 ln 2 = – 1 + 2 ln 2 = 0.3862943611 --- + ---------------= –4 4 2 2x – 1 (b) The function given in Problem 6.11 is f x = --------------------------------------------- . The function is written as a user1e4 x 4 sin x + x + 1 defined function: function f = Fun6_18b(x) f=(2*x-1)/((x^4*sin(x)+x+1)^(1/4)); The user-defined function DiffRichardson is then used in the Command Window for calculating the 2x – 1 derivative of f x = --------------------------------------------- at x = 2 : 1e4 x 4 sin x + x + 1 >> format long >> dfx = DiffRichardson('Fun6_18b',2) dfx = 0.48772845071753 This value agrees with the analytical value (see Problem 6.11) to 4 decimal places. . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.19 Write a MATLAB user-defined function that calculates the second derivative of a function that is given in an analytical form. For function name and arguments use ddfx = DDiffAnaly(‘FunName’,xi). ‘FunName’ is a string with the name of a function file that calculates the value of the function to be differentiated at a given value of x, and xi is the value of x where the second derivative is calculated. The function should calculate the second derivative with the three-point central difference formula. In the formula, the values of x i + 1 and x i – 1 should be taken to be 5% higher and 5% lower than the value of x i , respectively. x (a) Use the function to calculate the second derivative of f x = 2----- at x = 2 . x (b) Use the function to calculate the second derivative of the function that is given in Problem 6.11 at x = 2. Solution The listing of the user-defined function DDiffAnaly is: function ddfx = DDiffAnaly(FunName,xi) h=0.05*xi; ddfx=(feval(FunName,(xi-h))-2*feval(FunName,xi)+feval(FunName,(xi+h)))/(h^2); x (a) To calculate the value of the function, f x = 2----- , the following user-defined function is created: x function f = Fun6_19a(x) f=2^x/x; The user-defined function DDiffAnaly is then used in the Command Window for calculating the second x derivative of f x = 2----- at x = 2 : x >> format long >> ddfx = DDiffAnaly('Fun6_19a',2) ddfx = 0.57532441566441 This value can be checked by evaluating the second derivative analytically: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 2 x+1 x+1 2 x ln 2 2 ln 2 2 2 d f - + --------------------- – ------------------------------2- = ---------2 3 x x x dx Evaluating at x = 2 : 2 2 d f -------2dx ln 2 u 4 ln 2 u 8 8 = --- – ---------------------- + ------------------------- = 1 – 2 ln 2 + 2 ln 2 2 4 8 2 = 0.5746 x=2 The numerical answer can be seen to differ from this exact answer by 0.12%. 2x – 1 (b) The function given in Problem 6.11 is f x = --------------------------------------------- . The function is written as a user1e4 x 4 sin x + x + 1 defined function: function f = Fun6_19b(x) f=(2*x-1)/((x^4*sin(x)+x+1)^(1/4)); The user-defined function DDiffAnaly is then used in the Command Window for calculating the second 2x – 1 derivative of f x = --------------------------------------------- at x = 2 : 1e4 x 4 sin x + x + 1 >> format long >> ddfx = DDiffAnaly('Fun6_19b',2) ddfx = 0.11347462320155 This value agrees with the analytical value of 0.1131 (see Problem 6.12) to 3 decimal places. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.20 Write a MATLAB user-defined function that determines the first derivative of a function that is given by a set of points that are not spaced equally. For function name use yd = FirstDerivLag(x,y). The input arguments x and y are vectors with the coordinates of the points, and the output argument yd is a vector with the values of the derivative at each point. At the first and last points, the function should calculate the derivative with Eqs. (6.35) and (6.37), respectively. At all the other points the function should use Eq. (6.36). Use the function to calculate the derivative of the function that is given by the following set of points. -1 x f x -0.6 -0.3 -3.632 -0.8912 0.3808 0 1.0 0.5 0.8 1.6 0.6487 -0.3345 -5.287 2.5 2.8 3.2 3.5 4 -12.82 -14.92 -16.43 -15.88 -9.402 Solution The listing of the user-defined function FirstDerivLag is: function yd = FirstDerivLag(x,y) n=length(x); yd(1)=(2*x(1)-x(2)-x(3))*y(1)/(x(1)-x(2))/(x(1)-x(3))... +(x(1)-x(3))*y(2)/(x(2)-x(1))/(x(2)-x(3))... +(x(1)-x(2))*y(3)/(x(3)-x(1))/(x(3)-x(2)); for j=2:n-1 yd(j)=(x(j)-x(j+1))*y(j-1)/(x(j-1)-x(j))/(x(j-1)-x(j+1))... +(2*x(j)-x(j-1)-x(j+1))*y(j)/(x(j)-x(j-1))/(x(j)-x(j+1))... +(x(j)-x(j-1))*y(j+1)/(x(j+1)-x(j-1))/(x(j+1)-x(j)); end yd(n)=(x(n)-x(n-1))*y(n-2)/(x(n-2)-x(n-1))/(x(n-2)-x(n))... +(x(n)-x(n-2))*y(n-1)/(x(n-1)-x(n-2))/(x(n-1)-x(n))... +(2*x(n)-x(n-2)-x(n-1))*y(n)/(x(n)-x(n-2))/(x(n)-x(n-1)); The user-defined function FirstDerivLag is then used in the Command Window for calculating the derivative of function given in the problem statement: >> x=[-1 -0.6 -0.3 0 0.5 0.8 1.6 2.5 2.8 3.2 3.5 4]; >> y=[-3.632 -0.8912 0.3808 1.0 0.6487 -0.3345 -5.287 -12.82 -14.92 16.43 -15.88 -9.402]; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 >> yd = FirstDerivLag(x,y) yd = Columns 1 through 7 8.3446 5.3594 3.1520 Columns 8 through 12 -7.3425 -5.6179 -0.5702 1.0265 -2.3118 6.0043 19.9077 -4.0719 -7.2162 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.21 A 1-meter-long uniform beam is simply supported at both ends and is subjected to a load. The deflection of the beam is given by the differential equation: 2 x d y- = – M -----------------2 EI dx where y is the deflection, x is the coordinate measured along the length of the beam, M x is the bending moment, and EI = 1.2 u 10 7 N-m2 is the flexural rigidity of the beam. The following data is obtained from measuring the deflection of the beam versus position: x (m) 0 0.2 0.4 0.6 0.8 1.0 y (cm) 0 7.78 10.68 8.38 3.97 0 Find the bending moment M x at each location x from this given data. Use the central difference approximation for the internal points ( O h 2 ). At the endpoints use four-point forward and backward difference approximation ( O h 2 ). Solution The bending moment M x can be solved for from the given ODE: 2 d y M x = – EI -------2dx The second derivative can be calculated with the user-defined function SecDeriv that was written in Problem 6.15. The bending moment M x at each location x is calculated by using SecDeriv in the Command Window: >> EI=1.2E7; >> x=0:0.2:1.0; >> y=[0 0.0778 0.1068 0.0838 0.0397 0]; >> M=-EI*SecDeriv(x,y) M = 1.0e+007 * 1.3680 1.4640 1.5600 0.6330 -0.1320 -0.8970 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.22 The distribution of the x-component of the velocity u of a fluid near a flat surface is measured as a function of the distance y from the surface: y (m) 0 u (m/s) 0 0.002 0.004 0.006 0.008 0.00618 0.011756 0.01618 0.019021 The shear stress W yx in the fluid is described by Newton’s equation: wu W yx = P -----wy where P is the coefficient of dynamic viscosity. The viscosity can be thought of as a measure of the internal friction within the fluid. Fluids that obey Newton’s constitutive equation are called Newtonian fluids. Calculate the shear stress at y = 0 using (i) the two-point forward, and (ii) the three-points forward approximations for the derivative. Take P = 0.002 N-s/m2. Solution (i) The two-point forward difference formula applied at y = 0 yields: wu -----wy y=0 –1 0.00618 – 0 u 0.002 – u 0 = -------------------------------------- = ---------------------------- = 3.09 s 0.002 0.002 The shear stress at y = 0 is then given by W yx (ii) 2 y=0 = 0.002 3.09 = 0.00618 N e m . The three-point forward difference formula applied at y = 0 yields: wu -----wy y=0 –1 – 3 0 + 4 0.00618 – 0.011756 – 3u 0 + 4u 0.002 – u 0.004 = ------------------------------------------------------------------------------ = ------------------------------------------------------------------------------ = 3.241 s 0.004 2 0.002 The shear stress at y = 0 is then given by W yx 2 y=0 = 0.002 3.241 = 0.006482 N e m . It is to be expected that this answer is more accurate than the value obtained in part (i) since it is obtained from a higher order accurate formula. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.23 A fin is an extended surface used to transfer heat from a base material (at x = 0 ) to an ambient. Heat flows from the base material through the base of the fin, through its outer surface, and through the tip. Measurement of the temperature distribution along a pin fin gives the following data: x (cm) T (K) 0 1 2 3 4 5 6 7 8 9 10 473 446.3 422.6 401.2 382 364.3 348.0 332.7 318.1 304.0 290.1 The fin has a length L = 10 cm, constant cross-sectional area of 1.6 u 10 –5 m2, and thermal conductivity k = 240 W/m/K. The heat flux dT dx (W/m2) is given by q x = – k ------ (a) Determine the heat flux at x = 0 . Use the three-point forward difference formula for calculating the derivative. (b) Determine the heat flux at x = L . Use the three-point backward difference formula for calculating the derivative. (c) Determine the amount of heat (in W) lost between x = 0 and x = L . (The heat flow per unit time in Watts is the heat flux multiplied by the cross-sectional area of the fin.) Solution (a) The three point forward difference formula for the derivative is: – 3f x i + 4f x i + 1 – f x i + 2 f c x i = -------------------------------------------------------------------2h In the present problem h = 0.01 m. Applying the formula at x = 0 yields: dT 3T 0 + 4T 1 – T 2 = – 3 473 + 4 446.3 – 422.6 = – 2820 K/m = –---------------------------------------------------------------------------------------------------------------------------------dx x = 0 2 0.01 0.02 Therefore, the heat flux at x = 0 is: qx x=0 dT = – k -----dx = – 240 – 2820 = 676.8 kW/m 2 . x=0 (b) The three point backward difference formula for the derivative is: f x i – 2 – 4f x i – 1 + 3f x i f c x i = --------------------------------------------------------------2h Applying the formula at x = 0.1 m yields: dT -----dx x = 10 318.1 – 4 304 + 3 290.1 8 – 4T 9 + 3T 10 = T -------------------------------------------------------- = ---------------------------------------------------------------- = – 1380 K/cm 0.02 2 0.01 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 Therefore, the heat flux at x = 0.1 m is: qx x = 10 dT = – k -----dx = – 240 – 13.8 = 331.2 kW/m 2 . x = 10 (c) The amount of heat flow at x = 0 is from part (a), 676.8 kW/m 2 u 1.6 u 10 –5 m 2 = 10.8288 W . Similarly, the amount of heat flow at x = 0.1 is from part (b), 331.2 kW/m 2 u 1.6 u 10 –5 m 2 = 5.2992 W . The amount of heat lost between x = 0 and x = 10 cm is 10.8288 W – 5.2992 W = 5.5296 W . . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.24 The charge on the capacitor in the RLC circuit shown at various times after the switch is closed at time t = 0 is given in the following ------- . table.The current, I, as a function of time is given by I t = dQ dt Determine the current as a function of time by numerically differentiating the data. (a) Use the user-defined function FirstDerivLag that was written in Problem 6.20. (b) Use the MATLAB built-in function diff. In both parts plot I vs. t. Q u 10 (C) 0 6.67 16.93 23.38 27.44 29.02 29.5 29.29 27.17 24.58 21.43 18.56 16.61 16.01 t (ms) 0 6.9 12.84 17.23 21.24 24.05 27.01 28.49 32.95 36.33 40.1 44.06 48.21 51.94 3 3 Q u 10 (C) 16.87 18.06 19.51 21.42 22.99 24.05 24.12 23.35 22.5 21.53 20.33 19.44 18.98 19.36 t (ms) 56.94 59.9 62.86 66.69 70.52 75.3 78.48 83.26 86.29 89.33 93.21 97.09 101.6 107.7 Solution (a) The user-defined function FirstDerivLag is used in the following script file for calculating I t = dQ ------- , and making the plot. dt Q=[0 6.67 16.93 23.38 27.44 29.02 29.5 29.29 27.17 24.58 21.43 18.56 16.61 ... 16.01 16.87 18.06 19.51 21.42 22.99 24.05 24.12 23.35 22.5 21.53 20.33 ... 19.44 18.98 19.36]*0.001; t=[0 6.9 12.84 17.23 21.24 24.05 27.01 28.49 32.95 36.33 40.1 44.06 48.21 ... 51.94 56.94 59.9 62.86 66.69 70.52 75.3 78.48 83.26 86.29 89.33 93.21 ... 97.09 101.6 107.7]*0.001; I=FirstDerivLag(t,Q) plot(t,I) xlabel('time, t (s)'); ylabel('Current, I (A)'); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 When the program is executed, the following values of I are displayed in the Command Window, and the figure that is shown on the right is displayed in the Figure Window. 2 Current, I (A) 1.5 1 I = 0.5 Columns 1 through 7 0 0.5579 1.3754 1.5789 1.2305 −0.5 0.7478 0.3674 -0.0405 −1 Columns 8 through 14 0 -0.2250 -0.6408 -0.7990 -0.7815 0.6003 -0.3071 -0.0186 Columns 15 through 21 0.3165 0.4459 0.4937 0.4543 0.3262 Columns 22 through 28 -0.2342 -0.2998 -0.3148 -0.2693 -0.1705 0.02 0.04 0.06 0.08 0.1 0.12 time, t (s) 0.1018 -0.0511 -0.0322 0.1568 (b) The diff command in MATLAB performs the equivalent of a two-point forward difference. TheredQ fore, it can be used for evaluating the derivative ------- at all points except the last point. The following script dt ------- . The plot does not include the last file uses the MATLAB built-in function diff, to calculate I t = dQ dt point since no value for I is calculated for the last point. Q=[0 6.67 16.93 23.38 27.44 29.02 29.5 29.29 27.17 24.58 21.43 18.56 16.61 ... 16.01 16.87 18.06 19.51 21.42 22.99 24.05 24.12 23.35 22.5 21.53 20.33 ... 19.44 18.98 19.36]*0.001; t=[0 6.9 12.84 17.23 21.24 24.05 27.01 28.49 32.95 36.33 40.1 44.06 48.21 ... 51.94 56.94 59.9 62.86 66.69 70.52 75.3 78.48 83.26 86.29 89.33 93.21 ... 97.09 101.6 107.7]*0.001; I = diff(Q)./diff(t) n=length(Q); plot(t(1:n-1),I) xlabel('time, t (s)'); ylabel('Current, I (A)'); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 When the program is executed, the following values of I are displayed in the Command Window, and the figure that is shown on the right is displayed in the Figure Window. 2 Current, I (A) 1.5 1 I = 0.5 Columns 1 through 7 0 0.9667 1.7273 1.4692 1.0125 −0.5 0.5623 0.1622 -0.1419 Columns 8 through 14 −1 0 -0.4753 -0.7663 -0.8355 -0.7247 0.4699 -0.1609 0.1720 Columns 15 through 21 0.4020 0.4899 0.4987 0.4099 0.2218 Columns 22 through 27 -0.2805 -0.3191 -0.3093 -0.2294 -0.1020 0.02 0.04 0.06 0.08 0.1 0.12 time, t (s) 0.0220 -0.1611 0.0623 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.25 A radar station is tracking the motion of an aircraft. The recorded distance to the aircraft, r, and the angle T during a period of 60 s is given in the following table. The magnitude of the instantaneous velocity and acceleration of the aircraft can be calculated by: 2 dT 2 § dr -----· + § r ------· © dt ¹ © dt ¹ v = a = 2 2 d-------r- – r § dT ------· 2 © ¹ dt dt 2 2 d T dr dT + r --------2 + 2 ----- -----dt dt dt 2 Determine the magnitudes of the velocity and acceleration at the times given in the table. Plot the velocity and acceleration versus time (two separate plots on the same page). Solve the problem by writing a program in a script file. The program evaluates the various derivatives that are required for calculating the velocity and acceleration, and then makes the plots. For calculating the derivatives use: (a) The user-defined function FrstScndDeriv that was written in Problem 6.16. (b) MATLAB’s built-in function diff. t (s) 0 4 8 12 16 20 24 28 r (km) 18.803 18.861 18.946 19.042 19.148 19.260 19.376 19.495 T(rad) 0.7854 0.7792 0.7701 0.7594 0.7477 0.7350 0.7215 0.7073 t (s) 32 36 40 44 48 52 56 60 r (km) 19.617 19.741 19.865 19.990 20.115 20.239 20.362 20.484 T(rad) 0.6925 0.6771 0.6612 0.6448 0.6280 0.6107 0.5931 0.5750 Solution (a) The following script, in which the user-defined function FrstScndDeriv (written in Problem 6.16) is used for calculating the derivatives and solve this problem. t=0:4:60; r=[18.803 18.861 18.946 19.042 19.148 19.260 19.376 19.495 ... 19.617 19.741 19.865 19.990 20.115 20.239 20.362 20.484]*1000; theta=[0.7854 0.7792 0.7701 0.7594 0.7477 0.7350 0.7215 0.7073 ... 0.6925 0.6771 0.6612 0.6448 0.6280 0.6107 0.5931 0.5750]; [drdt,d2rdt2] = FrstScndDeriv(t,r); [d_theta_dt,d2_theta_dt2] = FrstScndDeriv(t,theta); v=sqrt(drdt.^2+(r.*d_theta_dt).^2) term1=(d2rdt2-r.*d_theta_dt.^2).^2; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 term2=(r.*d2_theta_dt2+2*drdt.*d_theta_dt).^2; a=sqrt(term1+term2) subplot(2,1,1) plot(t,v); xlabel('Time, t(s)'); ylabel('Velocity, v(m/s)'); subplot(2,1,2) plot(t,a); xlabel('Time, t(s)'); ylabel('Acceleration, a(m/s^2)'); Velocity, v(m/s) 80 60 40 20 0 10 20 30 40 50 60 40 50 60 Time, t(s) 2 v = Columns 1 through 8 24.9465 40.2577 52.0643 58.9943 64.4460 69.2163 73.2385 76.8224 Columns 9 through 16 80.1847 83.2256 86.0325 88.6492 91.2148 93.5353 95.8875 98.7576 a = Columns 1 through 8 5.6307 3.8442 2.0864 1.4140 1.3771 1.1505 1.0521 0.9519 Columns 9 through 16 0.9801 0.9111 0.9104 0.8335 0.9911 0.7883 1.0230 1.2743 100 Acceleration, a(m/s ) When the program is executed, the following results are displayed in the Command Window, and the figure shown on the right in displayed in the Figure Window. 6 4 2 0 0 10 20 30 Time, t(s) (b) The following script, in which the MATLAB built-in function diff is used for calculating the derivatives solves this problem. Note that when the diff function is used for calculating the first derivative by writing drdt=diff(r)./diff(t)and d_theta_dt=diff(theta)./diff(t) it does not calculate the derivative at the last point. The second derivative is calculated by writing d2rdt2=diff(r,2)/h/h and d2_theta_dt2=diff(theta,2)/h/h. In this case the second derivative is not calculated in the last two points. clear all t=0:4:60; r=[18.803 18.861 18.946 19.042 19.148 19.260 19.376 19.495 ... Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 19.617 19.741 19.865 19.990 20.115 20.239 20.362 20.484]*1000; theta=[0.7854 0.7792 0.7701 0.7594 0.7477 0.7350 0.7215 0.7073 ... 0.6925 0.6771 0.6612 0.6448 0.6280 0.6107 0.5931 0.5750]; [drdt,d2rdt2] = FrstScndDeriv(t,r); [d_theta_dt,d2_theta_dt2] = FrstScndDeriv(t,theta); h=t(2)-t(1); n=length(t); drdt=diff(r)./diff(t); d_theta_dt=diff(theta)./diff(t); d2rdt2=diff(r,2)/h/h; d2_theta_dt2=diff(theta,2)/h/h; v=sqrt(drdt.^2+(r(1:n-1).*d_theta_dt).^2) term1=(d2rdt2-r(1:n-2).*d_theta_dt(1:n-2).^2).^2; term2=(r(1:n-2).*d2_theta_dt2+2*drdt(1:n-2).*d_theta_dt(1:n-2)).^2; a=sqrt(term1+term2) subplot(2,1,1) plot(t(1:n-1),v(1:n-1)); xlabel('Time, t(s)'); ylabel('Velocity, v(m/s)'); subplot(2,1,2) plot(t(1:n-2),a(1:n-2)); s^2)'); xlabel('Time, t(s)'); ylabel('Acceleration, a(m/ Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 Velocity, v(m/s) 80 60 40 20 0 10 20 30 40 50 60 40 50 60 Time, t(s) 2 v = Columns 1 through 8 32.5524 47.8824 56.0760 61.6806 66.9330 71.1781 74.9427 78.3148 Columns 9 through 15 81.6400 84.3719 87.2359 89.5852 92.3555 94.2112 97.0550 a = Columns 1 through 8 3.8237 2.0687 1.4008 1.3618 1.1366 1.0389 0.9395 0.9672 Columns 9 through 14 0.9000 0.8987 0.8236 0.9795 0.7803 1.0109 100 Acceleration, a(m/s ) When the program is executed, the following results are displayed in the Command Window, and the figure shown on the right in displayed in the Figure Window. 4 3 2 1 0 0 10 20 30 Time, t(s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 6.26 A projectile is shot from a 200 m tall cliff as shown. Its position (x and y coordinates) as a function of time, t, is given in the table that follows. The velocity of the projectile, v, is given by v = 2 2 v x + v y where wy the horizontal and vertical components, v x and v y are given by: v x = wx ----- and v y = ----- . Write a MATLAB wt wt program in a script file that: (a) Determines v x and v y by using the user-defined function FirstDeriv that was written in Problem 6.14. (b) Determines the velocity v of the projectile. (c) Displays a figure with plots of v x , v y and v as a function of time (three plots in one figure). t (s) 0 2 4 6 8 10 12 14 16 18 x (m) 0 198 395 593 790 988 1185 1383 1580 1778 y (m) 200 523 806 1050 1254 1420 1546 1633 1681 1690 t (s) 20 22 24 26 28 30 32 34 36 x (m) 1975 2173 2370 2568 2765 2963 3160 3358 3555 y (m) 1659 1589 1480 1331 1144 917 651 345 1 Solution The following script, in which the user-defined function FirstDeriv (written in Problem 6.14) is used for calculating the derivatives and solve this problem. clear all t=0:2:36; x=[0 198 395 593 790 988 1185 1383 1580 1778 1975 2173 2370 ... 2568 2765 2963 3160 3358 3555]; y=[200 523 806 1050 1254 1420 1546 1633 1681 1690 1659 1589 1480 1331 ... 1144 917 651 345 1]; vx=FirstDeriv(t,x); vy=FirstDeriv(t,y); v=sqrt((vx.^2)+(vy.^2)); subplot(3,1,1) plot(t,vx); xlabel('time, t(s)'); ylabel('Vx (m/s)'); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 subplot(3,1,2) plot(t,vy); xlabel('time, t(s)'); ylabel('Vy (m/s)'); subplot(3,1,3) plot(t,v); xlabel('time, t(s)'); ylabel('Speed, V (m/s)'); Vx (m/s) 99 98.5 98 0 10 20 30 40 30 40 30 40 time, t(s) 200 Vy (m/s) vx = Columns 1 through 8 99.2500 98.7500 98.7500 98.7500 98.7500 98.7500 98.7500 98.7500 Columns 9 through 16 98.7500 98.7500 98.7500 98.7500 98.7500 98.7500 98.7500 98.7500 Columns 17 through 19 98.7500 98.7500 98.2500 vy = Columns 1 through 8 171.5000 151.5000 131.7500 112.0000 92.5000 73.0000 53.2500 33.7500 Columns 9 through 16 14.2500 -5.5000 -25.2500 -44.7500 -64.5000 -84.0000 -103.5000 -123.2500 Columns 17 through 19 -143.0000 -162.5000 -181.5000 v = Columns 1 through 8 198.1485 180.8420 164.6500 149.3170 104.3582 Columns 9 through 16 99.7729 98.9030 101.9271 108.4164 157.9308 Columns 17 through 19 173.7831 190.1521 206.3863 99.5 0 −200 0 10 20 time, t(s) Speed, V (m/s) When the program is executed, the following results are displayed in the Command Window, and the figure shown on the right in displayed in the Figure Window. 300 200 100 0 0 10 20 time, t(s) 135.3064 122.8029 112.1924 117.9483 129.6440 143.0518 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.1 The function f x is given in the following tabulated form. Compute ³ 1 0 f x dx with h = 0.25 and with h = 0.5 , using: (a) The composite rectangle method. (b) The composite midpoint method. Use linear interpolation to determine f x at the midpoints. (c) The composite trapezoidal method. x f x 0 0.9162 0.25 0.8109 0.5 0.6931 0.75 0.5596 1.0 0.4055 Solution (a) For the composite rectangle method, Eq. (7.4) is used: I f = ³ b N f x dx | h a ¦f x i i=1 There are five points spaced 0.25 units apart, so if h = 0.25 , there are four intervals and N = 4 . In this case, Eq. (7.4) gives I f | 0.25 0.9162 + 0.8109 + 0.6931 + 0.5596 I f | 0.7450 Note that the last point is not used because the interval between x = 0.75 and x = 1.0 is approximated by f x = 0.5596 . If h = 0.5 , there are two intervals (i.e. N = 2 ). In this case, Eq. (7.4) gives I f | 0.5 0.9162 + 0.6931 I f | 0.8047 (b) For the composite midpoint method, use Eq. (7.7): I f = ³ b a N f x dx | h xi + xi + 1 -· ¦ f §© -------------------¹ 2 i=1 For h = 0.25 , there are four intervals and linear interpolation must be used to determine f x at the midpoints: x i + x i + 1· yi + yi + 1 - = -------------------f § -------------------© ¹ 2 2 Eq. (7.7) gives: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 0.9162 + 0.8109 0.8109 + 0.6931 0.6931 + 0.5596 0.5596 + 0.4055 I f | 0.25 §© --------------------------------------- + --------------------------------------- + --------------------------------------- + ---------------------------------------·¹ 2 2 2 2 I f | 0.6811 For h = 0.5 , there are two intervals and the values of f x at the midpoints are given. In this case, Eq. (7.7) gives I f | 0.5 0.8109 + 0.5596 I f | 0.6853 (c) For the given data, the subintervals are all the same width, so Eq. (7.13) can be used to integrate by the composite trapezoidal method: h I f | --- > f a + f b @ + h 2 N–1 ¦f x i i=2 For h = 0.25 , there are four intervals and N = 4 . In this case, Eq. (7.13) gives 0.25 I f | ---------- 0.9162 + 0.4055 + 0.25 0.8109 + 0.6931 + 0.5596 2 I f | 0.6811 For h = 0.5 , there are two intervals and N = 2 . In this case, Eq. (7.13) gives 0.5 I f | ------- 0.9162 + 0.4055 + 0.5 0.6931 2 I f | 0.6770 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.2 To estimate the surface area and volume of a football, the diameter of the ball is measured at different points along the ball. The surface area, S, and volume, V, can be determined by: S = 2S ³ L r dz and 0 V = S ³ L r 2 dz 0 Use the composite Simpson’s 1/3 method, with the data given below to determine the volume and surface area of the ball. z (in.) d (in.) 0 0 1.0 2.6 2.0 3.2 3.0 4.8 4.0 5.6 5.0 6 6.0 6.2 7.0 6.0 8.0 5.6 9.0 4.8 10.0 3.3 11.0 2.6 12.0 0 Solution The composite Simpson’s 1/3 method is given by Eq. (7.19): N N–1 ¦ ¦ h f xi + 2 f xj + f b I f | --- f a + 4 3 i = 2 4 6 j = 3 5 7 In the present problem, there are 13 points and 12 subintervals. Note that the requirements given for Eq. (7.19) on p. 278 are met. That is, the subintervals are equally spaced and of an even number. To calculate the surface area S, The integral I r = ³ L 0 r dz has to be calculated. First, convert the given data from d to r using r = d--- : 2 z (in.) r (in.) 0 0 1.0 1.3 2.0 1.6 3.0 2.4 4.0 2.8 5.0 3.0 6.0 3.1 7.0 3.0 8.0 2.8 9.0 2.4 10.0 1.7 11.0 1.3 12.0 0 Next, use Eq. (7.19) to find I r for h = 1.0 in and N = 12 : 1.0 I r | ------- > 0 + 4 1.3 + 2.4 + 3.0 + 3.0 + 2.4 + 1.3 + 2 1.6 + 2.8 + 3.1 + 2.8 + 1.7 + 0 @ 3 I r | 25.8667 in2 Finally, calculate S using S = 2SI r | 2S 25.8667 S | 162.5251 in2 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 To find the volume V, the integral I r 2 = r 2 ³ L r 2 dz must be evaluated. First convert the given data from d to 0 2 using r 2 = §© d--- ·¹ : 2 z (in.) 2 0 0 2 r (in. ) 1.0 1.69 2.0 2.56 3.0 5.76 4.0 7.84 5.0 9.0 6.0 9.61 7.0 9.0 8.0 7.84 9.0 5.76 10.0 2.89 11.0 1.69 12.0 0 Next, use Eq. (7.19) to find I r 2 using h = 1.0 in and N = 12 : I r 2 1.0 | ------- > 0 + 4 1.69 + 5.76 + 9.0 + 9.0 + 5.76 + 1.69 + 2 2.56 + 7.84 + 9.61 + 7.84 + 2.89 + 0 @ 3 I r 2 | 64.36 in3 Finally, calculate V by: V = SI r 2 | S64.36 in3 V | 202.1929 in3 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.3 An approximate map of the state of Texas is shown in the figure. For determining the area of the state, the map is divided into two parts (one above and one below the x axis). Determine the area of the state by numerically integrating the two areas. For each part make a list of the coordinate y of the border as a function of x. Start with x = 0 and use increments of 50 mi, such that the last point is x = 750 . Once the tabulated data is available, determine the integrals once with the composite midpoint method and once with the composite Simpson’s 3/8 method. Solution The coordinates of the border y at 50-mile increments of x are as follows: x 0 above 0 below 0 50 0 50 100 0 100 150 0 175 200 0 200 550 above 125 below 400 600 125 250 650 125 225 700 125 150 750 0 150 x 250 300 150 300 300 150 350 300 200 400 175 300 450 150 375 500 125 400 The area of the state is found by integrating the areas above and below the x-axis. Eq. (7.7) is used to integrate using the composite midpoint method: I f = ³ b N f x dx | h a xi + xi + 1 -· ¦ f §© -------------------¹ 2 i=1 In this case, there are 16 data points defining 15 intervals. Linear interpolation is used to determine f x at the midpoints: x i + x i + 1· yi + yi + 1 - = -------------------f § -------------------© ¹ 2 2 Using the linear interpolation in Eq. (7.7) gives: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 I f = ³ b a N f x dx | h yi + yi + 1 ¦ -------------------2 i=1 The following MATLAB script is used to calculate the area using Eq. (7.7) with h = 50 mi and N = 15 : clear all; clc; x=0:50:750; y_above=[0 0 0 0 0 300 300 300 175 150 125 125 125 125 125 0]; y_below=[0 50 100 175 200 150 150 200 300 375 400 400 250 225 150 150]; N=length(x)-1; h=50; %mi, length of subinterval % Integrate the areas above and below the x-axis using composite midpoint % method, Eq. (7.7) for i=1:N i_above(i)=(y_above(i)+y_above(i+1))/2; i_below(i)=(y_below(i)+y_below(i+1))/2; end I_above=h*sum(i_above); I_below=h*sum(i_below); % Sum the integrals above and below the x-axis to find the total area I=I_above+I_below When the above script is executed, the value of I f appears in the Command Window: I = 252500 The total area of Texas by the composite midpoint method is I f = 252 500 mi2. The composite Simpson’s 3/8 method is given by Eq. (7.22): N–1 ¦ N–2 ¦ 3h > f xi + f xi + 1 @ + 2 f xj + f b I f | ------ f a + 3 8 i = 2 5 8 j = 4 7 10 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 The requirements for Eq. (7.22) are that the subintervals must be evenly spaced and that their number must be divisible by 3 (see p. 281). There are 15 subintervals spaced at intervals of 50 mi, so the requirements are met. The following MATLAB script is used to calculate the area using Eq. (7.22) with h = 50 mi and N = 15 : clear all; clc; x=0:50:750; y_above=[0 0 0 0 0 300 300 300 175 150 125 125 125 125 125 0]; y_below=[0 50 100 175 200 150 150 200 300 375 400 400 250 225 150 150]; N=length(x)-1; h=50; %mi, length of subinterval % Integrate the areas above and below the x-axis using Eq. (7.22) I_above=3*h/8*(y_above(1)+2*sum(y_above(4:3:(N-2)))+y_above(N+1)); I_above=I_above+3*h/8*3*(sum(y_above(2:3:(N-1)))+sum(y_above(3:3:N))); I_below=3*h/8*(y_below(1)+2*sum(y_below(4:3:(N-2)))+y_below(N+1)); I_below=I_below+3*h/8*3*(sum(y_below(2:3:(N-1)))+sum(y_below(3:3:N))); I=I_above+I_below When the above script is executed, the value of I f appears in the Command Window: I = 254060 The total area of Texas by the composite Simpson’s 3/8 method is I f = 254 060 mi2. The area of Texas is actually 261,797 mi2 (U.S. Census Bureau). Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.4 Evaluate the integrals: I1 = ³ S 0 sin2 x dx and I 2 = ³ 2S sin2 x dx 0 using the following methods: (a) Simpson’s 1/3 method. Divide the whole interval into six subintervals. (b) Simpson’s 3/8 method. Divide the whole interval into six subintervals. (c) Second-order Gauss quadrature. Compare the results and discuss the reasons for the differences. The exact value of the integrals is I 1 = S e 2 , and I 2 = S . Solution S 2S – 0 S S–0 b–a - . For I 1 , h = ------------ = --- , and for I 2 , h = --------------- = --- . The data points used to (a) For N = 6 , h = ----------N 3 6 6 6 find the two integrals are as follows: I1 : x 0 S --6 S --3 S --2 2S -----3 5S -----6 S f x 0 0.2500 0.7500 1.0000 0.7500 0.2500 0 x 0 S --3 2S -----3 S 4S -----3 5S -----3 2S f x 0 0.7500 0.7500 0 0.7500 0.7500 0 I2 : Eq. (7.19) is used to calculate both integrals by the composite Simpson’s 1/3 method: N ¦ N–1 ¦ h f xi + 2 f xj + f b I f | --- f a + 4 3 i = 2 4 6 j = 3 5 7 The following MATLAB script is used to calculate I 1 and I 2 using Eq. (7.19): clear all; clc; x1=0:(pi/6):pi; y1=sin(x1).^2; h1=pi/6; x2=0:(pi/3):2*pi; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 y2=sin(x2).^2; h2=pi/3; N=length(x1)-1; % Integrate by Simpon's 1/3 method using Eq. (7.19) I1=h1/3*(y1(1)+4*sum(y1(2:2:N))+2*sum(y1(3:2:(N-1)))+y1(N+1)) I2=h2/3*(y2(1)+4*sum(y2(2:2:N))+2*sum(y2(3:2:(N-1)))+y2(N+1)) When the script is executed, the calculated values of I 1 and I 2 are displayed in the Command Window: I1 = 1.5708 I2 = 3.1416 The exact values of the integrals are I 1 = S--- and I 2 = S . The error in both I 1 and I 2 is less than 1%. 2 (b) Eq. (7.22) is used to calculate both integrals using Simpson’s 3/8 method: N–1 ¦ N–2 ¦ 3h > f xi + f xi + 1 @ + 2 f xj + f b I f | ------ f a + 3 8 j = 4 7 10 i = 2 5 8 The following MATLAB script is used to calculate I 1 and I 2 using Eq. (7.22): clear all; clc; x1=0:(pi/6):pi; y1=sin(x1).^2; h1=pi/6; x2=0:(pi/3):2*pi; y2=sin(x2).^2; h2=pi/3; N=length(x1)-1; % Integrate by Simpon's 3/8 method using Eq. (7.22) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 I1=3*h1/8*(y1(1)+2*sum(y1(4:3:(N-2)))+y1(N+1)); I1=I1+3*h1/8*3*(sum(y1(2:3:(N-1)))+sum(y1(3:3:N))) I2=3*h2/8*(y2(1)+2*sum(y2(4:3:(N-2)))+y2(N+1)); I2=I2+3*h2/8*3*(sum(y2(2:3:(N-1)))+sum(y2(3:3:N))) When the script is executed, the calculated values of I 1 and I 2 are displayed in the Command Window: I1 = 1.5708 I2 = 3.5343 The error in I 1 is less than 1%, but the error in I 2 is 12.5%. Recall from Section 7.4 that Simpson’s 1/3 method fits three splines to a function divided into six subintervals, but that Simpson’s 3/8 method fits only two splines over six subintervals (see Figures 7-16 and 7-18). In this case, the error on I 2 is greater because we are integrating the same function using the same number of subintervals as I 1 but over a greater range of integration. It is expected that the error would be reduced if the number of subintervals were increased. (c) Eq. (7.25) is used to calculate the integrals using second-order Gauss quadrature: ³ 1 f x dx | C 1 f x 1 + C 2 f x 2 –1 The coefficients and Gauss points for second-order Gauss quadrature are given in Table 7-1 and are valid only if the range of integration is [-1,1]. Because the ranges of I 1 and I 2 are > 0 S @ and > 0 2S @ respectively, the integrals must be rewritten according to Eq. (7.31): 1 x = --- > t b – a + a + b @ 2 and 1 dx = --- b – a dt 2 The integration then has the form: ³ b f x dx = a ³ 1 –1 b–a t+a+b b–a f § -------------------------------------· ---------------- dt © ¹ 2 2 For I 1 , a = 0 and b = S , so S ³ 0 sin2 x dx = ³ 1 –1 St + S S sin2 § ---------------· --- dt © 2 ¹2 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 For second-order Gauss quadrature, C 1 = 1 , t 1 = – 0.57735027 , C 2 = 1 and t 2 = 0.57735027 . Evaluating Eq. (7.25) at these values of C i t i gives: I1 = ³ S0.57735027 + S S S – 0.57735027 + S S f t dt | C 1 f t 1 + C 2 f t 2 = sin2 § -------------------------------------------------· --- + sin2 § -----------------------------------------· --© ¹2 © ¹ 2 2 2 –1 I 1 = 0.5964 + 0.5964 = 1.1928 1 For I 2 , a = 0 and b = 2S , so Eq. (7.31) gives 2S ³ 0 sin2 x dx = ³ 1 sin2 St + S S dt –1 Evaluating Eq. (7.25) gives: I2 = ³ 1 f t dt | C 1 f t 1 + C 2 f t 2 = sin2 S – 0.57735027 + S S + sin2 S0.57735027 + S S –1 I 1 = 2.9597 + 2.9597 = 5.9194 The error in I 1 is approximately 24% and the error in I 2 is approximately 88%. For second-order Gauss quadrature we have picked only two Gauss points. The error in both integrals would be reduced by using a higher-order Gauss quadrature. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.5 The equation of a circle with a radius of 1 (unit circle) is given by x 2 + y 2 = 1 , and its area is A = S . Consequently: ³ S 1 – x 2 dx = --2 1 –1 Evaluate the integral using the following methods: (a) Simpson’s 1/3 method. Divide the whole interval into eight subintervals. (b) Simpson’s 3/8 method. Divide the whole interval into nine subintervals. (c) Second order Gauss quadrature. Compare the results and discuss the reasons for the differences. Solution b – a = 1--- . (a) For N = 8 , h = ----------N 4 The composite Simpson’s 1/3 method is given by Eq. (7.19): N ¦ N–1 ¦ h f xi + 2 f xj + f b I f | --- f a + 4 3 i = 2 4 6 j = 3 5 7 The following MATLAB script is used to calculate I using Eq. (7.19): clear all; clc; a=-1; b=1; N=8; h=(b-a)/N; x=a:h:b; y=sqrt(1-x.^2); % Integrate by Simpon's 1/3 method using Eq. (7.19) I=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1)) When the script is executed, the value of I is displayed in the Command Window: I = 1.5418 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The exact value of the integral is I = S--- , so the error for this method is less than 2%. 2 2 b–a (b) For N = 9 , h = ----------- = --- . Eq. (7.22) is used to calculate the integral using Simpson’s 3/8 method: N 9 N–1 ¦ N–2 ¦ 3h > f xi + f xi + 1 @ + 2 f xj + f b I f | ------ f a + 3 8 i = 2 5 8 j = 4 7 10 The following MATLAB script is used to calculate I using Eq. (7.22): clear all; clc; a=-1; b=1; N=9; h=(b-a)/N; x=a:h:b;y=sqrt(1-x.^2); % Integrate by Simpon's 3/8 method using Eq. (7.22) I=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); I=I+3*h/8*3*(sum(y(2:3:(N-1)))+sum(y(3:3:N))) When the script is executed, the value of I is displayed in the Command Window: I = 1.5412 The exact value of the integral is I = S--- , so the error for this method is less than 2%. 2 (c) Eq. (7.25) is used to calculate the integrals using second-order Gauss quadrature: ³ 1 f x dx | C 1 f x 1 + C 2 f x 2 –1 For second-order Gauss quadrature, C 1 = 1 , x 1 = – 0.57735027 , C 2 = 1 and x 2 = 0.57735027 . Evaluating Eq. (7.25) at these values of C i x i gives: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 I = ³ 1 f x dx | C 1 f x 1 + C 2 f x 2 = 1 – – 0.57735027 2 + 1 – 0.57735027 2 –1 I 1 = 0.8165 + 0.8165 = 1.6330 The error for this method is approximately 4%. One would expect the error to decrease if a higher-order Gauss quadrature were to be used. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.6 The shape of the centroid line of the Gateway Arch in St. Louis can be modeled approximately with the equation: x f x = 693.9 – 68.8 cosh § ----------· © 99.7¹ By using the equation L = ³ b a for – 299.25 d x d 299.25 ft. 1 + > f c x @ 2 dx determine the length of the arch with the following integration methods: (a) Simpson’s 1/3 method. Divide the whole interval into eight subintervals. (b) Simpson’s 3/8 method. Divide the whole interval into nine subintervals. (c) Second order Gauss quadrature. Solution 2 299.25 b–a (a) For N = 8 ,and evenly-spaced points h = ----------- = ------------------------ = 74.8125 ft. The derivative of N 8 f x is: 68.8 x x f ' x = – ---------- sinh § ----------· = – 0.6901 sinh § ----------· © 99.7¹ © 99.7¹ 99.7 The length of the arch is given by: L = ³ b a x 1 + 0.6901 sinh § ----------· © 99.7¹ 2 dx Using the composite Simpson’s 1/3 method (Eq. (7.19)) gives: N ¦ N–1 ¦ h f xj + f b f xi + 2 L f | --- f a + 4 3 j = 3 5 7 i = 2 4 6 The last equation is used in the following program (script file) to calculate L . clear all; clc; a=-299.25; b=299.25; N=8; h=(b-a)/N; x=a:h:b; fprime=-0.6901.*sinh(x./99.7); y=sqrt(1+fprime.^2); % Integrate by Simpon's 1/3 method using Eq. (7.19) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 L=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1)) When the script is executed, the value of L is displayed in the Command Window: L = 1482.3 The length of the arch as calculated by Simpson’s 1/3 method is L = 1482.3 ft. b–a 2 299.25 (b) For N = 9 and evenly-spaced points h = ----------- = ------------------------ = 66.5 ft. N 9 Eq. (7.22) is used to calculate the length using Simpson’s 3/8 method: N–1 ¦ N–2 ¦ 3h > f xi + f xi + 1 @ + 2 f xj + f b L f | ------ f a + 3 8 i = 2 5 8 j = 4 7 10 The last equation is used in the following program (script file) to calculate L . clear all; clc; a=-299.25; b=299.25; N=9; h=(b-a)/N; x=a:h:b; fprime=-0.6901.*sinh(x./99.7); y=sqrt(1+fprime.^2); % Integrate by Simpon's 3/8 method using Eq. (7.22) L=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); L=I+3*h/8*3*(sum(y(2:3:(N-1)))+sum(y(3:3:N))) When the script is executed, the value of L is displayed in the Command Window: L = 1483.2 The length of the arch as calculated by Simpson’s 3/8 method is L = 1483.2 ft. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 (c) The coefficients and Gauss points for second-order Gauss quadrature are given in Table 7-1 and are valid if the range of integration is > – 1 1 @ . Because the range of integration in the present problem is > – 299.25 299.25 @ , the integral must be rewritten according to Eq. (7.31): 1 x = --- > t b – a + a + b @ 2 1 dx = --- b – a dt 2 and The integration then has the form: ³ b f x dx = a ³ 1 –1 b–a t+a+b b–a f § -------------------------------------· ---------------- dt © ¹ 2 2 Here, a = – 299.25 and b = 299.25 , so L = 299.25 ³ – 299.25 x 1 + 0.6901 sinh § ----------· © 99.7¹ 2 dx = ³ 1 –1 598.5t 1 + 0.6901 sinh § ---------------· © 199.4 ¹ 2 598.5 ------------------ dt 2 For second-order Gauss quadrature, C 1 = 1 , t 1 = – 0.57735027 , C 2 = 1 and t 2 = 0.57735027 . Evaluating Eq. (7.25) at these values of C i t i gives: L = ³ 598.5 – 0.57735027 598.5 § f t dt | ------------- ¨ 1 + 0.6901 sinh § --------------------------------------------------· © ¹ 199.4 2 © –1 1 2 2· 598.5 0.57735027 + 1 + 0.6901 sinh § -----------------------------------------------· ¸ © ¹ ¹ 199.4 598.5 L = ------------- 2.1391 + 2.1391 = 1280.3 2 The length of the arch as calculated by second-order Gauss quadrature is L = 1280.3 ft. It is expected that if a higher-order Gauss quadrature were used, it would yield better agreement with the values of L obtained in parts (a) and (b). Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.7 The roof of a silo is made by revolving the curve S y = 10 cos § ------ x· from x = – 5 m to x = 5 m about the y axis, as © 10 ¹ shown in the figure to the right. The surface area, S, that is obtained by revolving a curve y = f x in the domain from a to b around the y axis can be calculated by: S = 2S ³ b a x 1 + > f c x @ 2 dx Calculate the surface area of the roof with the following integration methods: (a) Simpson’s 1/3 method. Divide the whole interval into eight subintervals. (b) Simpson’s 3/8 method. Divide the whole interval into nine subintervals. (c) Second order Gauss quadrature. Solution b – a- = 5 (a) The limits of integration are a = 0 and b = 5 . For N = 8 and evenly-spaced points h = ------------- ft. N 8 The derivative of f x is: S ------ = – S sin § ------ x· f ' x = dy © 10 ¹ dx The surface area of the roof of the silo is therefore: S = 2S S 1 + S sin § ------ x· © 10 ¹ 5 ³x 0 2 dx Using the composite Simpson’s 1/3 method (Eq. (7.19)) gives: N ¦ N–1 ¦ h f xi + 2 f xj + f b S f | --- f a + 4 3 i = 2 4 6 j = 3 5 7 The last equation is used in the following program (script file) to calculate S . clear all; clc; a=0; b=5; N=8; h=(b-a)/N; x=a:h:b; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 fprime=-pi*sin(pi/10.*x); y=x.*sqrt(1+fprime.^2); % Integrate by Simpon's 1/3 method using Eq. (7.19) S=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1)); S=S*2*pi When the script is executed, the value of S is displayed in the Command Window: S = 216.3386 The surface area of the roof as calculated by Simpson’s 1/3 method is 216.3386 ft2. 5 b–a - = --- ft. (b) For N = 9 and evenly-spaced points h = ----------N 9 Eq. (7.22) is used to calculate the surface area using Simpson’s 3/8 method: N–1 ¦ N–2 ¦ 3h > f xi + f x i + 1 @ + 2 f xj + f b S f | ------ f a + 3 8 i = 2 5 8 j = 4 7 10 The last equation is used in the following program (script file) to calculate S . clear all; clc; a=0; b=5; N=9; h=(b-a)/N; x=a:h:b; fprime=-pi*sin(pi/10.*x); y=x.*sqrt(1+fprime.^2); % Integrate by Simpon's 3/8 method using Eq. (7.22) S=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); S=S+3*h/8*3*(sum(y(2:3:(N-1)))+sum(y(3:3:N))); S=2*pi*S Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 When the script is executed, the value of S is displayed in the Command Window: S = 216.3302 The surface area of the silo roof as calculated by Simpson’s 3/8 method is 216.3302 ft2. (c) The coefficients and Gauss points for second-order Gauss quadrature are given in Table 7-1 and are valid if the range of integration is > – 1 1 @ . Because the range of integration in the present problem is > 0 5 @ , the integral must be rewritten according to Eq. (7.31): 1 x = --- > t b – a + a + b @ 2 1 dx = --- b – a dt 2 and The integration then has the form: ³ b f x dx = a ³ b–a t+a+b b–a f § -------------------------------------· ---------------- dt © ¹ 2 2 –1 1 Here, a = 0 and b = 5 , so S = 2S ³ S x 1 + S sin § ------ x· © 10 ¹ 0 5 2 dx = 2S ³ 2 §5 ---· t + 1 – 1 © 2¹ 1 S t+1 1 + S sin § -------------------· © 4 ¹ 2 dt For second-order Gauss quadrature, C 1 = 1 , t 1 = – 0.57735027 , C 2 = 1 and t 2 = 0.57735027 . Evaluating Eq. (7.25) at these values of C i t i gives: 5 S = 2S § ---· © 2¹ 2 – 0.57735027 + 1 + 0.57735027 + 1 S – 0.57735027 + 1 1 + S sin § --------------------------------------------------· © ¹ 4 S 0.57735027 + 1 1 + S sin § ----------------------------------------------· © ¹ 4 2 + 2 25 S = ------ S 0.6049 + 4.9433 = 217.8767 2 The silo roof surface area as calculated by second-order Gauss quadrature is 217.8767 ft2. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.8 In the standard Simpson’s 1/3 method (Eq. (7.16)), the points used for the integration are the endpoints of the domain, a and b, and the middle point a + b e 2 . Derive a new formula for Simpson’s 1/3 method in which the points used for the integration are x = a , x = b , and x = a + b e 3 . Solution Starting with the polynomial given by Eq.(7.15), > f x2 – f x1 @ f x3 – f x1 f x2 – f x1 - x – x 1 + ----------------------------------------p x = f x 1 + --------------------------------- – ----------------------------------------- x – x1 x – x2 x2 – x1 x3 – x1 x3 – x2 x2 – x1 x3 – x2 Here, x 1 = a , x 2 = a + b e 3 , and x 3 = b . Integrating from x = a to x = b yields: > f x2 – f a @ b2 a2 2 I = f a b – a + -------------------------------- § ----- – ----- – ab + a · ©2 2 ¹ x2 – a 2 2 f x2 – f a a + x2 a ­ b3 a3 b 2 ½ f b –f a + ------------------------------------ – ------------------------------------- ® ----- – ----- – a + x 2 ----- + -----------------------+ abx 2 – a x 2 ¾ 2 b – a b – x2 2 x2 – a b – x2 ¯ 3 3 ¿ This can be simplified to yield: > f x2 – f a @ I = f a b – a + -------------------------------- b – a 2 x2 – a 2 f x2 – f a 1 f b –f a 3 3 2 2 + --- ------------------------------------ – ------------------------------------- ^ 2b + a – 3ab – a + b b – a ` 6 b – a b – x2 x2 – a b – x2 This can be further simplified to yield: > f x2 – f a @ I = f a b – a + -------------------------------- b – a 2 x2 – a 2 f x2 – f a 1 f b –f a 3 2 2 + --- ------------------------------------ – ------------------------------------- ^ b – 2ab + a b ` 6 b – a b – x2 x2 – a b – x2 or, > f x2 – f a @ - b–a I = f a b – a + ------------------------------2 x2 – a 2 f x2 – f a b f b –f a - – ------------------------------------+ --- ----------------------------------- b–a 6 b – a b – x2 x2 – a b – x2 2 where x 2 = a + b e 3 . Substituting for x 2 yields: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 2 3 b–a a+b I = f a b – a + ----------------------- f § ------------· – f a 2 b – 2a © 3 ¹ a+b 3 f § ------------· – f a 2 © 3 ¹ b b – a >f b – f a @ + ----------------------- ------------------------------ – -------------------------------------------2 2b – a b – 2a b–a Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.9 Evaluate the integral 1 1 - 2 cos T + 5 cos 3T ³ ----64 2 sin T dT using Simpson’s 3/8 method and dividing the 0 whole interval into nine subintervals. Solution b – a = 1--- . For N = 9 , h = ----------N 9 Simpson’s 3/8 method is given by Eq. (7.22): N–1 ¦ N–2 ¦ 3h > f xi + f x i + 1 @ + 2 f xj + f b I f | ------ f a + 3 8 j = 4 7 10 i = 2 5 8 Eq. (7.22) is used in the following script file to calculate I . clear all; clc; a=0; b=1; N=9; h=(b-a)/N; x=a:h:b; y=1/64*(2*cos(x)+5*cos(3.*x)).^2.*sin(x); % Integrate by Simpon's 3/8 method using Eq. (7.22) I=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); I=I+3*h/8*3*(sum(y(2:3:(N-1)))+sum(y(3:3:N))) When the script is executed, the following value of I is displayed in the Command Window: I= 0.0721 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.10 Evaluate the integral 1 ³ 225x 1 – x 2 0 3 1 – x 2 dx using second-level Romberg integration. Use n = 1 in the first estimate with the trapezoidal method. Solution For second-level Romberg integration the integral is calculated with the composite trapezoidal integration three times using n 2n and 3n subintervals, respectively. After that, the second-level Romberg integration is calculated using the scheme described in Section 7.10: Subintervals n 1st level 2nd level I 1 1 I 1 2 2n I 2 1 I 1 3 I 2 2 3n I 3 1 The first column of integrals ( I 1 1 , I 2 1 and I 3 1 ) are calculated using the composite trapezoidal method given by Eq. (7.13): h I f | --- > f a + f b @ + h 2 N–1 ¦f x i i=2 The remaining integrals are calculated using Eq. (7.65): j–1 4 I i + 1 j – 1 – I i j – 1 - where j = 2 3 I i j = -----------------------------------------------j–1 4 –1 The following MATLAB program (script file) performs the calculations clear all; clc; a=0; b=1; n=1; k=3; % number of columns for i=1:k N=2^(i-1)*n; h=(b-a)/N; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 x=a:h:b; y=225*x.*(1-x.^2).*sqrt((1-x.^2).^3); % Calculate first column using composite trapezoidal method, Eq. (7.13) IR(i,1)=h/2*(y(1)+y(N+1))+h*sum(y(2:N)); end % Calculate extrapolated values using Romberg method, Eq. (7.65) for j=2:k for i=1:(k-j+1) IR(i,j)=(4^(j-1)*IR(i+1,j-1)-IR(i,j-1))/(4^(j-1)-1); end end IR When the script is executed, the k u k matrix > IR @ is displayed in the Command Window: IR = 0 27.4016 31.0090 36.5354 32.2115 0 31.9232 0 0 The value of the integral obtained by second-level Romberg integration is I = 31.9232 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.11 Evaluate the integral 53.3904 ³ 10 1 – e – 0.18355x dx by using third-order (two-point) Gauss quadrature. 0 Solution The following MATLAB script is used to perform the integration: clear all; a=0; b=10; % Coefficients and Gauss points from Table 7-1 C1=0.5555556; C2=0.8888889; C3=C1; t1=-0.77459667; x(1)=((b-a)*t1+a+b)/2; t2=0; x(2)=((b-a)*t2+a+b)/2; t3=0.77459667; x(3)=((b-a)*t3+a+b)/2; % Define F(x) F_x=(1-exp(-0.18355.*x)); I=C1*F_x(1)+C2*F_x(2)+C3*F_x(3); I=I*53.3904*(b-a)/2 When the script is executed, the value of the integral I appears in the Command Window: I = 289.4362 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 Evaluate the integral 7.12 ³ 0.8 0.2 + 25x – 200x 2 + 675x 3 – 900x 4 + 400x 5 dx 0 using third-order (two-point) Gauss quadrature. Solution The following MATLAB script is used to perform the integration: clear all; clc; a=0; b=0.8; % Coefficients and Gauss points from Table 7-1 C1=0.5555556; C2=0.8888889; C3=C1; t1=-0.77459667; x(1)=((b-a)*t1+a+b)/2; t2=0; x(2)=((b-a)*t2+a+b)/2; t3=0.77459667; x(3)=((b-a)*t3+a+b)/2; % Define F(x) F_x=(0.2+25.*x-200.*x.^2+675.*x.^3-900.*x.^4+400.*x.^5); I=C1*F_x(1)+C2*F_x(2)+C3*F_x(3); I=I*(b-a)/2 When the script is executed, the value of the integral I is displayed in the Command Window: I = 1.6405 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.13 Show that the truncation error for the composite trapezoidal method is of the order of h 2 , where h is the step size (width of subinterval). Solution The composite trapezoidal method is given by: N I f = ¦³ i=1 xi + 1 (1) f x dx xi The trapezoidal rule is derived by expressing the integrand in each subinterval > x i x i + 1 @ in a three term Taylor expansion about x = xi : 2 f s [ i x – xi f x = f x i + f c x i x – x i + ----------------------------------2 (2) Setting x = x i + 1 gives: 2 f s [i xi + 1 – xi f x i + 1 = f x i + f c x i x i + 1 – x i + ------------------------------------------2 (3) Solving Eq. (3) for the first derivative: f xi + 1 – f xi f s [i xi + 1 – xi f c x i = --------------------------------- – ----------------------------------------xi + 1 – xi 2 (4) Substituting (4) into (2) yields: 2 f xi + 1 – f xi f s [i xi + 1 – xi x – xi f s [i x – xi - x – x i – ---------------------------------------------------------f x = f x i + --------------------------------- + ----------------------------------xi + 1 – xi 2 2 (5) Substituting (5) into (1) and performing the integration yields: 3 = ¦ 3 > f x i + 1 – f xi @ x i + 1 – xi f s [i xi + 1 – xi ½ ­ ® f x i x i + 1 – x i + ----------------------------------------------------------------- – -------------------------------------------- ¾ 12 2 ¿ i = 1¯ N or I f 3 > f xi + 1 – f xi @ xi + 1 – xi f s [i xi + 1 – xi ­ f s [i x – xi ½ ® f x i x i + 1 – x i + ----------------------------------------------------------------- – ------------------------------------------- + ----------------------------------- ¾ 4 6 2 ¯ ¿ i=1 N I f = ¦ The first two terms in the series constitute the trapezoidal approximation for the integral, while the last term denotes the total truncation error: N E = – ¦ i=1 3 f s [i xi + 1 – xi -------------------------------------------12 For equal subintervals, x i + 1 – x i = h so that the accrued truncation error is: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 N E = – ¦ i=1 3 f s [i h --------------------12 (6) Now, following the discussion in Section 7.8, the second derivative f s [ i may be replaced by an average value, analogous to Eq. (7.42): N ¦f s [ i | Nf s (7) i=1 Substituting (7) into (6) and recognizing that Nh = b – a yields: 3 2 Nf sh b–a h f s E = – ---------------- = – ---------------------------12 12 Thus, the truncation error for the composite trapezoidal rule is of order O h 2 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.14 Write a user-defined MATLAB function for integration of a function, I = ³ b a y x dx , where x and y x are given as a set of tabulated points, with the midpoint method. For the function name and arguments use I=IntegrationMidpoint(x,y), where the input arguments x and y are vectors with the coordinates of the points, and I is the value of the integral. Use the function to solve Problem 7.9. Solution The listing of the user-defined MATLAB function IntegrationMidpoint is: function I = IntegrationMidpoint(x,y) % Numerically integrates a given function on [a,b] using the midpoint % method. Linear interpolation is used to determine the values of f(x) at % each of the midpoints. % Input variables: % x Vector of x-points % y Vector of y-points % Output variables: % I Value of integral N=length(x)-1; a=x(1); b=x(N+1); h=(b-a)/N; for i=1:N int(i)=(y(i)+y(i+1))/2; end I=h*sum(int); The user-defined function IntegrationMidpoint is used in the following program written in a script file to solve Problem 7.9. clear all; clc; a=0; b=1; N=9; h=(b-a)/N; x=a:h:b; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 y=1/64*(2*cos(x)+5*cos(3.*x)).^2.*sin(x); I = IntegrationMidpoint(x,y) When the script is executed, the value of the integral I is displayed in the Command Window: I = 0.0718 Therefore, the value of the integral calculated using the composite midpoint method is I = 0.0718 . Compare this to a value of I = 0.0721 obtained using Simpson’s 3/8 method. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.15 Write a user-defined MATLAB function for integration of a function with the composite trapezoidal method. For function name and arguments use I=Compzoidal(‘FunName’,a,b). ‘FunName’ is a string with the name of a function file that calculates the value of the function to be integrated for a given value of x. a and b are the limits of integration, and I is the value of the integral. The integration function calculates the value of the integral in iterations. In the first iteration the interval > a b @ is divided into two subintervals. In every iteration that follows the number of subintervals is doubled. The iterations stop when the difference in the value of the integral between two successive iterations is smaller than 1%. Use the function to solve Problems 7.4 and 7.5. Solution The listing of the user-defined MATLAB function Compzoidal is: function I = Compzoidal(FunName,a,b) % Numerically integrates a given function on [a,b] using the composite % trapezoidal method. The integral is first calculated using two subintervals. % Then, the process is iterated, doubling the N each time, until the difference % of I between successive iterations is <1%. % Input variables: % FunName The function to be integrated (exists separately as a function % file) % a Lower limit of integration % b Upper limit of integration % Output variables: % I Value of integral % Calculating the first value of I N=2; h=(b-a)/N; x=a:h:b; y=FunName(x); I=h/2*(y(1)+y(N+1))+h*sum(y(2:N)); N=2*N; check=0; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % Calculating subsequent values of I while check==0; h=(b-a)/N; x=a:h:b; y=FunName(x); % Composite trapezoidal method Eq. (7.13) I_new=h/2*(y(1)+y(N+1))+h*sum(y(2:N)); % Compare solution with that calculated in the previous iteration error=abs(I-I_new)/I*100; % (*) if error>1.0 % continue iteration check=0; N=N*2; I=I_new; elseif error<=1.0 % end iteration check=1; I=I_new; end end In Problem 7.4, the function f x = sin2 x is integrated over the domains > 0 S @ and > 0 2S @ . The userdefined function Compzoidal is used in the Command Window to perform these integrations: >> IProb7_4a = Compzoidal(@Prob7_4,0,pi) IProb7_4a = 1.570796326794897 >> IProb7_4a = Compzoidal(@Prob7_4,0,2*pi) IProb7_4a = 3.141592653589793 The exact values of the integrals are I 1 = S--- and I 2 = S , respectively, so the error in each case is very 2 small. In Problem 7.5, the function f x = 2 1 – x is integrated over the domain > – 1 1 @ . The user-defined func- Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 tion is used in the Command Window to perform this integration: >> IProb7_5 = Compzoidal(@Prob7_5,-1,1) IProb7_5 = 1.567551211438566 The exact value of this integral is I = S--- . 2 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.16 Write a user-defined MATLAB function for integration of a function with the composite Simpson’s 1/3 method. For function name and arguments use I=Simpson13(‘FunName’,a,b). ‘FunName’ is a string with the name of a function file that calculates the value of the function to be integrated for a given value of x. a and b are the limits of integration, and I is the value of the integral. The integration function calculates the value of the integral in iterations. In the first iteration the interval > a b @ is divided into two subintervals. In every iteration that follows, the number of subintervals is doubled. The iterations stop when the difference in the value of the integral between two successive iterations is smaller than 1%. Use the function to solve Problems 7.4 and 7.5. Solution The listing of the user-defined function Simpson13 is: function I = Simpson13(FunName,a,b) % Numerically integrates a given function on [a,b] using the composite % Simpson's 1/3 method. The integral is first calculated using two subintervals. % Then, the process is iterated, doubling the N each time, until the difference % of I between successive iterations is <1%. % Input variables: % FunName The function to be integrated (exists separately as a function % file) % a Lower limit of integration % b Upper limit of integration % Output variables: % I Value of integral % Calculating the first value of I N=2; h=(b-a)/N; x=a:h:b; y=FunName(x); I=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1)); N=2*N; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 check=0; % Calculating subsequent values of I while check==0; h=(b-a)/N; x=a:h:b; y=FunName(x); % Composite Simpson's 1/3 method, Eq. (7.19) I_new=h/3*(y(1)+4*sum(y(2:2:N))+2*sum(y(3:2:(N-1)))+y(N+1)); % Compare solution with that calculated in the previous iteration error=abs(I-I_new)/I*100; % (*) if error>1.0 % continue iteration check=0; N=N*2; I=I_new; elseif error<=1.0 % end iteration check=1; I=I_new; end end In Problem 7.4, the function f x = sin2 x is integrated over the domains > 0 S @ and > 0 2S @ . The userdefined function Simpson13 is used in the Command Window to perform these integrations: >> IProb7_4a = Simpson13(@Prob7_4,0,pi) IProb7_4a = 1.570796326794897 >> IProb7_4b = Simpson13(@Prob7_4,0,2*pi) IProb7_4b = 3.141592653589792 The exact values of the integrals are I 1 = S--- and I 2 = S , respectively, so the error in each case is very 2 small. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 In Problem 7.5, the function f x = 1 – x 2 is integrated over the domain > – 1 1 @ . The user-defined function Simpson13 is used in the Command Window to perform this integration: >> IProb7_5 = Simpson13(@Prob7_5,-1,1) IProb7_5 = 1.567198834492299 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.17 Write a user-defined MATLAB function for integration of a function with the composite Simpson’s 3/8 method. For function name and arguments use I=Simpsons38(‘FunName’,a,b). ‘FunName’ is a string with the name of a function file that calculates the value of the function to be integrated for a given value of x. a and b are the limits of integration, and I is the value of the integral. The integration function calculates the value of the integral in iterations. In the first iteration the interval > a b @ is divided into three subintervals. In every iteration that follows the number of subintervals is doubled. The iterations stop when the difference in the value of the integral between two successive iterations is smaller than 1%. Use the function to solve Problems 7.4 and 7.5. Solution The listing of the user-defined function Simpson13 is: function I = Simpson38(FunName,a,b) % Numerically integrates a given function on [a,b] using the composite % % % % Simpson's 3/8 method. The integral is first calculated using three subintervals. Then, the process is iterated, doubling the N each time, until the difference of I between successive iterations is <1%. Input variables: % FunName The function to be integrated (exists separately as a function % file) % a Lower limit of integration % b Upper limit of integration % Output variables: % I Value of integral % Calculating the first value of I N=3; h=(b-a)/N; x=a:h:b; y=FunName(x); I=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); N=2*N; check=0; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % Calculating subsequent values of I while check==0; h=(b-a)/N; x=a:h:b; y=FunName(x); % Composite Simpson's 3/8 method, Eq. (7.22) I_new=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); I_new=I_new+3*h/8*3*(sum(y(2:3:(N-1)))+sum(y(3:3:N))); % Compare solution with that calculated in the previous iteration error=abs(I-I_new)/I*100; % (*) if error>1.0 % continue iteration check=0; N=N*2; I=I_new; elseif error<=1.0 % end iteration check=1; I=I_new; end end In Problem 7.4, the function f x = sin2 x is integrated over the domains > 0 S @ and > 0 2S @ . The userdefined function Simpson38 is used in the Command Window to perform these integrations: >> IProb7_4a = Simpson38(@Prob7_4,0,pi) IProb7_4a = 1.570796326794897 >> IProb7_4b = Simpson38(@Prob7_4,0,2*pi) IProb7_4b = 3.141592653589794 The exact values of the integrals are I 1 = S--- and I 2 = S , respectively, so the error in each case is very 2 small. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 In Problem 7.5, the function f x = 1 – x 2 is integrated over the domain > – 1 1 @ . The user-defined function Simpson38 is used in the Command Window to perform this integration: >> IProb7_5 = Simpson38(@Prob7_5,-1,1) IProb7_5 = 1.564041575415192 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.18 ( ³ 1 Write a user-defined MATLAB function for integration of a function f x in the domain > – 1 1 @ f x dx ) with fourth-order Gauss quadrature. For function name and arguments use –1 I=GaussQuad4(‘FunName’), where ‘FunName’ is a string with the name of a function file that calculates the value of the function to be integrated. Use the function to evaluate the integral in Problem 7-5. Solution The listing of the user-defined function GaussQuad4 is: function I = GaussQuad4(FunName) % Numerically integrates a given function on [-1,1] using the 4th-order % Gaussian quadrature. % Input variables: % FunName The function to be integrated (exists separately as a function % file). % Output variables: % I Value of integral % Coefficients and Gauss points from Table 7-1 C1=0.3478548; C2=0.6521452; C3=C2; C4=C1; x1=-0.86113631; F_x1=feval(FunName,x1); x2=-0.33998104; F_x2=feval(FunName,x2); x3=-x2; F_x3=feval(FunName,x3); x4=-x1; F_x4=feval(FunName,x4); % Gauss quadrature, Eq.(7.24) I = C1*F_x1+C2*F_x2+C3*F_x3+C4*F_x4; To solve the problem, a user-defined function, named Prob7_5, that calculates the value of the function f x = 1 – x 2 (the function in Problem 7-5) is created: function y = Prob7_5(x) y=sqrt(1-x^2); Next, the user-defined function GaussQuad4 is used in the Command Window for calculating the integral: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 >> I = GaussQuad4('Prob7_5') I = 1.5803 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.19 ( ³ b a Write a user-defined MATLAB function for integration of a function f x in the domain > a b @ f x dx ) with fourth-order Gauss quadrature. For function name and arguments use I=GaussQuad4ab(‘FunName’,a,b), where ‘FunName’ is a string with the name of a function file that calculates the value of the function to be integrated, and a and b are the limits of integration. Use the function to evaluate the integral in Example 7-2. Solution The listing of the user-defined function GaussQuad4 is: function I = GaussQuad4ab(FunName,a,b) % Numerically integrates a given function on [a,b] using the 4th-order % Gaussian quadrature. % Input variables: % FunName The function to be integrated (exists separately as a function % file) % a Lower limit of integration % b Upper limit of integration % Output variables: % I Value of integral % Coefficients and Gauss points from Table 7-1 C1=0.3478548; C2=0.6521452; C3=C2; C4=C1; t1=-0.86113631; F_x1=feval(FunName,((b-a)*t1+a+b)/2); t2=-0.33998104; F_x2=feval(FunName,((b-a)*t2+a+b)/2); t3=-t2; F_x3=feval(FunName,((b-a)*t3+a+b)/2); t4=-t1; F_x4=feval(FunName,((b-a)*t4+a+b)/2); % Gauss quadrature, Eq.(7.24) I = C1*F_x1+C2*F_x2+C3*F_x3+C4*F_x4; I=I*(b-a)/2; To solve the problem, a user-defined function, named Example7_2, that calculates the value of the function f x = e –x2 (the function in Example 7-2) is created: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 function y = Example7_2(x) y=exp(-x^2); Next, the user-defined function GaussQuad4ab is used in the Command Window for calculating the integral: >> I = GaussQuad4ab('Example7_2',0,3) I = 0.8841 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.20 The density, U, of the Earth varies with the radius, r. The following table gives the approximate density at different radii: r (km) U (kg/m3) 0 800 1200 1400 2000 3000 3400 13000 12900 12700 12000 11650 10600 9900 3600 5500 4000 5300 5000 4750 5500 4500 6370 3300 The mass of the Earth can be calculated by: m = ³ 6370 U4Sr 2 dr 0 (a) Write a user-defined MATLAB function for integration with the composite trapezoidal method of tabulated data with general spacing (the spacing between the points does not have to be the same). For function name and arguments, use I=TrapTabulated(x,y), where the input arguments x and y are vectors with the data points. To carry out the integration, use Eq. (7.12). Use the function to calculate the mass of the Earth using the data given in the table. (b) Use MATLAB’s built-in trapz function to calculate the mass of the Earth using the data given in the table. (c) Use MATLAB’s built-in interp1 function (with the spline option for the interpolation method) to generate a new interpolated data set from the data that is given in the table. For spacing divide the domain > 0 6370 @ into 50 equal subintervals (use the linspace command). Calculate the mass of the Earth by integrating the interpolated data set with MATLAB’s built-in trapz function. Solution (a) The listing of the user-defined function TrapTabulated is: function I = TrapTabulated(x,y) % Numerically integrates a function given by tabulated data points % using the composite trapezoidal method. % Input variables: % x A vector with the x coordinate of the points. % y A vector with the y coordinate of the points. % Output variables: % I Value of integral. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 N=length(x)-1; S=0; for i=1:N S=S+(y(i)+y(i+1))*(x(i+1)-x(i)); end I=S/2; The mass of the earth is calculated in the following script file by using the data given in the problem, and the user-defined function TrapTabulated. % Solution of Problem 7.20(a) clear all r=[0 800 1200 1400 2000 3000 3400 3600 4000 5000 5500 6370]*1000; rho=[13 12.9 12.7 12 11.65 10.6 9.9 5.5 5.3 4.75 4.5 3.3]*1000; rhor2=rho.*r.^2; I=4*pi*TrapTabulated(r,rhor2) When the script file is executed, the following result is displayed in the Command Window. >> I = 6.0261e+024 (b) The mass of the earth is calculated in the following script file by using the data given in the problem, and MATLAB’s built-in function trapz. % Solution of Problem 7.20(b) clear all r=[0 800 1200 1400 2000 3000 3400 3600 4000 5000 5500 6370]*1000; rho=[13 12.9 12.7 12 11.65 10.6 9.9 5.5 5.3 4.75 4.5 3.3]*1000; rhor2=rho.*r.^2; I=4*pi*trapz(r,rhor2) When the script file is executed, the following result is displayed in the Command Window. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 >> I = 6.0261e+024 (c) The mass of the earth is calculated in the following script file by using the data given in the problem, and MATLAB’s built-in function trapz. % Solution of Problem 7.20(c) clear all r=[0 800 1200 1400 2000 3000 3400 3600 4000 5000 5500 6370]*1000; rho=[13 12.9 12.7 12 11.65 10.6 9.9 5.5 5.3 4.75 4.5 3.3]*1000; rnew=linspace(0,6370000,51); rhonew=interp1(r,rho,rnew,'spline'); rhor2=rhonew.*rnew.^2; I=4*pi*trapz(rnew,rhor2) When the script file is executed, the following result is displayed in the Command Window. >> I = 6.2331e+024 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.21 The perimeter, P, of an ellipse is given by: P = 4a ³ Se2 1 – k 2 sin2 T dT 0 2 2 a –b -. where k = -------------------a Write a user-defined MATLAB function that calculates the perimeter of an ellipse. For function name and arguments use P=EllipsePer(a,b), where a and b are the major and minor axes, respectively, and P is the perimeter. Use the function to calculate the perimeter of ellipses given by: (a) x2 y2 ----- + ----- = 1 2 2 5 2 (b) x2 y2 ----- + ----- = 1 2 2 4 7 Solution The listing of the user-defined function EllipsePer is: function I = EllipsePer(a,b) k=sqrt(a^2-b^2)/a; % The integral is calculated with the user-defined function Simpson13 % that was written in Problem 7.17. I = 4*a*Simpson38(@HW_7_21,0,pi/2); function y=HW_7_21(x) y=sqrt(1-k^2*(sin(x)).^2); end end (a) The user-defined function is used in the Command Window: >> I = EllipsePer(5,2) I = 23.0132 (b) The user-defined function is used in the Command Window: >> I = EllipsePer(4,7) I = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 35.2032 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.22 A cross-sectional area has the geometry of half an ellipse, as shown in the figure to the right. The coordinate x of the centroid of the area can be calculated by: M x = -------y A x2 y = b 1 – ----a2 1 where A is the area given by A = --- Sab , and M y is the 2 moment of the area about the y axis, given by: My = ³ x c dA = 2b A ³ a x2 x 1 – ----- dx a2 0 (a) Write a MATLAB program in a script file that calculates x 2y = – b 1 – ---x when a = 40 mm and b = 15 mm. For the integraa2 tion first use the user-defined function Compzoidal that was created in Problem 7.15. (b) Replace the integration function in the program from part (a) with one of MATLAB’s built-in integration functions. Repeat the calculation, and compare the results. Solution (a) The following MATLAB program in a script uses the user-defined function Compzoidal (listed in Problem 7.15) to solve the problem: % Solution of Problem 7.22a clear all a=40; b=15; A=pi*a*b/2; My=2*b*Compzoidal(@Prob7_22FUN,0,a); xcent=My/A 2 x -: The user-defined function Prob7_22FUN calculates the value of the function f x = x 1 – ---2 a function y = Prob7_22FUN(x) y=x.*sqrt(1-(x.^2/40^2)); When the script file is executed, the following result is displayed in the Command Window. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 xcent = 16.8901 (b) The following MATLAB program in a script uses the MATLAB’s built-in function quad to solve the problem: % Solution of Problem 7.22b clear all a=40; b=15; A=pi*a*b/2; My=2*b*quad(@Prob7_22FUN,0,a); xcent=My/A When the script file is executed, the following result is displayed in the Command Window. xcent = 16.9765 The result from part (b) is not exactly the same as the result from part (a). A better agreement can be obtained if a smaller value for the error in the user-defined function Compzoidal is used. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.23 A cylindrical vertical tank of diameter D = 2 ft has a hole of diameter d = 2 in. near its bottom. Water enters the tank through a pipe at the top at a rate of Q = 0.15 ft3/s. The time, t, that is required for the height of the water level in the tank to change from its initial ( t = 0 ) level of h 0 = 9 ft to level of h is given by: t = ³ h h0 1 ------------------------------------------- dy 2 4Q d ---------- – -----2- 2gy SD 2 D where g = 32.2 ft/s2. (a) Use the composite trapezoidal method (user-defined function created in Problem 7.15) to determine how long it would take for the height of the water level to change to h = 5 ft. (b) Determine the times that correspond to water levels from h = 9 ft to h = 1 ft in increments of 0.2 ft, and plot h as a function of t. To carry out the calculations and make the plot, write a MATLAB program in a script file. For integration use one of MATLAB’s built-in functions. Solution (a) The following program (script file) solves the problem: % Solution of Problem 7.23a clear all h0=9; h=5; t=Compzoidal(@Prob7_23FUN,h0,h) The listing of the user-defined function Prob7_23FUN is: function F = Prob7_23FUN(y) D=2; d=2/12; Q=0.15; g=32.2; F=1./((4*Q/(pi*D^2))-(d^2/D^2)*sqrt(2*g*y)); When the script file is executed, the following result is displayed in the Command Window: t = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 41.0884 (b) The following program (script file) solves the problem: % Solution of Problem 7.23b clear all h0=9; h=9:-0.2:1; n=length(h); for i=1:n t(i)=quadl(@Prob7_23FUN,h0,h(i)); end plot(t,h) xlabel('Time (s)') ylabel('h (ft)') The listing of the user-defined function Prob7_23FUN is the same as in Part (a). When the script file is executed, the following plot displayed in the Figure Window: 9 8 7 h (ft) 6 5 4 3 2 1 0 20 40 60 80 Time (s) 100 120 140 160 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.24 The moment of inertia, I y , about the y axis of the cross-sectional area of the half ellipse that is shown in Problem 7.22 is given by: Iy = ³ x c2 dA = 2b A ³ a x 2- dx x 2 1 – ---a2 0 Calculate I y when a = 40 mm and b = 15 mm. For the integration use one of MATLAB’s built-in integration functions. Solution The following program (script file) solves the problem. % Solution of Problem 7.24 clear all a=40; b=15; Iy=2*b*quad(@Prob7_24FUN,0,a) 2 x - . Its listing is: The user-defined function Prob7_24FUN calculates the function f x = x 2 1 – ---2 a function y = Prob7_24FUN(x) y=x.^2.*sqrt(1-(x.^2/40^2)); When the script file is executed, the following result is displayed in the Command Window: Iy = 3.7699e+005 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.25 The moment of inertia, I x , about the x axis of the cross-sectional area of the half ellipse that is shown in Problem 7.22 is given by: Ix = ³ a 0 3 1 x2 · ------ § 2b 1 – ---dx 12 © a2 ¹ Calculate I x when a = 40 mm and b = 15 mm. For the integration use one of MATLAB’s built-in integration functions. Solution The following program (script file) solves the problem. % Solution of Problem 7.25 (Script file) clear all a=40; b=15; Ix=(2*b)^3/12*quad('Prob7_25FUN',0,a) 2 3 x · - . Its listing is: The user-defined function Prob7_25FUN calculates the function f x = §© 1 – ---a 2¹ function y = Prob7_25FUN(x) a=40; y=(sqrt(1-(x.^2/a^2))).^3; When the script file is executed, the following result is displayed in the Command Window: Ix = 5.3014e+004 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.26 An aluminum beam with a length, L = 2 m, is clamped at one end and is loaded by an axial force P = 20 kN at the other end. The cross-sectional area of the beam varies with the length of the beam and is given as a function of x by: W x x H A = ----- § 2 – --- · ---- § 2 – --- · © ¹ 2© 2 L¹ L where W = 80 mm and H = 30 mm. The normal strain, H , along the beam is given by: P du H = ------ = ------EA dx where E = 70 GPa is the modulus of elasticity of the beam. The displacement G at a point x = x P along the beam can be calculated by: G = ³ xP 0 P ------- dx EA (a) Determine the displacement G at the endpoint of the beam ( x P = L ). Calculate the displacement twice, once by using the user-defined function from Problem 7.16, and once by using one of MATLAB’s built-in integration functions. (b) Write a MATLAB program in a script file that calculates, and makes a plot, of the displacement G as a function of x. For the integration use one of MATLAB’s built-in integration functions. Solution Substituting the expression for A into the equation for G gives: 4P G = ------------EWH ³ xP 0 1 --------------------- dx 2 § 2 – --x- · © ¹ L (a) The following MATLAB program in a script uses the user-defined function Simpson13 (listed in Problem 7.16) to solve the problem: % Solution of Problem 7.26a (Script file) clear all P=20e3; W=0.08; H=0.03; E=70e9; delta=4*P/(E*W*H)*Simpson13(@Prob7_26FUN,0,2) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 1 The user-defined function Prob7_26FUN calculates the function f x = --------------------. Its listing is: 2 § 2 – --x- · © L¹ function y = Prob7_26FUN(x) L=2; y=1./(2-x/L).^2; When the script file is executed, the following result is displayed in the Command Window: delta = 4.7659e-004 (b) The following MATLAB program in a script uses MATLAB’s built-in function quad to solve the problem: % Solution of Problem 7.26b (Script file) clear all P=20e3; W=0.08; H=0.03; E=70e9; delta=4*P/(E*W*H)*quad(@Prob7_26FUN,0,2) When the script file is executed, the following result is displayed in the Command Window: delta = 4.7619e-004 The result from part (b) is not exactly the same as the result from part (a). A better agreement can be obtained if a smaller value for the error in the user-defined function Simpson13 is used. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.27 In the design of underground pipes, there is a need to estimate the temperature of the ground. The temperature of the ground at various depths can be estimated by modeling the ground as a semi-infinite solid initially at constant temperature. The temperature at depth, x, and time, t, can be calculated from the expression: T x t – T S 2 x --------------------------- = erf § ------------- · = ------© 2 Dt ¹ Ti – TS S ³ x ------------2 Dt e – u 2 du 0 where T S is the surface temperature, T i is the initial soil temperature, and D = 0.138 u 10 –6 m2/s is the thermal diffusivity of the soil. Answer the following questions taking T S = – 15 oC and T i = 12 oC. (a) Find the temperature at a depth x = 1 m after 30 days ( t = 2.592 u 10 6 s). (b) Write a MATLAB program in a script file that generates a plot that shows the temperature as a function of time at a depth of x = 0.5 m for 40 days. Use increments of 1 day. (c) Write a MATLAB program that generates a three-dimensional plot (T vs. x and t) showing the temperature as a function of depth and time for 0 d x d 3 m and 0 d t d 2.592 u 10 7 s. Solution (a) The following MATLAB program in a script uses MATLAB’s built-in function quad to solve the problem: % Solution of Problem 7.27a (Script file) clear all Ts=-15; Ti=12; alpha=0.138e-6; x=1; t=2.592e6; b=x/(2*sqrt(alpha*t)); T=Ts+(Ti-Ts)*2/pi*quad('exp(-x.^2)',0,b) When the script file is executed, the following result is displayed in the Command Window: T = -3.3784 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 (b) The following MATLAB program in a script uses MATLAB’s built-in function quad to solve the problem: % Solution of Problem 7.27b (Script file) clear all Ts=-15; Ti=12; alpha=0.138e-6; x=0.5; tdays=[0.01, 1:1:40]; %Starting with a value close to zero. tsec=tdays*24*60*60; b=x./(2*sqrt(alpha*tsec)); n=length(tsec); for i=1:n T(i)=Ts+(Ti-Ts)*2/sqrt(pi)*quad('exp(-x.^2)',0,b(i)); end plot(tdays,T) xlabel('Time (day)'), ylabel('Temperature (C)') When the script file is executed, the following figure is displayed in the Figure Window: 14 12 Temperature (C) 10 8 6 4 2 0 −2 −4 −6 0 10 20 Time (day) 30 40 (c) The following MATLAB program in a script uses MATLAB’s built-in function quad to solve the problem: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 % Solution of Problem 7.27c (Script file) clear all Ts=-15; Ti=12; alpha=0.138e-6; x=[0.01 0.2:0.2:3]; %Starting with a value close to zero. m=length(x); tdays=[0.01, 1:1:30]; %Starting with a value close to zero. tsec=tdays*24*60*60; n=length(tsec); [X,Y]=meshgrid(tdays,x); for j=1:m xa=x(j); b=xa./(2*sqrt(alpha*tsec)); for i=1:n T(j,i)=Ts+(Ti-Ts)*2/sqrt(pi)*quadl('exp(-xa.^2)',0,b(i)); end end %plot(tdays,T(j,:)) mesh(Y,X,T) ylabel('Time (day)'), xlabel('Depth (m)') zlabel('Temperature (C)') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 When the script file is executed, the following figure is displayed in the Figure Window: 15 Temperature (C) 10 5 0 −5 −10 −15 30 20 Time (day) 3 2 10 1 0 Depth (m) 0 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 7.28 A thermocouple is used to measure the temperature of a flowing gas in a duct. The time-dependence of the temperature of the spherical junction of a thermocouple is given by the implicit integral equation: t = – ³ T Ti UVC ----------------------------------------------------------------------------------- dT 4 A S > h T – T f + HV SB T 4 – T surr @ where T is the temperature of the thermocouple junction at time t, U is the density of the junction material, V is the volume of the spherical junction, C is the heat capacity of the junction, A S is the surface area of the junction, h is the convection heat transfer coefficient, T f is the temperature of the flowing gas, H is the emissivity of the junction material, V SB is the Stefan–Boltzmann constant, and T surr is the temperature of the surrounding duct wall. For T i = 298 K, H = 0.9 , U = 8500 kg/m3, C = 400 J/kg/K, T f = 473 K, h = 400 W/m2/K, m3, A S = 1.0 u 10 –6 m2, V SB = 5.67 u 10 –8 W/(m2K4), and T surr = 673 K, determine the time, t, it takes for the thermocouple junction temperature, T, to increase to 490 K, using Romberg integration. V = 5.0 u 10 – 10 Solution The problem is solved by using the user-defined function Romberg (Program 7-2 in Section 7.10). The UVC value of the integrand ----------------------------------------------------------------------------------is calculated in a user-defined function named 4 4 A S > h T – T f + HV SB T – T surr @ Prob7_28FUN, which is listed below. The numerical integration inside the user-defined function Romberg is carried out by a user defined function named trapezoidalFun (listed below), which is a slightly modified version of the user-defined function trapezoidal (Program 7-1in Section 7.3). The following program (script file) solves the problem. It uses fifth level Romberg integration with 4 initial subintervals. % Solution of Problem 7.28 (Script file) clear all Ti=298; T=490; t = -Romberg(@Prob7_28FUN,Ti,T,4,5) When the program is executed the following result is displayed in the Command Window: t = 63.8960 38.4088 29.9131 22.8673 22.3976 19.9690 19.9305 19.2273 19.2245 19.0818 19.0816 0 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 26.7527 21.8008 19.9221 19.3040 20.1502 19.2958 19.0980 0 19.2389 19.0848 0 0 19.0823 0 0 0 0 0 0 0 0 0 0 0 The answer is 19.0816 second. The following is listing of the user-defined functions that are used in the script file: function IR = Romberg(integrand,a,b,Ni,Levels) % Romberg numerically integrate using the Romberg integration Method. % Input Variables: % integrand The function to be integrated. % a Lower limit of integration. % b Upper limit of integration. % Ni Initial number of subintervals. % Levels Number of levels of Romberg integration. % Output Variable: % IR A matrix with the estimated values of the integral. % Creating the first column with the composite Trapezoidal method: for i=1:Levels+1 Nsubinter=Ni*2^(i-1); IR(i,1)=trapezoidalFun(integrand,a,b,Nsubinter); end % Calculating the extrapolated values using Eq. (7.65): for j=2:Levels+1 for i=1:(Levels-j+2) IR(i,j)=(4^(j-1)*IR(i+1,j-1)-IR(i,j-1))/(4^(j-1)-1); end end function I = trapezoidalFun(FunName,a,b,N) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 % trapezoidal numerically integrate using the Composite Trapezoidal Method. % Input Variables: % integrand The function to be integrated (name of a user-defined function). % a Lower limit of integration. % b Upper limit of integration. % n Number of subintervals. % Output Variable: % I Value of the integral. h = (b-a)/N; x=a:h:b; for i=1:N+1 F(i)=FunName(x(i)); end I=h*(F(1)+F(N+1))/2+h*sum(F(2:N)); function y = Prob7_28FUN(T) ro=8500; V=5E-10; C=400; As=1E-6; h=400; Ep=0.9; SB=5.67E-8; Tsur=673; Tinf=473; y=ro*V*C./(As*(h*(T-Tinf)+Ep*SB*(T.^4-Tsur^4))); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.1 Consider the following first-order ODE: dy ------ = yx – x 3 dx from x = 0 to x = 1.8 with y 0 = 1 (a) Solve by hand with Euler’s explicit method using h = 0.6 . (b) Solve by hand with the modified Euler method using h = 0.6 . (c) Solve by hand with the classical fourth-order Runge–Kutta method using h = 0.6 . 1 --- x 2 The analytical solution of the ODE is: y = x 2 – e 2 + 2 . In each part, calculate the error between the true solution and the numerical solution at the points where the numerical solution is determined. Solution (a) Using Eqs. (8.14) and (8.15) for Euler’s explicit method with h = 0.6 . x i + 1 = x i + h = x i + 0.6 y i + 1 = y i + f x i y i h = y i + y i x i – x i3 0.6 1 2 --- x i 2 x i2 Error: E i = – e + 2 – y i First step: For the first step i = 1 , x 1 = 0 and y 1 = 1. x 2 = 0 + 0.6 = 0.6 y 2 = y 1 + y 1 x 1 + x 13 0.6 = 1 + 1 0 – 0 3 0.6 = 1 2 E 2 = 0.6 – e 1 2 --- 0.6 2 + 2 – 1 = 0.1628 Second step: For the second step i = 2 . x 3 = 0.6 + 0.6 = 1.2 y 3 = y 2 + y 2 x 2 + x 23 0.6 = 1 + 1 0.6 – 0.6 3 0.6 = 1.2304 2 E 3 = 1.2 – e 1 2 --- 1.2 2 + 2 – 1.2304 = 0.1552 Third step: For the third step i = 3 . x 4 = 1.2 + 0.6 = 1.8 y 4 = y 3 + y 3 x 3 + x 33 0.6 = 1.2304 + 1.2304 1.2 – 1.2 3 0.6 = 1.0795 2 E 4 = 1.8 – e 1 2 --- 1.8 2 + 2 – 1.0795 = – 0.8926 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The problem is also solved with the following script file: % Chapter 8 Problem 1 Part (a) dF=inline('y*x-x^3'); h=0.6; N=3; x(1) = 0; y(1) = 1; for i = 1:N x(i+1) = x(i) + h; y(i+1) = y(i) + dF(x(i),y(i))*h; end x y error=x.^2-exp(0.5*x.^2)+2-y When the program is executed, the following results are displayed in the Command Window: x = 0 0.6000 1.2000 1.8000 1.0000 error = 0 1.0000 1.2304 1.0795 0.1628 0.1552 -0.8926 y = (b) Using Eq. (8.56) for the modified Euler method with h = 0.6 . x i + 1 = x i + h = x i + 0.6 3 y iEU + 1 = y i + f x i y i h = y i + y i x i – x i 0.6 3 EU 3 y i + 1 = y i + 0.5 > f x i y i + f x i + 1 y iEU + 1 @h = y i + 0.5 > y i x i – x i + y i + 1 x i + 1 – x i + 1 @h 1 2 --- x i 2 Error: E i = x i2 – e + 2 – y i First step: For the first step i = 1 , x 1 = 0 and y 1 = 1. x 2 = 0 + 0.6 = 0.6 y 2EU = y 1 + y 1 x 1 + x 13 0.6 = 1 + 1 0 – 0 3 0.6 = 1 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 y 2 = y 1 + 0.5 > y 1 x 1 + x 13 + y 2EU x 2 + x 23 @0.6 = 1 + 0.5 > 1 0 – 0 3 + 1 0.6 – 0.6 3 @0.6 = 1.1152 2 E 2 = 0.6 – e 1 2 --- 0.6 2 + 2 – 1.1152 = 0.0476 Second step: For the second step i = 2 . x 3 = 0.6 + 0.6 = 1.2 y 3EU = y 2 + y 2 x 2 + x 23 0.6 = 1.1152 + 1.1152 0.6 – 0.6 3 0.6 = 1.3871 y 3 = y 2 + 0.5 > y 2 x 2 + x 23 + y 3EU x 3 + x 33 @0.6 = 1.1152 + 0.5 > 1.1152 0.6 – 0.6 3 + 1.3871 1.2 – 1.2 3 @0.6 = 1.2321 2 E 3 = 1.2 – e 1 2 --- 1.2 2 + 2 – 1.2321 = 0.1535 Third step: For the third step i = 3 . x 4 = 1.2 + 0.6 = 1.8 y 4EU = y 3 + y 3 x 3 + x 33 0.6 = 1.2321 + 1.2321 1.2 – 1.2 3 0.6 = 1.0824 y 4 = y 3 + 0.5 > y 3 x 3 + x 33 + y 4EU x 4 + x 43 @0.6 = 1.2321 + 0.5 > 1.2321 1.2 – 1.2 3 + 1.0824 1.8 – 1.8 3 @0.6 = – 0.00785 2 E 4 = 1.8 – e 1 2 --- 1.8 2 + 2 – – 0.00785 = 0.1948 The problem is also solved with the following script file: % Chapter 8 Problem 1 Part (b) dF=inline('y*x-x^3'); h=0.6; N=3; x(1) = 0; y(1) = 1; for i = 1:N x(i+1) = x(i) + h; yEU=y(i) + dF(x(i),y(i))*h; y(i+1) = y(i) + (dF(x(i),y(i))+dF(x(i+1),yEU))*h/2; end x y error=x.^2-exp(0.5*x.^2)+2-y When the program is executed, the following results are displayed in the Command Window: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 x = 0 0.6000 1.2000 1.8000 1.0000 error = 0 1.1152 1.2321 -0.0079 0.0476 0.1535 0.1948 y = (c) Using Eqs. (8.86) and (8.87) for the classical fourth-order Runge–Kutta method with h = 0.6 . x i + 1 = x i + h = x i + 0.6 f x i y i = y i x i – x i3 K 1 = f x i y i 1 1 K 2 = f § x i + --- h y i + --- K 1 h· © ¹ 2 2 1 1 K 3 = f § x i + --- h y i + --- K 2 h· © ¹ 2 2 K 4 = f x i + h y i + K 3 h 1 y i + 1 = y i + --- K 1 + 2K 2 + 2K 3 + K 4 h 6 1 --- x i2 Error: E i = x i2 – e 2 + 2 – y i First step: For the first step i = 1 , x 1 = 0 and y 1 = 1. x 2 = 0 + 0.6 = 0.6 K 1 = y 1 x 1 – x 13 = 1 0 – 0 3 = 0 1 1 x 1 + --- h = 0 + --- 0.6 = 0.3 2 2 K 2 = 1 0.3 – 0.3 3 = 0.273 1 1 y 1 + --- K 1 h = 1 + --- 0 0.6 = 1 2 2 1 1 y 1 + --- K 2 h = 1 + --- 0.273 0.6 = 1.0819 2 2 K 3 = 1.0819 0.3 – 0.3 3 = 0.2976 y 1 + K 3 h = 1 + 0.2976 0.6 = 1.1786 K 4 = 1.1786 0.6 – 0.6 3 = 0.4912 1 1 y 2 = y 1 + --- K 1 + 2K 2 + 2K 3 + K 4 h = 1 + --- 0 + 2 0.273 + 2 0.2976 + 0.4912 0.6 = 1.1632 6 6 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 2 E 2 = 0.6 – e 1 2 --- 0.6 2 + 2 – 1.1632 = – 0.000417 Second step: For the first step i = 2 , x 2 = 0.6 and y 2 = 1.1632 . x 3 = 0.6 + 0.6 = 1.2 K 1 = y 2 x 2 – x 23 = 1.1632 0.6 – 0.6 3 = 0.49192 1 1 1 1 y 2 + --- K 1 h = 1.1632 + --- 0.49192 0.6 = 1.3108 x 2 + --- h = 0.6 + --- 0.6 = 0.9 2 2 2 2 K 2 = 1.3108 0.9 – 0.9 3 = 0.4507 1 1 y 2 + --- K 2 h = 1.1632 + --- 0.4507 0.6 = 1.298 2 2 K 3 = 1.298 0.9 – 0.9 3 = 0.4392 y 2 + K 3 h = 1.1632 + 0.4392 0.6 = 1.4267 K 4 = 1.4267 1.2 – 1.2 3 = – 0.01596 1 1 y 3 = y 2 + --- K 1 + 2K 2 + 2K 3 + K 4 h = 1.1632 + --- 0.49192 + 2 0.4507 + 2 0.4392 – 0.01596 0.6 = 1.389 6 6 2 E 3 = 1.2 – e 1 2 --- 1.2 2 + 2 – 1.389 = – 0.00343 Third step: For the first step i = 3 , x 3 = 1.2 and y 3 = 1.389 . x 4 = 1.2 + 0.6 = 1.8 K 1 = y 3 x 3 – x 33 = 1.389 1.2 – 1.2 3 = – 0.0612 1 1 1 1 y 3 + --- K 1 h = 1.389 + --- – 0.0612 0.6 = 1.3706 x 3 + --- h = 1.2 + --- 0.6 = 1.5 2 2 2 2 3 K 2 = 1.3706 1.5 – 1.5 = – 1.3191 1 1 y 3 + --- K 2 h = 1.389 + --- – 1.3191 0.6 = 0.9933 2 2 K 3 = 0.9933 1.5 – 1.5 3 = – 1.8851 y 3 + K 3 h = 1.389 + – 1.8851 0.6 = 0.2579 K 4 = 0.2579 1.8 – 1.8 3 = – 5.368 1 1 y 4 = y 3 + --- K 1 + 2K 2 + 2K 3 + K 4 h = 1.389 + --- – 0.0612 + 2 – 1.3191 + 2 – 1.8851 – 5.368 0.6 = 0.2052 6 6 2 E 4 = 1.8 – e 1 2 --- 1.8 2 + 2 –0.2052 = – 0.0183 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 6 The problem is also solved with the following script file: % Chapter 8 Problem 1 Part (c) dF=inline('y*x-x^3'); h=0.6; N=3; x(1) = 0; y(1) = 1; for i = 1:N x(i+1) = x(i) + h; K1= dF(x(i),y(i)); K2= dF(x(i)+0.5*h,y(i)+K1*h/2); K3= dF(x(i)+0.5*h,y(i)+K2*h/2); K4= dF(x(i+1),y(i)+K3*h); y(i+1) = y(i) + (K1+2*K2+2*K3+K4)*h/6; end x y error=x.^2-exp(0.5*x.^2)+2-y When the program is executed, the following results are displayed in the Command Window: x = 0 0.6000 1.2000 1.8000 1.0000 error = 0 1.1632 1.3872 0.2008 -0.0004 -0.0016 -0.0139 y = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.2 Consider the following first-order ODE: dy = x – y -----dx from x = 0 to x = 1.5 with y 0 = 1 (a) Solve by hand with Euler’s explicit method using h = 0.5 . (b) Solve by hand with the modified Euler method using h = 0.5 . (c) Solve by hand with the classical fourth-order Runge–Kutta method using h = 0.5 . The analytical solution of the ODE is: y = x + 2e –x – 1 . In each part, calculate the error between the true solution and the numerical solution at the points where the numerical solution is determined. Solution (a) The following script file solves the ODE with Euler’s explicit method using h = 0.5 . clear all % Chapter 8 Problem 2 Part (a) dF=inline('x-y'); h=0.5; N=3; x(1) = 0; y(1) = 1; for i = 1:N x(i+1) = x(i) + h; y(i+1) = y(i) + dF(x(i),y(i))*h; end x y error=x+2*exp(-x)-1-y When the program is executed, the following results are displayed in the Command Window: x = 0 0.5000 1.0000 1.5000 1.0000 error = 0 0.5000 0.5000 0.7500 0.2131 0.2358 0.1963 y = (b) The following script file solves the ODE with modified Euler method using h = 0.5 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 clear all % Chapter 8 Problem 2 Part (b) dF=inline('x-y'); h=0.5; N=3; x(1) = 0; y(1) = 1; for i = 1:N x(i+1) = x(i) + h; yEU=y(i) + dF(x(i),y(i))*h; y(i+1) = y(i) + (dF(x(i),y(i))+dF(x(i+1),yEU))*h/2; end x y error=x+2*exp(-x)-1-y When the program is executed, the following results are displayed in the Command Window: x = 0 0.5000 1.0000 1.5000 1.0000 error = 0 0.7500 0.7813 0.9883 -0.0369 -0.0455 -0.0420 y = (c) The following script file solves the ODE with classical fourth-order Runge–Kutta method using h = 0.5 . clear all % Chapter 8 Problem 2 Part (c) dF=inline('x-y'); h=0.5; N=3; x(1) = 0; y(1) = 1; for i = 1:N x(i+1) = x(i) + h; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 K1= dF(x(i),y(i)); K2= dF(x(i)+0.5*h,y(i)+K1*h/2); K3= dF(x(i)+0.5*h,y(i)+K2*h/2); K4= dF(x(i+1),y(i)+K3*h); y(i+1) = y(i) + (K1+2*K2+2*K3+K4)*h/6; end x y error=x+2*exp(-x)-1-y When the program is executed, the following results are displayed in the Command Window: x = 0 0.5000 1.0000 1.5000 1.0000 0.7135 error = 1.0e-003 * 0 -0.4803 0.7363 0.9468 -0.5828 -0.5303 y = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.3 Consider the following first-order ODE: dy ------ = y + t 3 dt from t = 0.5 to t = 2 with y 0.5 = – 1 (a) Solve by hand with Euler’s explicit method using h = 0.5 . (b) Solve by hand with the midpoint method using h = 0.5 . (c) Solve by hand with the classical fourth-order Runge–Kutta method using h = 0.5 . The analytical solution of the ODE is: y = – t 3 – 3t 2 – 6t – 6 + 71e t e 8e 0.5 . In each part, calculate the error between the true solution and the numerical solution at the points where the numerical solution is determined. Solution (a) The following script file solves the ODE with Euler’s explicit method using h = 0.5 . clear all % Chapter 8 Problem 3 Part (a) dF=inline('y+t^3'); h=0.5; N=3; t(1) = 0.5; y(1) = -1; for i = 1:N t(i+1) = t(i) + h; y(i+1) = y(i) + dF(t(i),y(i))*h; end t y error=-t.^3-3*t.^2-6*t-6+71/8*exp(t)/exp(1/2)-y When the program is executed, the following results are displayed in the Command Window: t = 0.5000 1.0000 1.5000 2.0000 -1.0000 error = 0 -1.4375 -1.6563 -0.7969 0.0699 0.6560 2.5719 y = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 (b) The following script file solves the ODE with modified Euler method using h = 0.5 . clear all % Chapter 8 Problem 3 Part (b) dF=inline('y+t^3'); h=0.5; N=3; t(1) = 0.5; y(1) = -1; for i = 1:N t(i+1) = t(i) + h; tm = t(i) + h/2; ym=y(i) + dF(t(i),y(i))*h/2; y(i+1) = y(i) + dF(tm,ym)*h; end t y error=-t.^3-3*t.^2-6*t-6+71/8*exp(t)/exp(1/2)-y When the program is executed, the following results are displayed in the Command Window: t = 0.5000 1.0000 1.5000 2.0000 -1.0000 error = 0 -1.3984 -1.1709 1.1989 0.0308 0.1706 0.5761 y = (c) The following script file solves the ODE with classical fourth-order Runge–Kutta method using h = 0.5 . clear all % Chapter 8 Problem 3 Part (c) dF=inline('y+t^3'); h=0.5; N=3; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 t(1) = 0.5; y(1) = -1; for i = 1:N t(i+1) = t(i) + h; K1= dF(t(i),y(i)); K2= dF(t(i)+0.5*h,y(i)+K1*h/2); K3= dF(t(i)+0.5*h,y(i)+K2*h/2); K4= dF(t(i+1),y(i)+K3*h); y(i+1) = y(i) + (K1+2*K2+2*K3+K4)*h/6; end t y error=-t.^3-3*t.^2-6*t-6+71/8*exp(t)/exp(1/2)-y When the program is executed, the following results are displayed in the Command Window: t = 0.5000 1.0000 1.5000 2.0000 -1.0000 error = 0 -1.3677 -1.0011 1.7711 0.0001 0.0009 0.0039 y = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 Consider the following system of two ODEs: 8.4 dy = y – x from t = 0 to t = 2 with x 0 = 1 , and y 0 = 3 . -----dt (a) Solve by hand with Euler’s explicit method using h = 0.5 . (b) Solve by hand with the modified Euler method using h = 0.5 . The analytical solution of the system is: x = e t > 3 sin t + cos t @ , y = e t > 3 cos t – sin t @ . In each part, dx = x + y -----dt calculate the error between the true solution and the numerical solution at the points where the numerical solution is determined. Solution (a) The following script file solves the system of the ODEs with Euler’s explicit method using h = 0.5 . clear all dxdt = inline('x+y'); dydt = inline('y-x'); x(1) = 1; y(1) = 3; t(1)=0; h = 0.5; N=4; for i = 1:N t(i+1) = t(i) + h; x(i+1) = x(i) + dxdt(x(i),y(i))*h; y(i+1) = y(i) + dydt(x(i),y(i))*h; end t x y Xerror=exp(t).*(3*sin(t)+cos(t))-x Yerror=exp(t).*(3*cos(t)-sin(t))-y When the program is executed, the following results are displayed in the Command Window: x = 1.0000 3.0000 6.5000 12.0000 19.7500 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 y = 3.0000 Xerror = 0 Yerror = 0 4.0000 4.5000 3.5000 -0.7500 0.8182 1.8308 1.7284 -2.6684 -0.4498 -2.3813 -7.0194 -15.1936 (b) The following script file solves the system of the ODEs with the modified Euler method using h = 0.5 . clear all dxdt = inline('x+y'); dydt = inline('y-x'); x(1) = 1; y(1) = 3; t(1)=0; h = 0.5; N=4; for i = 1:N t(i+1) = t(i) + h; xEU = x(i) + dxdt(x(i),y(i))*h; yEU = y(i) + dydt(x(i),y(i))*h; x(i+1) = x(i) + (dxdt(x(i),y(i))+dxdt(xEU,yEU))*h/2; y(i+1) = y(i) + (dydt(x(i),y(i))+dydt(xEU,yEU))*h/2; end t x y Xerror=exp(t).*(3*sin(t)+cos(t))-x Yerror=exp(t).*(3*cos(t)-sin(t))-y When the program is executed, the following results are displayed in the Command Window: t = 0 0.5000 1.0000 1.5000 2.0000 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 x = 1.0000 3.7500 8.4375 14.7656 20.5664 3.0000 Xerror = 0 Yerror = 0 3.7500 2.8125 -2.1094 -14.2383 0.0682 -0.1067 -1.0372 -3.4848 -0.1998 -0.6938 -1.4100 -1.7054 y = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 Consider the following system of two ODEs: 8.5 dx = xt – y -----dt dy = yt + x -----dt from t = 0 to t = 1.2 with x 0 = 1 , and y 0 = 0.5 . (a) Solve by hand with Euler’s explicit method using h = 0.4 . (b) Solve by hand with the classical fourth-order Runge–Kutta method using h = 0.4 . 1 2 --- t 1 2 --- t The analytical solution of the system is: x = e 2 §© cos t – --- sin t·¹ , y = §© – e 2 ·¹ §© – sin t – --- cos t·¹ . In each part, 2 2 calculate the error between the true solution and the numerical solution at the points where the numerical solution is determined. 1 1 Solution (a) The following script file solves the system of the ODEs with Euler’s explicit method using h = 0.4 . clear all dxdt = inline('x*t-y'); dydt = inline('y*t+x'); x(1) = 1; y(1) = 0.5; t(1)=0; h = 0.4; N=3; for i = 1:N t(i+1) = t(i) + h; x(i+1) = x(i) + dxdt(t(i),x(i),y(i))*h; y(i+1) = y(i) + dydt(t(i),x(i),y(i))*h; end t x y Xerror=exp(0.5*t.^2).*(cos(t)-0.5*sin(t))-x Yerror=-exp(0.5*t.^2).*(-sin(t)-0.5*cos(t))-y When the program is executed, the following results are displayed in the Command Window: t = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 0 0.4000 0.8000 1.2000 1.0000 0.8000 0.5680 0.2042 0.5000 Xerror = 0 Yerror = 0 0.9000 1.3640 2.0277 -0.0132 -0.1025 -0.4171 0.0207 0.1036 0.2594 x = y = (b) The following script file solves the system of the ODEs with the fourth-order Runge-Kutta method using h = 0.4 . clear all dxdt = inline('x*t-y'); dydt = inline('y*t+x'); x(1) = 1; y(1) = 0.5; t(1)=0; h = 0.4; N=3; for i = 1:N t(i+1) = t(i) + h; Kx1= dxdt(t(i),x(i),y(i)); Ky1= dydt(t(i),x(i),y(i)); Kx2= dxdt(t(i)+0.5*h,x(i)+Kx1*h/2,y(i)+Ky1*h/2); Ky2= dydt(t(i)+0.5*h,x(i)+Kx1*h/2,y(i)+Ky1*h/2); Kx3= dxdt(t(i)+0.5*h,x(i)+Kx2*h/2,y(i)+Ky2*h/2); Ky3= dydt(t(i)+0.5*h,x(i)+Kx2*h/2,y(i)+Ky2*h/2); Kx4= dxdt(t(i+1),x(i)+Kx3*h,y(i)+Ky3*h); Ky4= dydt(t(i+1),x(i)+Kx3*h,y(i)+Ky3*h); x(i+1) = x(i) + (Kx1+2*Kx2+2*Kx3+Kx4)*h/6; y(i+1) = y(i) + (Ky1+2*Ky2+2*Ky3+Ky4)*h/6; end Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 t x y Xerror=exp(0.5*t.^2).*(cos(t)-0.5*sin(t))-x Yerror=-exp(0.5*t.^2).*(-sin(t)-0.5*cos(t))-y When the program is executed, the following results are displayed in the Command Window: t = 0 0.4000 0.8000 1.2000 1.0000 0.7869 0.4654 -0.2134 0.5000 0.9206 Xerror = 1.0e-003 * 0 -0.0026 Yerror = 1.0e-003 * 0 0.0922 1.4675 2.2875 0.1099 0.3956 0.0760 -0.4700 x = y = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.6 Write the following second-order ODEs as a system of two first-order ODEs: 2 Q QL d y (a) EI --------2- = – Py + -------- x – ---- x 2 , where E, I, P, Q, and L are constants. 2 2 dx 2 2 3e2 dy d y (b) EI --------2- = M 1 + §© ------·¹ , where E, I, and M are constants. dx dx Solution 2 d y dw ------ , ------- = -------- . With these definitions, the system of two first-order ODEs is: (a) Define w = dy 2 dx dx dx dy = w -----dx 1 QL Q dw ------- = ------ § – Py + -------- x – ---- x 2· EI © 2 2 ¹ dx 2 dw d y ------ , ------- = -------- . With these definitions, the system of two first-order ODEs is: (b) Define w = dy 2 dx dx dx dy ------ = w dx M dw ------- = ------ > 1 + w 2 @ 3 e 2 EI dx Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.7 Write the following second-order ODEs as a system of two first-order ODEs: 2 0.008 dh 2 1d h T (a) --- --------2- = --- – 1 – ------------- § ------ · , where g, T, and w are constants. © ¹ g dt w w dt 2 dQ 250 dQ 3 Q + 500 Q --------- ------- + --------- § -------· + ---------------------------------(b) d---------= 1000 ------------ . 2 –6 © ¹ dt 15 dt 15 dt 15 4.2 u 10 15 Solution 2 -------h- . With these definitions, the system of two first-order ODEs is: (a) Define u = dh ------ , du ------ = d 2 dt dt dt dh ------ = u dt 0.008g du ------ – g – ---------------- u 2 ------ = gT w w dt 2 du d Q . With these definitions, the system of two first-order ODEs is: (b) Define u = dQ ------- , ------ = --------2 dt dt dt dQ ------- = u dt 250 Q du = 1000 – 500 ------------ --------- u – --------- u 3 – --------------------------------------–6 15 15 15 dt 15 4.2 u 10 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.8 Write the following system of two second-order ODEs as a system of four first-order ODEs: 2 J § dx · § dx · 2 § dy · 2 d--------x- = – --- ----------- + -----© dt ¹ m © dt ¹ © dt ¹ dt 2 2 J § dy · § dx · 2 § dy · 2 d--------y- = – g – --- ----------- + -----© dt ¹ m © dt ¹ © dt ¹ dt 2 Solution 2 2 d x dx du d y dw ------ , ------- = -------- , and u = ------ , ------ = -------Define w = dy 2 2 dt dt dt dt dt dt With these definitions, the system of four first-order ODEs is: dx dy ------ = u ------ = w dt dt J 2 2 du ------ = – ---- u u + w , m dt J 2 2 dw ------- = – g – ---- w u + w m dt Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.9 Use the user-defined MATLAB function odeEULER (Program 8-1 which is listed in Example 8-1) to solve Problem 8.1. Write a MATLAB program is a script file that solves the ODE in Problem 8.1 three times. Once by using h = 0.6 , once by using h = 0.3 , and once by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.1) and plot the three numerical solutions (all in the same figure). Solution The listing of the user-defined function odeEULER is: function [x, y] = odeEULER(ODE,a,b,h,yINI) % odeEULER solves a first order initial value ODE using Euler's % explicit method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h Step size. % yINI The value of the solution y at the first point (initial value. % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = yINI; N = (b-a)/h; for i = 1:N x(i+1) = x(i) + h; y(i+1) = y(i) + feval(ODE,x(i),y(i))*h; end The user-defined function odeEULER is used in following program (in a script file) solves the problem. % HW 8_9 Script clear all Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 a=0; b=1.8; yINI=1; h1=0.6; h2=0.3; h3=0.1; [x1, y1] = odeEULER('ODEHW8_1',a,b,h1,yINI); [x2, y2] = odeEULER('ODEHW8_1',a,b,h2,yINI); [x3, y3] = odeEULER('ODEHW8_1',a,b,h3,yINI); fplot('x^2-exp(0.5*x^2)+2',[0 1.8]) hold on plot(x1,y1,'o',x2,y2,'s',x3,y3,'*') hold off xlabel('x'); ylabel('y') legend('Exact','h=0.6','h=0.3','h=0.1',3) disp('Numerical solution with h=0.6') Answer=[x1;y1] disp('Numerical solution with h=0.3') Answer=[x2;y2] disp('Numerical solution with h=0.1') Answer=[x3;y3] The listing of the user-defined function ODEHW8_1 (the ODE in Problem 8.3) that is used in the solution is: function dydx = ODEHW8_1(x,y) dydx=y*x-x^3; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Numerical solution with Answer = 0 0.6000 1.0000 1.0000 Numerical solution with Answer = 0 0.3000 1.0000 1.0000 h=0.6 1.2000 1.2304 h=0.3 1.8000 1.0795 0.6000 1.0819 0.9000 1.2118 1.2000 1.3203 1.5000 1.2773 1.8000 0.8395 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 Numerical solution with Answer = Columns 1 through 7 0 0.1000 1.0000 1.0000 Columns 8 through 14 0.7000 0.8000 1.1821 1.2305 Columns 15 through 19 1.4000 1.5000 1.3157 1.2255 h=0.1 0.2000 1.0099 0.3000 1.0293 0.4000 1.0575 0.5000 1.0934 0.6000 1.1355 0.9000 1.2778 1.0000 1.3199 1.1000 1.3518 1.2000 1.3675 1.3000 1.3587 1.6000 1.0718 1.7000 0.8337 1.8000 0.4841 1.4 1.2 1 y 0.8 0.6 0.4 Exact h=0.6 h=0.3 h=0.1 0.2 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.10 Use the user-defined MATLAB function odeEULER (Program 8-1 which is listed in Example 8-1) to solve Problem 8.2. Write a MATLAB program is a script file that solves the ODE in Problem 8.2 three times. Once by using h = 0.5 , once by using h = 0.25 , and once by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.2) and plot the three numerical solutions (all in the same figure). Solution The listing of the user-defined function odeEULER is: function [x, y] = odeEULER(ODE,a,b,h,yINI) % odeEULER solves a first order initial value ODE using Euler's % explicit method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h Step size. % yINI The value of the solution y at the first point (initial value. % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = yINI; N = (b-a)/h; for i = 1:N x(i+1) = x(i) + h; y(i+1) = y(i) + feval(ODE,x(i),y(i))*h; end The the user-defined function odeEULER is used in following program (in a script file) solves the problem. % HW 8_10 Script clear all Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 a=0; b=1.5; yINI=1; h1=0.5; h2=0.25; h3=0.1; [x1, y1] = odeEULER('ODEHW8_2',a,b,h1,yINI) [x2, y2] = odeEULER('ODEHW8_2',a,b,h2,yINI); [x3, y3] = odeEULER('ODEHW8_2',a,b,h3,yINI); fplot('x+2*exp(-x)-1',[0 1.5]) hold on plot(x1,y1,'o',x2,y2,'s',x3,y3,'*') hold off xlabel('x'); ylabel('y') legend('Exact','h=0.5','h=0.25','h=0.1',3) axis([0 1.5 0 1.5]) disp('Numerical solution with h=0.5') Answer=[x1;y1] disp('Numerical solution with h=0.25') Answer=[x2;y2] disp('Numerical solution with h=0.1') Answer=[x3;y3] The listing of the user-defined function ODEHW8_2 (the ODE in Problem 8.2) that is used in the solution is: function dydx = ODEHW8_2(x,y) dydx=x-y; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Numerical solution with Answer = 0 0.5000 1.0000 0.5000 Numerical solution with Answer = 0 0.2500 h=0.5 1.0000 0.5000 h=0.25 1.5000 0.7500 0.5000 0.7500 1.0000 1.2500 1.5000 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 1.0000 0.7500 Numerical solution with Answer = Columns 1 through 7 0 0.1000 1.0000 0.9000 Columns 8 through 14 0.7000 0.8000 0.6566 0.6609 Columns 15 through 16 1.4000 1.5000 0.8575 0.9118 0.6250 h=0.1 0.5938 0.6328 0.7246 0.8560 0.2000 0.8200 0.3000 0.7580 0.4000 0.7122 0.5000 0.6810 0.6000 0.6629 0.9000 0.6748 1.0000 0.6974 1.1000 0.7276 1.2000 0.7649 1.3000 0.8084 1.5 y 1 0.5 Exact h=0.5 h=0.25 h=0.1 0 0 0.5 1 1.5 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.11 Write a user-defined MATLAB function that solves a first-order ODE by applying the midpoint method (use the form of second-order Runge–Kutta method, Eqs. (8.65), (8.66)). For function name and arguments use [x,y]=odeMIDPOINT(ODE,a,b,h,yINI), where the input argument ODE is the dy name (string) of the user-defined function that calculates ------ , a and b define the domain of the solution, h dx is the step size, and yINI is the initial value. The output arguments, x and y, are vectors with the x and y coordinates of the solution. Use the function odeMIDPOINT to solve the ODE in Problem 8.2. Write a MATLAB program in a script file that solves the ODE twice, once by using h = 0.5 and once by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.2) and plot the two numerical solutions (all in the same figure). Solution The listing of the user-defined function odeMIDPOIN is: function [x, y] = odeMIDPOINT(ODE,a,b,h,yINI) % odeMIDPOINT solves a first order initial value ODE using the % midpoint method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h Step size. % yINI The value of the solution y at the first point (initial value. % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = yINI; N = (b-a)/h; for i = 1:N x(i+1) = x(i) + h; K1 = feval(ODE,x(i),y(i)); K2 = feval(ODE,x(i)+h/2,y(i)+K1*h/2); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 y(i+1) = y(i) + K2*h; end The the user-defined function odeMIDPOIN is used in following program (in a script file) solves the problem: % HW 8_11 Script clear all a=0; b=1.5; yINI=1; h1=0.5; h2=0.1; [x1, y1] = odeMIDPOINT('ODEHW8_2',a,b,h1,yINI); [x2, y2] = odeMIDPOINT('ODEHW8_2',a,b,h2,yINI); fplot('x+2*exp(-x)-1',[0 1.5]) hold on plot(x1,y1,'s',x2,y2,'*r') hold off xlabel('x'); ylabel('y') legend('Exact','h=0.5','h=0.1',3) axis([0 1.5 0 1.5]) disp('Numerical solution with h=0.5') Answer=[x1;y1] disp('Numerical solution with h=0.1') Answer=[x2;y2] The listing of the user-defined function ODEHW8_2 (the ODE in Problem 8.2) that is used in the solution is: function dydx = ODEHW8_2(x,y) dydx=x-y; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Numerical solution with h=0.5 Answer = Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 0 0.5000 1.0000 0.7500 Numerical solution with Answer = Columns 1 through 7 0 0.1000 1.0000 0.9100 Columns 8 through 14 0.7000 0.8000 0.6944 0.7000 Columns 15 through 16 1.4000 1.5000 0.8944 0.9475 1.0000 0.7813 h=0.1 1.5000 0.9883 0.2000 0.8381 0.3000 0.7824 0.4000 0.7416 0.5000 0.7142 0.6000 0.6988 0.9000 0.7145 1.0000 0.7371 1.1000 0.7671 1.2000 0.8037 1.3000 0.8463 1.5 y 1 0.5 Exact h=0.5 h=0.1 0 0 0.5 1 1.5 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.12 Write a user-defined MATLAB function that solves a first-order ODE by applying the classical thirdorder Runge–Kutta method, Eqs. (8.82), (8.83). For function name and arguments use [x,y]=odeRK3(ODE,a,b,h,yINI), where the input argument ODE is the name (string) of the userdy defined function that calculates ------ , a and b define the domain of the solution, h is the step size, and yINI dx is the initial value. The output arguments, x and y, are vectors with the x and y coordinates of the solution. Use the function odeRK3 to solve Problem 8.2. Write a MATLAB program in a script file that solves the ODE in Problem 8.2 twice, once by using h = 0.5 and once by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.2) and plot the two numerical solutions (all in the same figure). Solution The listing of the user-defined function odeRK3 is: function [x, y] = odeRK3(ODE,a,b,h,y1) % odeRK4 solves a first order initial value ODE using Ronge-Kutta third % order method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h The size of a increment. % y1 The value of the solution y at the first point (initial value). % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = y1; n = (b-a)/h; for i = 1:n x(i+1) = x(i) + h; K1 = feval(ODE,x(i),y(i)); xhalf = x(i) + h/2; yK1 = y(i) + K1*h/2; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 K2 = feval(ODE,xhalf,yK1); yK2 = y(i) - K1*h + 2*K2*h; K3 = feval(ODE,xhalf,yK2); y(i+1) = y(i) + (K1 + 4*K2 + K3)*h/6; end The the user-defined function odeMIDPOIN is used in following program (in a script file) solves the problem: % HW 8_12 Script clear all a=0; b=1.5; yINI=1; h1=0.5; h2=0.1; [x1, y1] = odeRK3('ODEHW8_2',a,b,h1,yINI); [x2, y2] = odeRK3('ODEHW8_2',a,b,h2,yINI); fplot('x+2*exp(-x)-1',[0 1.5]) hold on plot(x1,y1,'s',x2,y2,'*r') hold off xlabel('x'); ylabel('y') legend('Exact','h=0.25','h=0.1',3) axis([0 1.5 0 1.5]) disp('Numerical solution with h=0.5') Answer=[x1;y1] disp('Numerical solution with h=0.1') Answer=[x2;y2] The listing of the user-defined function ODEHW8_2 (the ODE in Problem 8.2) that is used in the solution is: function dydx = ODEHW8_2(x,y) dydx=x-y; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 Numerical solution with Answer = 0 0.5000 1.0000 0.6875 Numerical solution with Answer = Columns 1 through 7 0 0.1000 1.0000 0.9088 Columns 8 through 14 0.7000 0.8000 0.6887 0.6938 Columns 15 through 16 1.4000 1.5000 0.8866 0.9394 h=0.5 1.0000 0.6966 h=0.1 1.5000 0.9000 0.2000 0.8359 0.3000 0.7793 0.4000 0.7377 0.5000 0.7096 0.6000 0.6936 0.9000 0.7079 1.0000 0.7302 1.1000 0.7599 1.2000 0.7962 1.3000 0.8387 1.5 y 1 0.5 Exact h=0.25 h=0.1 0 0 0.5 1 1.5 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.13 Write a user-defined MATLAB function that solves a first-order ODE by using the second-order Adams–Bashforth method. The function should use the modified Euler method for calculating the solution at the second point, and Eq. (8.95) for calculating the solution at the rest of the points. For function name and arguments use [x,y]=odeAdams2(ODE,a,b,h,yINI), where the input argument ODE is the dy name (string) of the user-defined function that calculates ------ , a and b define the domain of the solution, h dx is the step size, and yINI is the initial value. The output arguments, x and y, are vectors with the x and y coordinates of the solution. Use the function odeAdams2 to solve the ODE in Problem 8.3. Write a MATLAB program in a script file that solves the ODE by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.3) and the numerical solution (both in the same figure). Solution The listing of the user-defined function odeAdams2 is: function [x, y] = odeAdams2(ODE,a,b,h,yINI) % odeMIDPOINT solves a first order initial value ODE using the % second-order Abam-Bashforth method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h Step size. % yINI The value of the solution y at the first point (initial value. % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = yINI; N = (b-a)/h; x(2) = x(1) + h; K1=feval(ODE,x(1),y(1)); K2=feval(ODE,x(2),y(1)+K1*h); y(2) = y(1) + (K1+K2)*h/2; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 for i = 2:N x(i+1) = x(i) + h; y(i+1) = y(i) + (3*feval(ODE,x(i),y(i))-feval(ODE,x(i-1),y(i-1)))*h/2; end The listing of the user-defined function HW8_3ODE (the ODE in Problem 8.3) that is used in the solution is: function dydt = HW8_3ODE(t,y) dydt = y+t^3; The the user-defined function odeAdams2 is used in following program (script file) solves the problem: % HW 8_13 Solution Script clear all a=0.5; b=2; yINI=-1; h=0.1; [t, y] = odeAdams2('HW8_3ODE',a,b,h,yINI) fplot('-t.^3-3*t.^2-6*t-6+71/8*exp(t)/exp(1/2)',[0.5 2]) hold on plot(t,y,'*r') hold off xlabel('x'); ylabel('y') legend('Exact','Numerical',2) Answer=[t;y] When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Answer = Columns 1 through 7 0.5000 0.6000 0.7000 -1.0000 -1.0873 -1.1743 Columns 8 through 14 1.2000 1.3000 1.4000 -1.3966 -1.3432 -1.2317 0.8000 -1.2554 0.9000 -1.3253 1.0000 -1.3776 1.1000 -1.4045 1.5000 -1.0476 1.6000 -0.7741 1.7000 -0.3921 1.8000 0.1199 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 Columns 15 through 16 1.9000 2.0000 0.7866 1.6359 2 1.5 Exact Numerical 1 y 0.5 0 −0.5 −1 −1.5 0.5 1 1.5 2 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.14 Write a user-defined MATLAB function that solves a first-order ODE by using the modified Euler predictor–corrector method (see algorithm in Section 8.7). In each step the iterations should continue until y k k–1 i + 1 – yi + 1 the estimated relative error is smaller than 0.001, ------------------------------ d H = 0.001 . For function name and arguk–1 yi + 1 ments use [x,y]=odeEulerPreCor(ODE,a,b,h,yINI), where the input argument ODE is the dy name (string) of the user-defined function that calculates ------ , a and b define the domain of the solution, h dx is the step size, and yINI is the initial value. The output arguments, x and y, are vectors with the x and y coordinates of the solution. Use the function odeAdams2 to solve the ODE in Problem 8.3. Write a MATLAB program in a script file that solves the ODE by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.3) and the numerical solution (both in the same figure). Solution The listing of the user-defined function odeEulerPreCor is: function [x, y] = odeEulerPreCor(ODE,a,b,h,yINI) % odeEulerPreCor solves a first order initial value ODE using the modified % Euler's predictor-corrector method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h Step size. % yINI The value of the solution y at the first point (initial value. % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = yINI; N = (b-a)/h; C=0; for i = 1:N Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 x(i+1) = x(i) + h; clear ye ye(1) = y(i) + feval(ODE,x(i),y(i))*h; for j = 2:20 ye(j) = y(i) + (feval(ODE,x(i),y(i))+feval(ODE,x(i+1),ye(j-1)))*h/2; Err=abs((ye(j)-ye(j-1))/ye(j-1)); if Err <= 0.001 break end if j == 20 C=1; end end if C==1 disp('Progam does not converge') break end y(i+1) = ye(j); end The the user-defined function odeEulerPreCor is used in following program (script file) solves the problem: % HW 8_14 Solution Script clear all a=0.5; b=2; yINI=-1; h=0.1; [t, y] = odeEulerPreCor('HW8_3ODE',a,b,h,yINI); fplot('-t.^3-3*t.^2-6*t-6+71/8*exp(t)/exp(1/2)',[0.5 2]) hold on plot(t,y,'*r') hold off Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 xlabel('x'); ylabel('y') legend('Exact','Numerical',2) Answer=[t;y] The listing of the user-defined function HW8_3ODE (the ODE in Problem 8.3) that is used in the solution is: function dydt = HW8_3ODE(t,y) dydt = y+t^3; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Answer = Columns 1 through 7 0.5000 0.6000 0.7000 -1.0000 -1.0873 -1.1724 Columns 8 through 14 1.2000 1.3000 1.4000 -1.3708 -1.3086 -1.1862 Columns 15 through 16 1.9000 2.0000 0.9271 1.8067 0.8000 -1.2508 0.9000 -1.3171 1.0000 -1.3648 1.1000 -1.3858 1.5000 -0.9891 1.6000 -0.7000 1.7000 -0.2995 1.8000 0.2345 2 1.5 Exact Numerical 1 y 0.5 0 −0.5 −1 −1.5 0.5 1 1.5 2 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.15 The user-defined MATLAB function Sys2ODEsRK2(ODE1,ODE2,a,b,h,yINI,zINI) (Program 8-5), that is listed in the solution of Example 8-7, solves a system of two ODEs by using the secondorder Runge–Kutta method (modified Euler’s version). Modify the function such that the two ODEs are entered in one input argument. Similarly, the domain should be entered by using one input argument, and the two initial conditions entered in one input argument. For function name and arguments use [t,x,y]=Sys2ODEsModEu(ODEs,ab,h,INI). The input argument ODE is the name (string) of the dx dy user-defined function that calculates ------ and ------ (the input arguments of this user-defined function are dt dt dx dy t,x,y, and the output argument is a two-element vector with the values of ------ and ------ ). ab is a two-eledt dt ment vector that defines the domain of the solution, h is the step size, and INI is a two-element vector with the initial values. The output arguments, t, x, and y, are vectors of the solution. Use the function Sys2ODEsModEu to solve the system of ODEs in Problem 8.5. Write a MATLAB program in a script file that solves the system by using h = 0.1 . The program should also plot the exact solution (given in Problem 8.5) and the numerical solution (both in the same figure). Solution The listing of the user-defined function Sys2ODEsModEu is: function [t, x, y] = Sys2ODEsModEu(ODEs,ab,h,INI) % Sys2ODEsModEu solves a system of two first-order initial value ODEs using % second-order Ronge-Kutta method (modified Euler). % The independent variable is t, and the dependent variables are x and y. % Input variables: % ODEs Name (string) of a function file that calculates dx/dt and dy/dt % The output argument of the function is a vector. its first element is dxdt % and its second element is dydt. % ab A vector with the domain of the solution ab = [a, b]. % h The size of a increment. % INI A vector with the he initial values of x and y [xINI, yINI]. % Output variable: % t A vector with the t coordinate of the solution points. % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 t(1) = ab(1); x(1) = INI(1); y(1) = INI(2); N = round((ab(2) - ab(1))/h); for i = 1:N t(i+1) = t(i) + h; K1 = feval(ODEs,t(i),x(i),y(i)); K2 = feval(ODEs,t(i+1),x(i)+K1(1)*h,y(i)+K1(2)*h); x(i+1) = x(i) + (K1(1) + K2(1))*h/2; y(i+1) = y(i) + (K1(2) + K2(2))*h/2; end The the user-defined function Sys2ODEsModEu is used in following program (script file) solves the problem: % HW 8_15 Solution Script clear all ab=[0 1.2]; INI=[1 0.5]; h=0.1; [t, x, y] = Sys2ODEsModEu('ODEsHW8_5',ab,h,INI); fplot('exp(t^2/2)*(cos(t)-0.5*sin(t))',[0 1.2]) hold on fplot('-exp(t^2/2)*(-sin(t)-0.5*cos(t))',[0 1.2],'g') plot(t,x,'*r') plot(t,y,'sr') hold off xlabel('x'); ylabel('y') legend('Exact x','Exact y','Numerical x','Numerical y',2) Answer=[t;x;y] The listing of the user-defined function ODEsHW8_5 (the system of ODEs in Problem 8.5) that is used in the solution is: function [dxydt] = ODEsHW8_5(t,x,y) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 dxydt(1)=x*t-y; dxydt(2)=y*t+x; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Answer = Columns 1 through 7 0 0.1000 1.0000 0.9497 0.5000 0.6005 Columns 8 through 13 0.7000 0.8000 0.5656 0.4656 1.3133 1.4697 0.2000 0.8984 0.7030 0.3000 0.8446 0.8093 0.4000 0.7867 0.9215 0.5000 0.7226 1.0415 0.9000 0.3450 1.6431 1.0000 0.1977 1.8362 1.1000 0.0157 2.0517 1.2000 -0.2113 2.2927 0.6000 0.6499 1.1714 2.5 2 Exact x Exact y Numerical x Numerical y y 1.5 1 0.5 0 −0.5 0 0.2 0.4 0.6 0.8 1 1.2 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.16 The user-defined MATLAB function Sys2ODEsRK4(ODE1,ODE2,a,b,h,x1,y1) (Program 8-6) that is listed in the solution of Example 8-8 solves a system of two ODEs by using fourth-order Runge–Kutta method. Modify the function such that the two ODEs are entered in one input argument. Similarly, the domain is entered by using one input argument, and the two initial conditions are entered in one input argument. For function name and arguments use [t,x,y]=Sys2ODEsRKclas(ODEs,ab,h,INI). The input argument ODE is the name (string) of dx dy the user-defined function that calculates ------ and ------ (the input arguments of this user-defined function are dt dt dx dy t,x,y, and the output argument is a two-element vector with the values of ------ and ------ ). ab is a two-eledt dt ment vector that defines the domain of the solution, h is the step size, and INI is a two-element vector with the initial values. The output arguments, t, x, and y, are vectors of the solution. Use the function Sys2ODEsRKclas to solve the system of ODEs in Problem 8.5. Write a MATLAB program in a script file that solves the system by using h = 0.1 . The program should also plot the exact solution and the numerical solution (both in the same figure). Solution The listing of the user-defined function Sys2ODEsRKclas is: function [t, x, y] = Sys2ODEsRKclas(ODEs,ab,h,INI) % Sys2ODEsRKclas solves a system of two first order initial value ODEs using % fourth order Ronge-Kutta method. % The independent variable is t, and the dependent variables are x and y. % Input variables: % ODEs Name (string) of a function file that calculates dx/dt and dy/dt % The output argument of the function is a vector. its first element is dxdt % and its second element is dydt. %ab A vector with the domain of the solution ab = [a, b]. % h The size of a increment. % INI A vector with the he initial values of x and y [xINI, yINI]. % Output variable: % t A vector with the t coordinate of the solution points. % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 t(1)=ab(1); x(1) = INI(1); y(1) = INI(2); n = round((ab(2) - ab(1))/h); for i = 1:n t(i+1) = t(i) + h; tm = t(i) + h/2; K1 = feval(ODEs,t(i),x(i),y(i)); K2 = feval(ODEs,tm,x(i)+K1(1)*h/2,y(i)+K1(2)*h/2); K3 = feval(ODEs,tm,x(i)+K2(1)*h/2,y(i)+K2(2)*h/2); K4 = feval(ODEs,t(i+1),x(i)+K3(1)*h,y(i)+K3(2)*h); x(i+1) = x(i) + (K1(1) + 2*K2(1) + 2*K3(1) + K4(1))*h/6; y(i+1) = y(i) + (K1(2) + 2*K2(2) + 2*K3(2) + K4(2))*h/6; end The the user-defined function Sys2ODEsRKclas is used in following program (script file) which solves the problem: % HW 8_16 Solution Script clear all ab=[0 1.2]; INI=[1 0.5]; h=0.1; [t, x, y] = Sys2ODEsRKclas('ODEsHW8_5',ab,h,INI); fplot('exp(t^2/2)*(cos(t)-0.5*sin(t))',[0 1.2]) hold on fplot('-exp(t^2/2)*(-sin(t)-0.5*cos(t))',[0 1.2],'g') plot(t,x,'*r') plot(t,y,'sr') hold off xlabel('x'); ylabel('y') legend('Exact x','Exact y','Numerical x','Numerical y',2) Answer=[t;x;y] The listing of the user-defined function ODEsHW8_5 (the system of ODEs in Problem 8.5) that is used in Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 the solution is: function [dxydt] = ODEsHW8_5(t,x,y) dxydt(1)=x*t-y; dxydt(2)=y*t+x; When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Answer = Columns 1 through 7 0 0.1000 1.0000 0.9498 0.5000 0.6003 Columns 8 through 13 0.7000 0.8000 0.5656 0.4655 1.3117 1.4676 0.2000 0.8985 0.7026 0.3000 0.8447 0.8088 0.4000 0.7868 0.9207 0.5000 0.7228 1.0405 0.9000 0.3448 1.6404 1.0000 0.1971 1.8328 1.1000 0.0146 2.0474 1.2000 -0.2130 2.2870 0.6000 0.6501 1.1701 2.5 2 Exact x Exact y Numerical x Numerical y y 1.5 1 0.5 0 −0.5 0 0.2 0.4 0.6 0.8 1 1.2 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.17 Write a program in a script file that solves the IVP in Example 8-13, using Euler’s implicit method with h = 0.1 . Plot the numerical and analytical solutions. Solution The ODE that is being solved is: dy ------ = – 2.5y , dx with y 0 = 1, for 0 x 3.4 With Euler’s implicit method the numerical solution is obtained by applying: xi + 1 = xi + h y i + 1 = y i + f x i + 1 y i + 1 h Substituting the ODE and solving for y i + 1 gives: yi y i + 1 = ------------------1 + 2.5h The last equation is used in following program (script file) solves the problem: % Script HW8_17. clear all a=0; b=3.4; h = 0.1; x(1) = a; y(1) = 1; N=round((b-a)/h); for i = 1:N x(i+1)=x(i)+h; y(i+1)=y(i)/(1+2.5*h); end fplot('exp(-2.5*x)',[0 3.4]) hold on plot(x,y,'r*') hold off xlabel('x'); ylabel('y') legend('Exact solution','Numerical solution') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 When the program is executed the following figure is displayed in the Figure Window. 1 Exact solution Numerical solution 0.8 y 0.6 0.4 0.2 0 0 0.5 1 1.5 2 2.5 3 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.18 An inductor and a nonlinear resistor of resistance R = 500 + 250I 2 : are connected in series with a DC power source and a switch, as shown in the figure. The switch is initially open and then is closed at time t = 0 . The current I in the circuit for t ! 0 is determined from the solution of the equation: V R dI ----- = -----0 – --- I dt L L For V 0 = 1000 V and L = 15 Henries, determine and plot the current as a function of time for 0 t t t 0.1 s. (a) Solve the problem with the function odeMIDPOINT that was written in Problem 8.11. For step size use 0.005 s. (b) Solve the problem using one of MATLAB’s built-in functions for solving an ODE. Solution The ODE in the problem is written in a user defined function named ODEHW8_18: function [dIdt] = ODEHW8_18(t,I) R=500+250*I*I; V0=1000; L=15; dIdt=V0/L-R*I/L; (a) The listing of the user-defined function odeMIDPOINT is: function [x, y] = odeMIDPOINT(ODE,a,b,h,yINI) % odeMIDPOINT solves a first order initial value ODE using the % midpoint method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h Step size. % yINI The value of the solution y at the first point (initial value. % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 x(1) = a; y(1) = yINI; N = (b-a)/h; for i = 1:N x(i+1) = x(i) + h; K1 = feval(ODE,x(i),y(i)); K2 = feval(ODE,x(i)+h/2,y(i)+K1*h/2); y(i+1) = y(i) + K2*h; end The the user-defined functions odeMIDPOINT and ODEHW8_18 are used in following program (script file) which solves the problem: % HW 8_18a Script clear all [t, I] = odeMIDPOINT('ODEHW8_18',0,0.1,0.005,0); plot(t,I) xlabel('t (s)'); ylabel('I Answer=[t;I] (A)') When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Answer = Columns 1 through 7 0 0.0050 0 0.3052 Columns 8 through 14 0.0350 0.0400 1.1068 1.1341 Columns 15 through 21 0.0700 0.0750 1.1769 1.1779 0.0100 0.5569 0.0150 0.7535 0.0200 0.8976 0.0250 0.9975 0.0300 1.0639 0.0450 1.1513 0.0500 1.1620 0.0550 1.1687 0.0600 1.1728 0.0650 1.1754 0.0800 1.1785 0.0850 1.1789 0.0900 1.1791 0.0950 1.1793 0.1000 1.1794 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 1.4 1.2 I (A) 1 0.8 0.6 0.4 0.2 0 0 0.02 0.04 0.06 0.08 0.1 0.12 t (s) (b) The following program (script file) uses MATLAB built-in function ode45 to solve the problem. % HW 8_18b Script clear all [t,I]=ode45('ODEHW8_18',[0:0.005:0.1],0); plot(t,I) xlabel('t (s)'); ylabel('I Answer=[t';I'] (A)') When the program is executed the numerical solution is displayed in the Command Window and the following plot is displayed in the Figure Window: Answer = Columns 1 through 7 0 0.0050 0 0.3065 Columns 8 through 14 0.0350 0.0400 1.1126 1.1389 Columns 15 through 21 0.0100 0.5593 0.0150 0.7574 0.0200 0.9029 0.0250 1.0037 0.0300 1.0702 0.0450 1.1550 0.0500 1.1648 0.0550 1.1707 0.0600 1.1742 0.0650 1.1763 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 0.0700 1.1776 0.0750 1.1784 0.0800 1.1788 0.0850 1.1791 0.0900 1.1793 0.0950 1.1794 0.1000 1.1794 1.4 1.2 I (A) 1 0.8 0.6 0.4 0.2 0 0 0.02 0.04 0.06 0.08 0.1 t (s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.19 Consider the cylindrical water tank that is shown in Fig. 8-1 (shown also on the right). The tank is being filled at the top, and water flows out of the tank through a pipe that is connected at the bottom. The rate of change of the height, h, of the water is given by Eq. (8.6): S dh UA tank ------ = K 1 + K 2 cos § ------ t· – UA pipe 2gh © 12 ¹ dt For the given tank, A tan k = 3.13 m2, A pipe = 0.06 m2, K 2 = 200 kg/h. Also, U = 1000 kg/m3, K 1 = 300 kg/h, g = 9.81 m/s2. and Determine, and plot the height of the water as a function of time for 0 d t d 150 s, if at t = 0 , h = 3 m. (a) Use the user-defined function odeRK3 that was written in Problem 8.12. For step size use 0.1s. (b) Use one of MATLAB’s built-in functions for solving an ODE. Solution The ODE in the problem is written in a user defined function named ODEHW8_19: function dhdt=ODEHW8_19(t,h) At=3.13; Ap=0.06; K1=300; K2=200; rho=1000; C=pi/12; g=9.81; dhdt=(K1+K2*cos(C*t)-rho*Ap*sqrt(2*g*h))/(At*rho); (a) The listing of the user-defined function odeRK3 is: function [x, y] = odeRK3(ODE,a,b,h,y1) % odeRK4 solves a first order initial value ODE using Ronge-Kutta third % order method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h The size of a increment. % y1 The value of the solution y at the first point (initial value). Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. x(1) = a; y(1) = y1; n = (b-a)/h; for i = 1:n x(i+1) = x(i) + h; K1 = feval(ODE,x(i),y(i)); xhalf = x(i) + h/2; yK1 = y(i) + K1*h/2; K2 = feval(ODE,xhalf,yK1); yK2 = y(i) - K1*h + 2*K2*h; K3 = feval(ODE,xhalf,yK2); y(i+1) = y(i) + (K1 + 4*K2 + K3)*h/6; end The the user-defined functions odeRK3 and ODEHW8_19 are used in following program (script file) which solves the problem: % Solution of Problem 8.19 (script file) clear all [t, h] = odeRK3('ODEHW8_19',0,150,0.1,3); plot(t,h) xlabel('Time (s)'); ylabel('Water Height (m)') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 When the program is executed the following plot is displayed in the Figure Window: Water Height (m) 3.5 3 2.5 2 1.5 1 0 50 100 150 Time (s) (b) The following program (script file) uses MATLAB built-in function ode45 to solve the problem. % Solution of Problem 8.19 Part (b) clear all [t,h]=ode45('ODEHW8_19',[0:1:150],3); plot(t,h) xlabel('Time (s)'); ylabel('Water Height (m)') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 When the program is executed the following plot is displayed in the Figure Window: Water Height (m) 3.5 3 2.5 2 1.5 1 0 50 100 150 Time (s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.20 A spherical water tank of radius R = 4 m is emptied through a small circular hole of radius r = 0.02 m at the bottom. The top of the tank is open to the atmosphere. The instantaneous water level, h (measured from the bottom of the tank, at the drain), in the tank can be determined from the solution of the following ODE: r 2 2gh dh ------ = – --------------------2 dt 2hR – h 2 where g = 9.81 m/s . If the initial ( t = 0 ) water level is h = 6 m, find the time required to drain the tank to a level of h = 0.5 m. (a) Use the fourth-order Runge–Kutta method (use the user-defined function odeRK (Program 8-4) that was developed in Example 8-6). (b) Use one of MATLAB’s built-in functions for solving an ODE. Solution The ODE in the problem is written in a user defined function named ODEHW8_20: function dhdt = HW8_20(t, h) R=4; r=0.02; g=9.81; dhdt = -1*r^2*sqrt(2*g*h)/(2*h*R-h^2); (a) The listing of the user-defined function odeRK4 is: function [x, y] = odeRK4(ODE,a,b,h,yIni) % odeRK4 solves a first order initial value ODE using Ronge-Kutta fourth % order method. % Input variables: % ODE Name (string) of a function file that calculates dy/dx. % a The first value of x. % b The last value of x. % h The size of a increment. % y1 The value of the solution y at the first point (initial value). % Output variable: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 x(1) = a; y(1) = yIni; n = (b-a)/h; for i = 1:n x(i+1) = x(i) + h; K1 = feval(ODE,x(i),y(i)); xhalf = x(i) + h/2; yK1 = y(i) + K1*h/2; K2 = feval(ODE,xhalf,yK1); yK2 = y(i) + K2*h/2; K3 = feval(ODE,xhalf,yK2); yK3 = y(i) + K3*h; K4 = feval(ODE,x(i+1),yK3); y(i+1) = y(i) + (K1 + 2*K2 + 2*K3 + K4)*h/6; end The the user-defined functions odeRK4 and ODEHW8_20 are used in following program (script file) which solves the problem: % Solution of Problem 8.20(a) (script file) clear all [t, h] = odeRK4('ODEHW8_20',0,24000,1,6); plot(t,h) xlabel('Time (s)'); ylabel('Water Height (m)') n=length(h); for i=1:n if h(i) < 0.5 break end end Time=t(i) height = h(i) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 When the program is executed the following (time in seconds height in meters) is displayed in the Command Window: Time = 23308 height = 0.4999 (b) The following program (script file) uses MATLAB built-in function ode45 to solve the problem. % Solution of Problem 8.20(b) (script file) clear all [t,h]=ode45('ODEHW8_20',[0:1:24000],6); plot(t,h) xlabel('Time (s)'); ylabel('Water Height (m)') n=length(h); for i=1:n if h(i) < 0.5 break end end Time=t(i) height = h(i) When the program is executed the following (time in seconds height in meters) is displayed in the Command Window: Time = 23309 height = 0.4999 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.21 A small rocket having an initial weight of 3000 lb (including 2400 lb of fuel) and initially at rest, is launched vertically upward. The rocket burns fuel at a constant rate of 80 lb/s, which provides a constant thrust, T, of 7000 lb. The instantaneous weight of the rocket is w t = 3000 – 80t lb. The drag force, D, experienced 2 by the rocket is given by D = 0.008g §© ------ ·¹ lb, where y is distance in ft, and dt 2 g = 32.2 ft/s . Using Newton’s law, the equation of motion for the rocket is given by: dy 2 wd y ---- -------2- = T – w – D g dt Determine and plot the position, velocity, and acceleration of the rocket (three separate figures on one page) as a function of time from t = 0 when the rocket start moving upward from rest, until t = 3 . Reduce the second-order ODE to a system of two first-order ODEs. (a) Use the fourth-order Runge–Kutta method. Use either the user-defined function Sys2ODEsRK4 (Program 8-6) that was developed in Example 8-8, or the user-defined function Sys2ODEsRKclas that was written in Problem 8.16. For step size use 0.05 s. (b) Use one of MATLAB’s built-in functions for solving ODEs. Solution To solve the problem the second-order ODE is rewritten as a system of two first-order ODEs. This is done by introducing a new variable u, such that: 2 du d y u = dy ------ and ------ = -------2dt dt dt With these definitions the system of two first-order ODEs is: dy ------ = u with the initial condition y 0 = 0 dt g du ------ = ---- > T – w – 0.008gu 2 @ with the initial condition u 0 = 0 w dt where w t = 3000 – 80t (a) The listing of the user-defined function Sys2ODEsRK4 is: function [t, x, y] = Sys2ODEsRK4(ODE1,ODE2,a,b,h,x1,y1) % Sys2ODEsRK4 solves a system of two first order initial value ODEs using % fourth order Ronge-Kutta method. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % The independent variable is t, and the dependent variables are x and y. % Input variables: % ODE1 Name (string) of a function file that calculates dx/dt. % ODE2 Name (string) of a function file that calculates dy/dt. % a The first value of t. % b The last value of t. % h The size of a increment. % x1 The initial value of x. % y1 The initial value of y. % Output variable: % t A vector with the t coordinate of the solution points. % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. t(1)=a; x(1) = x1; y(1) = y1; n = (b-a)/h; for i = 1:n t(i+1) = t(i) + h; tm = t(i) + h/2; Kx1 = feval(ODE1,t(i),x(i),y(i)); Ky1 = feval(ODE2,t(i),x(i),y(i)); Kx2 = feval(ODE1,tm,x(i)+Kx1*h/2,y(i)+Ky1*h/2); Ky2 = feval(ODE2,tm,x(i)+Kx1*h/2,y(i)+Ky1*h/2); Kx3 = feval(ODE1,tm,x(i)+Kx2*h/2,y(i)+Ky2*h/2); Ky3 = feval(ODE2,tm,x(i)+Kx2*h/2,y(i)+Ky2*h/2); Kx4 = feval(ODE1,t(i+1),x(i)+Kx3*h,y(i)+Ky3*h); Ky4 = feval(ODE2,t(i+1),x(i)+Kx3*h,y(i)+Ky3*h); x(i+1) = x(i) + (Kx1 + 2*Kx2 + 2*Kx3 + Kx4)*h/6; y(i+1) = y(i) + (Ky1 + 2*Ky2 + 2*Ky3 + Ky4)*h/6; end Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 ------ = u is written in a user-defined function called ODEdydtHW8_21a: The differential equation dy dt function dydt = ODEdydtHW8_21a(t,y,u) dydt = u; The differential equation g du ------ = ---- > T – w – 0.008gu 2 @ w dt is written in a user-defined function called ODEduydtHW8_21a: function dudt = ODEdudtHW8_21a(t,y,u) g=32.2; T=7000; w=3000-80*t; dudt = (T-w-0.008*g*u.^2)*g./w; The user-defined functions Sys2ODEsRK4, ODEdydtHW8_21a, and ODEdudtHW8_21a are used in the following script file to solve the problem and make the plots. % Solution of HW Problem 8.21 a clear all [t, y, u] = Sys2ODEsRK4('ODEdydtHW8_21a','ODEdudtHW8_21a',0,3,0.1,0,0) subplot(3,1,1) plot(t,y) xlabel('Time (s)') ylabel('Position (ft)') subplot(3,1,2) plot(t,u) xlabel('Time (s)') ylabel('Velocity (ft/s)') Accel= ODEdudtHW8_21a(t,y,u) subplot(3,1,3) plot(t,Accel) xlabel('Time (s)') ylabel('Acceleration (ft/s^2)') When the script file is executed the following results are displayed in the Command Window, and the fig- Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 ure that follows is displayed in the Figure Window. t = Columns 1 through 7 0 0.1000 0.2000 Columns 8 through 14 0.7000 0.8000 0.9000 Columns 15 through 21 1.4000 1.5000 1.6000 Columns 22 through 28 2.1000 2.2000 2.3000 Columns 29 through 31 2.8000 2.9000 3.0000 y = Columns 1 through 7 0 0.2150 0.8607 Columns 8 through 14 10.5307 13.7349 17.3518 Columns 15 through 21 41.3769 47.3096 53.5955 Columns 22 through 28 89.9345 98.1041 106.5471 Columns 29 through 31 152.4619 162.3070 172.3481 u = Columns 1 through 7 0 4.3017 8.6132 Columns 8 through 14 29.9588 34.1154 38.2124 Columns 15 through 21 57.5279 61.1108 64.5882 Columns 22 through 28 80.2898 83.0823 85.7583 Columns 29 through 31 97.4374 99.4482 101.3572 Accel = Columns 1 through 7 0.3000 0.4000 0.5000 0.6000 1.0000 1.1000 1.2000 1.3000 1.7000 1.8000 1.9000 2.0000 2.4000 2.5000 2.6000 2.7000 1.9376 3.4451 5.3818 7.7448 21.3751 25.7976 30.6112 35.8074 60.2236 67.1830 74.4623 82.0501 115.2519 124.2070 133.4011 142.8230 12.9242 17.2245 21.5037 25.7517 42.2415 46.1945 50.0642 53.8440 67.9564 71.2126 74.3544 77.3805 88.3186 90.7647 93.0983 95.3217 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 42.9333 43.0829 43.1300 Columns 8 through 14 41.8337 41.2830 40.6443 Columns 15 through 21 36.3419 35.3080 34.2337 Columns 22 through 28 28.5092 27.3412 26.1795 Columns 29 through 31 20.6277 19.5940 18.5920 43.0737 42.9143 42.6529 42.2917 39.9231 39.1254 38.2580 37.3277 33.1262 31.9931 30.8414 29.6780 25.0293 23.8951 22.7811 21.6909 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 6 200 Position (ft) 150 100 50 0 0 0.5 1 1.5 2 Time (s) 2.5 3 3.5 0 0.5 1 1.5 2 Time (s) 2.5 3 3.5 0 0.5 1 1.5 2 Time (s) 2.5 3 3.5 120 Velocity (ft/s) 100 80 60 40 20 0 2 Acceleration (ft/s ) 45 40 35 30 25 20 15 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 7 g ------ = u and du ------ = ---- > T – w – 0.008gu 2 @ are written in a user-defined func(b) The differential equations dy dt dt w tion called ODEHW8_21b: function dydt = ODEHW8_21b(t, y) T=7000; g=32.2; w=3000-80*t; m=w/g; D=0.008*g*y(2)^2; dydt(1,1) = y(2); dydt(2,1) =(T-w-D)/m; The user-defined functions ODEHW8_21b, and MATLAB’s built-in function ode45 are used in the following script file to solve the problem and make the plots. % Solution of HW Problem 8.21 b clear all tspan = [0:0.1:3]; yini = [0 0]; [Time y] = ode45(@ODEHW8_21b,tspan,yini); subplot(3,1,1) plot(Time,y(:,1)) xlabel('Time (s)') ylabel('Position (ft)') subplot(3,1,2) plot(Time,y(:,2)) xlabel('Time (s)') ylabel('Velocity (ft/s)') w=3000-80*Time; Accel= (7000-w-(0.008*32.2*y(:,2).^2))*32.2./w; subplot(3,1,3) plot(Time,Accel) xlabel('Time (s)') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 8 ylabel('Acceleration (ft/s^2)') Answer=[Time, y(:,1), y(:,2), Accel]; Time (s) disp(' disp(Answer) Pos(ft) vel(ft/s) Acc (ft/s^2)') When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) Pos(ft) 0 0 0.1000 0.2150 0.2000 0.8607 0.3000 1.9376 0.4000 3.4451 0.5000 5.3818 0.6000 7.7448 0.7000 10.5307 0.8000 13.7349 0.9000 17.3518 1.0000 21.3751 1.1000 25.7976 1.2000 30.6112 1.3000 35.8074 1.4000 41.3769 1.5000 47.3096 1.6000 53.5955 1.7000 60.2236 1.8000 67.1830 1.9000 74.4623 2.0000 82.0501 2.1000 89.9345 2.2000 98.1041 2.3000 106.5471 2.4000 115.2519 2.5000 124.2070 2.6000 133.4011 2.7000 142.8230 vel(ft/s) 0 4.3017 8.6132 12.9242 17.2245 21.5037 25.7517 29.9588 34.1154 38.2124 42.2415 46.1945 50.0642 53.8440 57.5279 61.1108 64.5882 67.9564 71.2126 74.3544 77.3805 80.2898 83.0823 85.7583 88.3186 90.7647 93.0983 95.3217 Acc (ft/s^2) 42.9333 43.0829 43.1300 43.0737 42.9143 42.6529 42.2917 41.8337 41.2830 40.6443 39.9231 39.1254 38.2580 37.3277 36.3419 35.3080 34.2337 33.1262 31.9931 30.8414 29.6780 28.5092 27.3412 26.1795 25.0292 23.8951 22.7811 21.6909 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 9 2.8000 2.9000 3.0000 152.4619 162.3070 172.3481 97.4374 99.4482 101.3572 20.6277 19.5940 18.5920 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 10 200 Position (ft) 150 100 50 0 0 0.5 1 1.5 Time (s) 2 2.5 3 0 0.5 1 1.5 Time (s) 2 2.5 3 0 0.5 1 1.5 Time (s) 2 2.5 3 120 Velocity (ft/s) 100 80 60 40 20 0 Acceleration (ft/s2) 45 40 35 30 25 20 15 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.22 A U-tube manometer (used to measure pressure) is initially filled with water, but is exposed to a pressure difference such that the water level on the left side of the U-tube is 0.05 m higher than the water level on the water level on the right. At t = 0 the pressure difference is suddenly removed. When friction is neglected, the height of the water level on the left side, y, measured from the midplane between the two initial water levels is given by the solution of the equation: 2 d y L -------2- = – 2gy dt where L = 0.2 m is the total length of the U-tube, and g = 9.81 m/s2. Solve the dy ODE and plot y, and ------ (two separate figures on one page) as a function of t for dt the first 10 seconds. Reduce the second-order ODE to a system of two first-order ODEs and solve the system by: (a) using the user-defined function Sys2ODEsModEu that was written in Problem 8.15. For step size use 0.02 s. (b) using one of MATLAB’s built-in functions for solving ODEs. Solution To solve the problem the second-order ODE is rewritten as a system of two first-order ODEs. This is done by introducing a new variable w, such that: 2 d y dw w = dy ------ and ------- = -------2dt dt dt With these definitions the system of two first-order ODEs is: dy ------ = w with the initial condition y 0 = 0.025 dt 2gy dw ------- = – --------- with the initial condition w 0 = 0 L dt (a) The listing of the user-defined function Sys2ODEsModEu is: function [t, x, y] = Sys2ODEsModEu(ODEs,ab,h,INI) % Sys2ODEsModEu solves a system of two first-order initial value ODEs using % second-order Ronge-Kutta method (modified Euler). % The independent variable is t, and the dependent variables are x and y. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % Input variables: % ODEs Name (string) of a function file that calculates dx/dt and dy/ dt % The output argument of the function is a vector. its first element is dxdt % and its second element is dydt] % ab A vector with the domain of the solution ab = [a, b]. % h The size of a increment. % INI A vector with the he initial values of x and y [xINI, yINI]. % Output variable: % t A vector with the t coordinate of the solution points. % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. t(1) = ab(1); x(1) = INI(1); y(1) = INI(2); N = round((ab(2) - ab(1))/h); for i = 1:N t(i+1) = t(i) + h; K1 = feval(ODEs,t(i),x(i),y(i)); K2 = feval(ODEs,t(i+1),x(i)+K1(1)*h,y(i)+K1(2)*h); x(i+1) = x(i) + (K1(1) + K2(1))*h/2; y(i+1) = y(i) + (K1(2) + K2(2))*h/2; end 2gy dw The differential equations dy ------ = w and ------- = – --------dt dt L are written in a user-defined function called HW8_22aODEs: function [dywdt] = HW8_22aODEs(t,y,w) g=9.81; L=0.2; dywdt(1)=w; dywdt(2)=-2*g*y/L; The user-defined functions Sys2ODEsModEu and HW8_22aODEs are used in the following script file to solve the problem and make the plots. % HW 8_22a Solution Script Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 clear all ab=[0 10]; INI=[0.025 0]; h=0.02; [t, y, w] = Sys2ODEsModEu('HW8_22aODEs',ab,h,INI); subplot(2,1,1) plot(t,y) xlabel('Time (s)') ylabel('Position y (m)') subplot(2,1,2) plot(t,w) xlabel('Time (s)') ylabel('dy/dt (m/s)') Answer=[t', y', w']; disp(' Time (s) y(m) dy/dt(m/s)') disp(Answer) When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0200 0.0400 0.0600 0.0800 0.1000 0.1200 0.1400 0.1600 0.1800 0.2000 0.2200 0.2400 0.2600 0.2800 0.3000 0.3200 y(m) 0.0250 0.0245 0.0230 0.0207 0.0175 0.0136 0.0092 0.0044 -0.0006 -0.0056 -0.0103 -0.0146 -0.0183 -0.0214 -0.0235 -0.0248 -0.0250 dy/dt(m/s) 0 -0.0491 -0.0962 -0.1395 -0.1773 -0.2081 -0.2307 -0.2442 -0.2479 -0.2419 -0.2262 -0.2016 -0.1690 -0.1297 -0.0852 -0.0374 0.0120 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 0.3400 -0.0243 0.3600 -0.0226 0.3800 -0.0200 ... ... This answer continues up to 10 s. 0.0609 0.1074 0.1497 ... 0.03 Position y (m) 0.02 0.01 0 −0.01 −0.02 −0.03 0 1 2 3 4 5 Time (s) 6 7 8 9 10 0 1 2 3 4 5 Time (s) 6 7 8 9 10 0.3 0.2 dy/dt (m/s) 0.1 0 −0.1 −0.2 −0.3 −0.4 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 (b) The differential equations dy ------ = w and dw ------- = – 2gy --------- are written in a user-defined function called dt dt L HW8_22bODEs: function [dywdt] = HW8_22bODEs(t,y) g=9.81; L=0.2; dywdt(1,1)=y(2); dywdt(2,1)=-2*g*y(1)/L; The user-defined functions HW8_22bODEs and MATLAB’s built-in function ode45 are used in the following script file to solve the problem and make the plots. % Solution of HW Problem 8.22 b clear all tspan = [0:0.02:10]; yini = [0.025 0]; [Time y] = ode45(@HW8_22bODEs,tspan,yini); subplot(2,1,1) plot(Time,y(:,1)) xlabel('Time (s)') ylabel('Position (m)') subplot(2,1,2) plot(Time,y(:,2)) xlabel('Time (s)') ylabel('dy/dt (m/s)') Answer=[Time, y(:,1), y(:,2)]; disp(' Time (s) y(m) dy/dt(m/s)') disp(Answer) When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0200 0.0400 0.0600 y(m) 0.0250 0.0245 0.0231 0.0207 dy/dt(m/s) 0 -0.0487 -0.0956 -0.1386 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 6 0.0800 0.0176 0.1000 0.0137 0.1200 0.0093 0.1400 0.0046 0.1600 -0.0004 0.1800 -0.0053 0.2000 -0.0100 0.2200 -0.0143 0.2400 -0.0180 0.2600 -0.0211 0.2800 -0.0233 0.3000 -0.0246 0.3200 -0.0250 0.3400 -0.0244 0.3600 -0.0228 0.3800 -0.0203 ... ... This answer continues up to 10 s. -0.1763 -0.2071 -0.2298 -0.2434 -0.2476 -0.2421 -0.2271 -0.2032 -0.1714 -0.1328 -0.0891 -0.0419 0.0070 0.0556 0.1020 0.1444 ... Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 7 0.03 Position (m) 0.02 0.01 0 −0.01 −0.02 −0.03 0 2 4 6 8 10 6 8 10 Time (s) 0.4 dy/dt (m/s) 0.2 0 −0.2 −0.4 0 2 4 Time (s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.23 If the effect of friction is included in the analysis of the U-tube manometer in Problem 8.22, the height of the water level, y, is given by the solution of the equation: 2 d y dy L -------2- = – 0.5 ------ – 2gy dt dt dy Solve the ODE and plot y, and ------ (two separate figures on one page) as a function of t for the first 10 secdt onds. Reduce the second-order ODE to a system of two first-order ODEs and solve the system by: (a) using the user-defined function Sys2ODEsRKclas that was written in Problem 8.16. For step size use 0.02 s. (b) using one of MATLAB’s built-in functions for solving ODEs. Solution To solve the problem the second-order ODE is rewritten as a system of two first-order ODEs. This is done by introducing a new variable w, such that: 2 d y dw w = dy ------ and ------- = -------2dt dt dt With these definitions the system of two first-order ODEs is: dy ------ = w with the initial condition y 0 = 0.025 dt –1 dw ------- = ------ 0.5w + 2gy with the initial condition w 0 = 0 L dt The solution procedure is the same as in Problem 8.22, except that one of the differential equations is different. –1 dw (a) The differential equations dy ------ = w and ------- = ------ 0.5w + 2gy are written in a user-defined function dt dt L called HW8_23abODEs: function [dywdt] = HW8_23aODEs(t,y,w) g=9.81; L=0.2; dywdt(1)=w; dywdt(2)=-(0.5*w+2*g*y)/L; The user-defined functions Sys2ODEsModEu (listed in the solution of Problem 8.22) and HW8_23aODEs are used in the following script file to solve the problem and make the plots. % HW 8_23a Solution Script Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 clear all ab=[0 10]; INI=[0.025 0]; h=0.02; [t, y, w] = Sys2ODEsModEu('HW8_23aODEs',ab,h,INI); subplot(2,1,1) plot(t,y) xlabel('Time (s)') ylabel('Position y (m)') subplot(2,1,2) plot(t,w) xlabel('Time (s)') ylabel('dy/dt (m/s)') Answer=[t', y', w']; disp(' Time (s) y(m) dy/dt(m/s)') disp(Answer) When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0200 0.0400 0.0600 0.0800 0.1000 0.1200 0.1400 0.1600 0.1800 0.2000 0.2200 0.2400 0.2600 0.2800 0.3000 0.3200 y(m) 0.0250 0.0245 0.0231 0.0209 0.0179 0.0144 0.0106 0.0065 0.0024 -0.0017 -0.0054 -0.0088 -0.0116 -0.0139 -0.0155 -0.0165 -0.0167 dy/dt(m/s) 0 -0.0478 -0.0914 -0.1294 -0.1604 -0.1838 -0.1988 -0.2055 -0.2039 -0.1944 -0.1780 -0.1554 -0.1280 -0.0970 -0.0637 -0.0297 0.0038 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 0.3400 -0.0163 0.3600 -0.0153 0.3800 -0.0137 0.4000 -0.0117 ... ... This answer continues up to 10 s. 0.0356 0.0644 0.0892 0.1094 ... 0.03 Position y (m) 0.02 0.01 0 −0.01 −0.02 0 2 4 6 8 10 6 8 10 Time (s) 0.15 0.1 dy/dt (m/s) 0.05 0 −0.05 −0.1 −0.15 −0.2 −0.25 0 2 4 Time (s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 –1 (b) The differential equations dy ------ = w and dw ------- = ------ 0.5w + 2gy are written in a user-defined function dt dt L called HW8_23bODEs: function [dywdt] = HW8_23bODEs(t,y) g=9.81; L=0.2; dywdt(1,1)=y(2); dywdt(2,1)=-(0.5*y(2)+2*g*y(1))/L; The user-defined functions HW8_23bODEs and MATLAB’s built-in function ode45 are used in the following script file to solve the problem and make the plots. % Solution of HW Problem 8.23 b clear all tspan = [0:0.02:10]; yini = [0.025 0]; [Time y] = ode45(@HW8_23bODEs,tspan,yini); subplot(2,1,1) plot(Time,y(:,1)) xlabel('Time (s)') ylabel('Position (m)') subplot(2,1,2) plot(Time,y(:,2)) xlabel('Time (s)') ylabel('dy/dt (m/s)') Answer=[Time, y(:,1), y(:,2)]; disp(' Time (s) y(m) dy/dt(m/s)') disp(Answer) When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0200 0.0400 0.0600 y(m) 0.0250 0.0245 0.0231 0.0209 dy/dt(m/s) 0 -0.0475 -0.0909 -0.1287 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 0.0800 0.0180 0.1000 0.0146 0.1200 0.0107 0.1400 0.0067 0.1600 0.0026 0.1800 -0.0014 0.2000 -0.0052 0.2200 -0.0086 0.2400 -0.0115 0.2600 -0.0138 0.2800 -0.0154 0.3000 -0.0164 0.3200 -0.0168 0.3400 -0.0164 0.3600 -0.0155 0.3800 -0.0140 0.4000 -0.0121 ... ... This answer continues up to 10 s. -0.1598 -0.1833 -0.1986 -0.2055 -0.2044 -0.1954 -0.1795 -0.1574 -0.1305 -0.0998 -0.0669 -0.0330 0.0005 0.0323 0.0613 0.0866 0.1074 ... Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 6 0.03 Position (m) 0.02 0.01 0 −0.01 −0.02 0 2 4 6 8 10 6 8 10 Time (s) 0.2 dy/dt (m/s) 0.1 0 −0.1 −0.2 −0.3 0 2 4 Time (s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.24 Consider forced vibration of a mass–spring system that is shown in the figure. The position x of the mass as a function of time is given by the solution of the equation: 2 F k d x -------- = – ---- x + -----0 cos Zt 2 m m dt where m = 2 kg is the mass, k = 800 N/m is the spring constant, F 0 = 50 N is the amplitude of the applied harmonic force, and Z = 3 rad/s is the frequency of the applied harmonic force. The initial conditions are x 0 = 0.1m, dx -----dt t=0 dx = 0.1 m/s. Solve the ODE for 0 d t d 10 s, and plot x and ------ as a function of t (two separate plots dt on the same page). (a) Use the user-defined function Sys2ODEsRKclas that was written in Problem 8.16. For step size use 0.01 s. (b) Use one of MATLAB’s built-in functions for solving ODEs. Solution To solve the problem the second-order ODE is rewritten as a system of two first-order ODEs. This is done by introducing a new variable u, such that: 2 d x du dx u = ------ and ------ = -------2dt dt dt With these definitions the system of two first-order ODEs is: dx ------ = u with the initial condition x 0 = 0.1 dt – 1 du ------ = ------ kx – F 0 cos Zt with the initial condition w 0 = 0.1 m dt a) The listing of the user-defined function Sys2ODEsRKclas is: function [t, x, y] = Sys2ODEsRKclas(ODEs,ab,h,INI) % Sys2ODEsRKclas solves a system of two first order initial value ODEs using % fourth order Ronge-Kutta method. % The independent variable is t, and the dependent variables are x and y. % Input variables: % ODEs Name (string) of a function file that calculates dx/dt and dy/ Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 dt % The output argument of the function is a vector. its first element is dxdt % and its second element is dydt. %ab A vector with the domain of the solution ab = [a, b]. % h The size of a increment. % INI A vector with the he initial values of x and y [xINI, yINI]. % Output variable: % t A vector with the t coordinate of the solution points. % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. t(1)=ab(1); x(1) = INI(1); y(1) = INI(2); n = round((ab(2) - ab(1))/h);; for i = 1:n t(i+1) = t(i) + h; tm = t(i) + h/2; K1 = feval(ODEs,t(i),x(i),y(i)); K2 = feval(ODEs,tm,x(i)+K1(1)*h/2,y(i)+K1(2)*h/2); K3 = feval(ODEs,tm,x(i)+K2(1)*h/2,y(i)+K2(2)*h/2); K4 = feval(ODEs,t(i+1),x(i)+K3(1)*h,y(i)+K3(2)*h); x(i+1) = x(i) + (K1(1) + 2*K2(1) + 2*K3(1) + K4(1))*h/6; y(i+1) = y(i) + (K1(2) + 2*K2(2) + 2*K3(2) + K4(2))*h/6; end –1 du ------ = u and ------ = ------ kx – F 0 cos Zt The differential equations dx dt dt m are written in a user-defined function called ODEsHW8_24a: function [dxudt] = ODEsHW8_24a(t,x,u) k=800; m=2; F=50; w=3; dxudt(1)=u; dxudt(2)=-(k*x-F*cos(w*t))/m; The user-defined functions Sys2ODEsRKclas and ODEsHW8_24a are used in the following script file to solve the problem and make the plots. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 % HW 8_24a Solution Script clear all ab=[0 10]; INI=[0.1 0.1]; h=0.01; [t, x, u] = Sys2ODEsRKclas('ODEsHW8_24a',ab,h,INI); subplot(2,1,1) plot(t,x) xlabel('Time (s)') ylabel('Position x (m)') subplot(2,1,2) plot(t,u) xlabel('Time (s)') ylabel('dx/dt (m/s)') disp(' Time (s) x(m) dx/dt(m/s)') disp([t',x', u']) When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 x(m) 0.1000 0.1002 0.0990 0.0963 0.0922 0.0869 0.0806 0.0736 0.0661 0.0583 0.0506 0.0433 0.0366 0.0308 0.0261 0.0226 0.0204 dx/dt(m/s) 0.1000 -0.0510 -0.2003 -0.3419 -0.4707 -0.5815 -0.6703 -0.7337 -0.7694 -0.7762 -0.7541 -0.7041 -0.6285 -0.5304 -0.4141 -0.2842 -0.1463 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 0.1700 0.0197 -0.0060 0.1800 0.0203 0.1308 0.1900 0.0222 0.2586 0.2000 0.0254 0.3721 ... ... ... This answer continues up to 10 s. 0.15 Position x (m) 0.1 0.05 0 −0.05 −0.1 0 2 4 6 8 10 6 8 10 Time (s) 1 dx/dt (m/s) 0.5 0 −0.5 −1 0 2 4 Time (s) –1 du ------ = u and ------ = ------ kx – F 0 cos Zt are written in a user-defined function (b) The differential equations dx dt dt m called ODEsHW8_24b: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 function [dxudt] = ODEsHW8_24b(t,x) k=800; m=2; F=50; w=3; dxudt(1,1)=x(2); dxudt(2,1)=-(k*x(1)-F*cos(w*t))/m; The user-defined functions ODEsHW8_24b and MATLAB’s built-in function ode45 are used in the following script file to solve the problem and make the plots. % HW 8_24b Solution Script clear all tspan = [0:0.01:10]; yini = [0.1 0.1]; [t, x] = ode45(@ODEsHW8_24b,tspan,yini); subplot(2,1,1) plot(t,x(:,1)) xlabel('Time (s)') ylabel('Position x (m)') subplot(2,1,2) plot(t,x(:,2)) xlabel('Time (s)') ylabel('dx/dt (m/s)') disp(' Time (s) x(m) disp([t,x(:,1), x(:,2)]) dx/dt(m/s)') When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0100 0.0200 0.0300 0.0400 0.0500 x(m) 0.1000 0.1002 0.0990 0.0963 0.0922 0.0869 dx/dt(m/s) 0.1000 -0.0510 -0.2003 -0.3419 -0.4707 -0.5816 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 6 0.0600 0.0806 0.0700 0.0736 0.0800 0.0660 0.0900 0.0583 0.1000 0.0506 0.1100 0.0433 0.1200 0.0366 0.1300 0.0308 0.1400 0.0261 0.1500 0.0226 0.1600 0.0204 0.1700 0.0197 0.1800 0.0203 0.1900 0.0223 0.2000 0.0254 ... ... This answer continues up to 10 s. -0.6704 -0.7337 -0.7695 -0.7764 -0.7542 -0.7040 -0.6284 -0.5303 -0.4140 -0.2840 -0.1460 -0.0057 0.1313 0.2590 0.3724 ... Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 7 0.15 Position x (m) 0.1 0.05 0 −0.05 −0.1 0 2 4 6 8 10 6 8 10 Time (s) 1 dx/dt (m/s) 0.5 0 −0.5 −1 0 2 4 Time (s) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.25 A capacitor of C = 4.2 u 10 –6 Farads is added in series to the circuit in Problem 8.18. As shown in the figure, the circuit includes a DC voltage source, V 0 = 1000 V, an inductor, L = 15 Henries, and a nonlinear resistor of resistance R = R 0 + R 1 I 2 :, where R 0 = 500: and R 1 = 250:$. The switch is initially open and then is closed at time t = 0 . The charge, Q, in the capacitor for t ! 0 is determined from the solution of the equation: 2 V dQ R dQ 3 Q d Q R ---------- + -----o ------- + -----1 § ------- · + ------- = -----0 2 © ¹ L dt L dt LC L dt dQ Initially, Q = 0 and ------- = 0 . dt (a) Reduce the second-order ODE to a system of two first-order ODEs, and determine the charge, Q, as a function of time for 0 t t t 0.3 s by solving the system using the fourth-order Runge–Kutta method. Use either the user-defined function Sys2ODEsRK4 (Program 8-6) that was developed in Example 88, or the user-defined function Sys2ODEsRKclas that was written in Problem 8.16. Use h = 0.002 s, and plot of Q versus time. (b) Use the results from part (a) to plot the current in the circuit. The current is given by the derivative w.r.t. time of the charge, I = dQ ------- . dt (c) Solve the problem (parts (a) and (b)) using MATLAB’s built-in functions. Solution To solve the problem the second-order ODE is rewritten as a system of two first-order ODEs. This is done by introducing a new variable w, such that: 2 d Q dw dQ w = ------- and ------- = ---------2 dt dt dt With these definitions the system of two first-order ODEs is: dQ ------- = w with the initial condition Q 0 = 0 dt 1 dQ dQ 3 dw ------- = --- § V 0 – R o ------- – R 1 § ------- · – Q ----· with the initial condition w 0 = 0 © © L dt dt ¹ dt C¹ a) The listing of the user-defined function Sys2ODEsRKclas is: function [t, x, y] = Sys2ODEsRK4(ODE1,ODE2,a,b,h,x1,y1) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % Sys2ODEsRK4 solves a system of two first order initial value ODEs using % fourth order Ronge-Kutta method. % The independent variable is t, and the dependent variables are x and y. % Input variables: % ODE1 Name (string) of a function file that calculates dx/dt. % ODE2 Name (string) of a function file that calculates dy/dt. % a The first value of t. % b The last value of t. % h The size of a increment. % x1 The initial value of x. % y1 The initial value of y. % Output variable: % t A vector with the t coordinate of the solution points. % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. t(1)=a; x(1) = x1; y(1) = y1; n = (b-a)/h; for i = 1:n t(i+1) = t(i) + h; tm = t(i) + h/2; Kx1 = feval(ODE1,t(i),x(i),y(i)); Ky1 = feval(ODE2,t(i),x(i),y(i)); Kx2 = feval(ODE1,tm,x(i)+Kx1*h/2,y(i)+Ky1*h/2); Ky2 = feval(ODE2,tm,x(i)+Kx1*h/2,y(i)+Ky1*h/2); Kx3 = feval(ODE1,tm,x(i)+Kx2*h/2,y(i)+Ky2*h/2); Ky3 = feval(ODE2,tm,x(i)+Kx2*h/2,y(i)+Ky2*h/2); Kx4 = feval(ODE1,t(i+1),x(i)+Kx3*h,y(i)+Ky3*h); Ky4 = feval(ODE2,t(i+1),x(i)+Kx3*h,y(i)+Ky3*h); x(i+1) = x(i) + (Kx1 + 2*Kx2 + 2*Kx3 + Kx4)*h/6; y(i+1) = y(i) + (Ky1 + 2*Ky2 + 2*Ky3 + Ky4)*h/6; end Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 1 dQ 3 Q dQ dw The differential equations dQ ------- = w and ------- = --- § V 0 – R o ------- – R 1 § ------- · – ----· are written in a user-defined © ¹ © ¹ dt dt L dt dt C functions called HW8_25adQdt and HW8_25adwdt: function [dQdt] = HW8_25adQdt(t,Q,w) dQdt=w; function [dwdt] = HW8_25adwdt(t,Q,w) C=4.2E-6; V=1000; L=15; R0=500; R1=250; dwdt=(V-R0*w-R1*w^3-Q/C)/L; The user-defined functions HW8_25adQdt, HW8_25adwdt, and Sys2ODEsRK4 are used in the following script file to solve the problem and make the plots. The last four lines in the program solve part (b) of the problem. % Solution of HW Problem 8.25 a clear all [t, Q, w] = Sys2ODEsRK4('HW8_25adQdt','HW8_25adwdt',0,0.3,0.002,0,0); plot(t,Q) xlabel('Time (s)') ylabel('Q (C)') Time (s) Q(C)') disp(' disp([t', Q']) % Solving part (b) of the problem. figure plot(t,w) xlabel('Time (s)') ylabel('I (A)') When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) Q(C) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 0 0 0.0020 0.0001 0.0040 0.0005 0.0060 0.0011 0.0080 0.0018 0.0100 0.0026 0.0120 0.0035 0.0140 0.0043 0.0160 0.0051 0.0180 0.0058 0.0200 0.0063 0.0220 0.0067 0.0240 0.0069 0.0260 0.0069 0.0280 0.0067 0.0300 0.0064 0.0320 0.0060 0.0340 0.0055 0.0360 0.0050 0.0380 0.0044 0.0400 0.0039 0.0420 0.0034 0.0440 0.0030 0.0460 0.0027 0.0480 0.0025 0.0500 0.0025 ... ... This answer continues up to 0.3 s. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 −3 7 x 10 6 5 Q (C) 4 3 2 1 0 0 0.05 0.1 0.15 0.2 Time (s) 0.25 0.3 0.35 ------- is actually the variable w in the solution in part (a). The program in part (a), cre(b) The current I = dQ dt ates also the following plot of w versus t. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 6 0.5 0.4 0.3 I (A) 0.2 0.1 0 −0.1 −0.2 −0.3 0 0.05 0.1 0.15 0.2 Time (s) 0.25 0.3 0.35 1 dQ 3 Q dQ dw ------- = w and ------- = --- § V 0 – R o ------- – R 1 § ------- · – ----· are written in a user(b) The differential equations dQ © ¹ © ¹ dt defined function called HW8_25bODEs: dt L dt dt C function [dQdt] = HW8_25bODEs(t,Q) C=4.2E-6; V=1000; L=15; R0=500; R1=250; dQdt(1,1)=Q(2); dQdt(2,1)=(V-R0*Q(2)-R1*Q(2)^3-Q(1)/C)/L; The user-defined functions HW8_25bODEs and MATLAB’s built-in function ode45 are used in the following script file to solve the problem and make the plots. % Solution of HW Problem 8.25 b Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 7 clear all tspan = [0:0.002:0.3]; yini = [0 0]; [t, Q] = ode45(@HW8_25bODEs,tspan,yini); plot(t,Q(:,1)) xlabel('Time (s)') ylabel('Q (C)') disp(' Time (s) Q(C)') disp([t, Q(:,1)]) % Solving part (b) of the problem. figure plot(t,Q(:,2)) xlabel('Time (s)') ylabel('I (A)') When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. Time (s) 0 0.0020 0.0040 0.0060 0.0080 0.0100 0.0120 0.0140 0.0160 0.0180 0.0200 0.0220 0.0240 0.0260 0.0280 0.0300 Q(C) 0 0.0001 0.0005 0.0011 0.0018 0.0026 0.0035 0.0043 0.0051 0.0058 0.0063 0.0067 0.0069 0.0069 0.0067 0.0064 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 8 0.0320 0.0060 0.0340 0.0055 0.0360 0.0050 0.0380 0.0044 0.0400 0.0039 0.0420 0.0034 0.0440 0.0030 0.0460 0.0027 0.0480 0.0025 0.0500 0.0025 ... ... This answer continues up to 0.3 s. −3 7 x 10 0.5 0.4 6 0.3 5 0.2 I (A) Q (C) 4 0.1 3 0 2 −0.1 1 0 −0.2 0 0.05 0.1 0.15 0.2 Time (s) 0.25 0.3 0.35 −0.3 0 0.05 0.1 0.15 0.2 Time (s) 0.25 0.3 0.35 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 8.26 A paratrooper jumps from an aircraft in straight and level flight. The motion of the paratrooper is approximately described by the following system of equations: 2 J dx dx 2 dy 2 d x --------- = – ---- § ------ · § ------ · + § ------ · © dt ¹ m © dt ¹ © dt ¹ dt 2 2 J § dy · § dx · 2 § dy · 2 d--------y- = – g – --- ----------- + -----© dt ¹ m © dt ¹ © dt ¹ dt 2 where x and y are the paratrooper’s position according to the coordinate system shown in the figure. For m = 80 kg, g = 9.81 m/s2, J = 5.38 Ns2/m2, and initial conditions: x 0 = 0, y 0 = 0, dx -----dt = 134m/s, t=0 dy -----dt = 0 t=0 Determine and plot the trajectory of the paratrooper for the first 5s. Reduce the system of two second-order ODE to a system of four first-order ODEs and solve using MATLAB’s built-in functions. Solution To solve the problem the system of two second-order ODEs is rewritten as a system of four first-order ODEs. This is done by introducing two new variables u, and w such that: 2 2 dy du dw d x d y u = dx ------ , w = ------ and ------ = -------2- , ------- = -------2dt dt dt dt dt dt With these definitions the system of two first-order ODEs is: dx ------ = u with the initial condition x 0 = 0 dt J 2 2 du ------ = – ---- u u + w with the initial condition u 0 = 134 m dt dy ------ = w with the initial condition y 0 = 0 dt J 2 2 dw ------- = – g – ---- w u + w with the initial condition w 0 = 0 m dt The differential equations are written in a user-defined function called HW8_26ODEs: function dxydt = HW8_26ODEs(t,xy) m=80; g=9.81; gam=5.38; dxydt(1,1) = xy(2); dxydt(2,1) = -(gam/m)*xy(2)*sqrt((xy(2)^2)+(xy(4)^2)); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 dxydt(3,1) = xy(4); dxydt(4,1) = -g-(gam/m)*xy(4)*sqrt((xy(2)^2)+(xy(4)^2)); The user-defined functions HW8_26ODEs and MATLAB’s built-in function ode45 are used in the following script file to solve the problem and make the plots. % Solution of Problem 8.26. clear all tspan = [0 5]; xy1(1) = 0; xy1(2) = 134; xy1(3) = 0; xy1(4) = 0; [T,Y] = ode45('HW8_26ODEs',tspan,xy1); plot(Y(:,1),Y(:,3)) xlabel('x (m)'); ylabel('y (m)') disp(' time (s) x (m) disp([T, Y(:,2), Y(:,4)]) y (m)') When the script file is executed the following results are displayed in the Command Window, and the figure that follows is displayed in the Figure Window. time (s) 0 3.7491e-007 7.4982e-007 1.1247e-006 1.4996e-006 3.3742e-006 5.2487e-006 7.1233e-006 8.9978e-006 1.8371e-005 2.7743e-005 3.7116e-005 x (m) 1.3400e+002 1.3400e+002 1.3400e+002 1.3400e+002 1.3400e+002 1.3400e+002 1.3399e+002 1.3399e+002 1.3399e+002 1.3398e+002 1.3397e+002 1.3396e+002 y (m) 0 -3.6778e-006 -7.3557e-006 -1.1033e-005 -1.4711e-005 -3.3100e-005 -5.1489e-005 -6.9877e-005 -8.8265e-005 -1.8020e-004 -2.7213e-004 -3.6405e-004 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 4.6489e-005 9.3352e-005 1.4022e-004 1.8708e-004 2.3394e-004 4.6826e-004 7.0258e-004 9.3690e-004 1.1712e-003 2.3428e-003 3.5144e-003 4.6860e-003 5.8576e-003 1.1716e-002 1.7573e-002 2.3431e-002 2.9289e-002 4.1111e-002 5.2933e-002 6.4755e-002 7.6576e-002 9.3060e-002 1.0954e-001 1.2603e-001 1.4251e-001 1.6502e-001 1.8753e-001 2.1004e-001 2.3255e-001 2.6323e-001 2.9391e-001 3.2459e-001 3.5528e-001 3.9703e-001 4.3879e-001 4.8055e-001 5.2231e-001 1.3394e+002 1.3389e+002 1.3383e+002 1.3377e+002 1.3372e+002 1.3344e+002 1.3316e+002 1.3288e+002 1.3260e+002 1.3123e+002 1.2989e+002 1.2857e+002 1.2728e+002 1.2120e+002 1.1568e+002 1.1064e+002 1.0602e+002 9.7772e+001 9.0721e+001 8.4625e+001 7.9294e+001 7.2883e+001 6.7435e+001 6.2751e+001 5.8673e+001 5.3883e+001 4.9819e+001 4.6330e+001 4.3295e+001 3.9740e+001 3.6726e+001 3.4139e+001 3.1891e+001 2.9261e+001 2.7031e+001 2.5118e+001 2.3453e+001 -4.5596e-004 -9.1540e-004 -1.3746e-003 -1.8337e-003 -2.2926e-003 -4.5840e-003 -6.8706e-003 -9.1525e-003 -1.1430e-002 -2.2745e-002 -3.3947e-002 -4.5038e-002 -5.6022e-002 -1.0944e-001 -1.6061e-001 -2.0982e-001 -2.5733e-001 -3.4878e-001 -4.3540e-001 -5.1818e-001 -5.9785e-001 -7.0469e-001 -8.0765e-001 -9.0758e-001 -1.0050e+000 -1.1348e+000 -1.2616e+000 -1.3863e+000 -1.5090e+000 -1.6737e+000 -1.8363e+000 -1.9972e+000 -2.1567e+000 -2.3717e+000 -2.5849e+000 -2.7969e+000 -3.0076e+000 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 5.7914e-001 6.3598e-001 6.9281e-001 7.4964e-001 8.2709e-001 9.0453e-001 9.8197e-001 1.0594e+000 1.1653e+000 1.2713e+000 1.3772e+000 1.4831e+000 1.6081e+000 1.7331e+000 1.8581e+000 1.9831e+000 2.1081e+000 2.2331e+000 2.3581e+000 2.4831e+000 2.6081e+000 2.7331e+000 2.8581e+000 2.9831e+000 3.1081e+000 3.2331e+000 3.3581e+000 3.4831e+000 3.6081e+000 3.7331e+000 3.8581e+000 3.9831e+000 4.1081e+000 4.2331e+000 4.3581e+000 4.4831e+000 4.6123e+000 2.1505e+001 1.9851e+001 1.8429e+001 1.7190e+001 1.5732e+001 1.4489e+001 1.3413e+001 1.2469e+001 1.1344e+001 1.0373e+001 9.5215e+000 8.7630e+000 7.9636e+000 7.2497e+000 6.6062e+000 6.0218e+000 5.4887e+000 5.0012e+000 4.5547e+000 4.1456e+000 3.7707e+000 3.4275e+000 3.1134e+000 2.8263e+000 2.5643e+000 2.3253e+000 2.1075e+000 1.9094e+000 1.7292e+000 1.5654e+000 1.4168e+000 1.2820e+000 1.1597e+000 1.0489e+000 9.4850e-001 8.5761e-001 7.7268e-001 -3.2922e+000 -3.5749e+000 -3.8561e+000 -4.1354e+000 -4.5123e+000 -4.8851e+000 -5.2536e+000 -5.6166e+000 -6.1021e+000 -6.5737e+000 -7.0295e+000 -7.4667e+000 -7.9553e+000 -8.4126e+000 -8.8366e+000 -9.2261e+000 -9.5804e+000 -9.9005e+000 -1.0187e+001 -1.0442e+001 -1.0668e+001 -1.0866e+001 -1.1040e+001 -1.1191e+001 -1.1321e+001 -1.1434e+001 -1.1531e+001 -1.1615e+001 -1.1686e+001 -1.1747e+001 -1.1799e+001 -1.1842e+001 -1.1880e+001 -1.1911e+001 -1.1938e+001 -1.1960e+001 -1.1980e+001 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 4.7415e+000 4.8708e+000 5.0000e+000 6.9610e-001 -1.1996e+001 6.2704e-001 -1.2010e+001 5.6480e-001 -1.2021e+001 0 −5 −10 y (m) −15 −20 −25 −30 −35 −40 −45 0 10 20 30 x (m) 40 50 60 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.1 Consider the following second-order ODE: 2 d y --------- = y + x x – 4 dx 2 (a) Using the central difference formula for approximating the second derivative, discretize the ODE (rewrite the equation in a form suitable for solution with the finite difference method). (b) If h = 1 , what is the value of the diagonal elements in the resulting matrix of coefficients of the system of linear equations that has to be solved? Solution (a) The central difference formula for the second derivative at the point x = xi is given by 2 y i – 1 – 2y i + y i + 1 d y - . Substituting this into the ODE yields: -------- = --------------------------------------2 2 dx h y i – 1 – 2y i + y i + 1 ---------------------------------------- = y i + x i x i – 4 2 h Collecting like terms gives: 2 yi – 1 – 2 + h yi + yi + 1 = xi xi – 4 h 2 which is the discretized form of the ODE. (b) The discretized form of the ODE obtained in part (a) is a tridiagonal system of equations with the ys as the unknowns. The diagonal elements are the coefficients of the y i terms: – 2 + h 2 . For h = 1 , the diagonal elements are equal to -3. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.2 Consider the following second-order ODE: 2 d y y --------- + -- = C dr 2 r where C is a constant. (a) Using the central difference formula for approximating the second derivative, discretize the ODE (rewrite the equation in a form suitable for solution with the finite difference method). (b) If h = 1 , what is the value of the diagonal elements in the resulting matrix of coefficients of the system of linear equations that has to be solved? Solution (a) The central difference formula for the second derivative at the point x = xi is given by 2 y i – 1 – 2y i + y i + 1 d y -------- = --------------------------------------- . Substituting this into the ODE yields: 2 2 h dx y i – 1 – 2y i + y i + 1 y i ---------------------------------------- + ---- = C 2 ri h Collecting like terms gives: 2 2 r i y i – 1 + h – 2r i y i + r i y i + 1 = Ch r i which is the discretized form of the ODE. (b) The discretized form of the ODE obtained in part (a) is a tridiagonal system of equations with the ys as the unknowns. The diagonal elements are the coefficients of the y i terms: h 2 – 2r i . For h = 1, the diagonal elements are equal to 1 – 2r i . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.3 Consider the following second-order ODE: 2 dy d y --------- + x ------ + y = 2x for 0 d x d 1 , with y 0 = 1 and y 1 = 1 2 dx dx (a) Using the central difference formulas for approximating the derivatives, discretize the ODE (rewrite the equation in a form suitable for solution with the finite difference method). (b) What is the expression for the above-diagonal terms in the resulting matrix of coefficients of the tridiagonal system of linear equations that has to be solved? Solution (a) The central difference formula for the second derivative at the point x = x i is given by 2 y i – 1 – 2y i + y i + 1 d y - . The central difference formula for the first derivative at the point x = x i is given -------- = --------------------------------------2 2 dx h yi + 1 – yi – 1 ------ = -------------------------by dy . Substituting these into the ODE yields: dx 2h yi + 1 – yi – 1 y i – 1 – 2y i + y i + 1 ---------------------------------------- + x i § --------------------------· + y i = 2x i 2 © ¹ 2h h Collecting like terms, the discretized form of the ODE is: 2 2 – hx i y i – 1 + 2h – 4 y i + 2 + hx i y i + 1 = 4x i h 2 This forms the following system of equations: 2 – hx 2 2h – 4 2 + hx 2 0 } 0 0 4x 2 h 2 y1 0 2 – hx 3 2h – 4 2 2 + hx 3 } 0 0 y2 4x 3 h 2 0 0 2 – hx 4 2h – 4 2 2 + hx 4 } 0 y3 4x 4 h 2 0 } 0 } 0 } } } } } } } 0 } } yn – 1 } } } } } 2 – hx n – 1 2h – 4 2 2 + hx n – 1 0 yn 4x n – 1 h 0 0 } 0 2 – hx n 2h – 4 2 2 + hx n 2 yn + 1 = 4x n h 2 2 Note that y 1 = y 0 = 1 and y n + 1 = y 1 = 1 . Substituting into the above system and rewriting the first and last equations (rows) yields the following tridiagonal system of equations: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 2h – 4 2 2 + hx 2 2 – hx 3 2 2h – 4 2 + hx 3 0 } 0 0 0 2 – hx 4 2 2h – 4 2 + hx 4 0 } 0 } } 0 0 } } } } } } } } 0 0 0 } } 0 2 – hx n – 1 2h – 4 2 2 + hx n – 1 2 – hx n 2 0 0 0 } } 0 0 0 0 2h – 4 y2 2 y3 4x 2 h – 2 + hx 2 y4 4x 3 h 2 4x 4 h 2 y5 } yn – 2 yn – 1 yn = } 4x n – 1 h 2 2 4x n h – 2 – hx n (b) From part (a) above, it can be seen from the resulting tridiagonal matrix that the above-diagonal terms are: 2 + hx i , where i = 2 3 }n – 1 . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.4 Consider the following boundary value problem. 2 d y --------- + ay + by 4 = 0 for 0 d x d 1 , with the boundary conditions: dy -----dx 2 dx = 0 and y 1 = 1 x=0 where a and b are constants. Discretize the second-order ODE using: (a) Second-order accurate forward difference. (b) Second-order accurate backward difference. (c) Discretize the boundary condition at x = 0 using the second-order accurate forward difference. Solution (a) The second-order accurate forward difference formula for the second derivative is the four-point for2f x – 5f x + 4f x –f x i i+1 i+2 i+3 mula given in Table 6-1, f cc x i = ------------------------------------------------------------------------------------------, which in the present case would 2 h 2 2y i – 5y i + 1 + 4y i + 2 – y i + 3 be written as: d-------y2- = --------------------------------------------------------------- . Substituting into the ODE yields the following discretized 2 dx h form: 2y i – 5y i + 1 + 4y i + 2 – y i + 3 4 ---------------------------------------------------------------- + ay i + by i = 0 2 h Note that y n + 1 = y 1 = 1 . (b) The second order backward difference formula for the second derivative is the four-point formula – f x i – 3 + 4f x i – 2 – 5f x i – 1 + 2f x i - , which in the present case would be written as f cc x i = ----------------------------------------------------------------------------------------------2 h 2 – y i – 3 + 4 y i – 2 – 5y i – 1 + 2y i d y - . Substituting into the ODE yields the following discretized form: -------- = ------------------------------------------------------------------2 2 dx h – y i – 3 + 4 y i – 2 – 5y i – 1 + 2y i 4 ------------------------------------------------------------------- + ay i + by i = 0 2 h (c) The boundary condition at x = 0 discretized using a second-order accurate difference formula, i.e. the -----three-point forward difference formula, yields dy dx x=0 – 3y 1 + 4y 2 – y 3 = ------------------------------------- = 0 . Since y 1 = y 0 , is 2h unknown, the latter equation can be solved to yield: y 4y y 1 = ----3 – -------23 3 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 Consider the following boundary value problem. 9.5 3 d y dy --------- + ------ – y 4 = 0 for 0 d x d 1 , with the boundary conditions: y 0 = 0 , dy -----dx 3 dx dx = 0 and y 1 = 10 . x=0 Discretize the third-order ODE using second-order central differences. When the boundary conditions are discretized, make sure that the order of the truncation error is compatible with that of the ODE. Solution The second-order accurate central difference formula for the third derivative is given in Table 6-1 as – f x i – 2 + 2f x i – 1 – 2f x i + 1 + f x i + 2 f ccc x i = -------------------------------------------------------------------------------------------------- , which in the present case may be written as 3 2h 3 – y i – 2 + 2y i – 1 – 2y i + 1 + y i + 2 d y -------- = ----------------------------------------------------------------------. Similarly, the second order accurate central difference formula for the 3 3 dx 2h yi + 1 – yi – 1 ------ = -------------------------first derivative is dy . Substituting these into the ODE yields the following discretized equadx 2h tion: – y i – 2 + 2y i – 1 – 2y i + 1 + y i + 2 y i + 1 – y i – 1 4 ----------------------------------------------------------------------+ -------------------------- – y i = 0 3 2h 2h Taking y 1 = y 0 = 0 and y n + 1 = y 1 = 10 , the only boundary condition that must be discretized is y2 – y0 dy -----= 0 . If the second order accurate central difference formula is used, --------------= 0 or y 0 = y 2 , where dx x = 0 2h y 0 is an additional point that is introduced outside the original domain. Note that y 0 will appear in the equation relating y 0 y 1 y 2 y 3 and y 4 . But, this is not useful because it introduces another unknown without providing another equation. Consequently, the derivative boundary condition must be discretized using -----the second order accurate forward difference formula dy dx x=0 – 3y 1 + 4y 2 – y 3 4y 2 – y 3 - = -----------------= ------------------------------------- = 0 or 2h 2h 4y 2 – y 3 = 0 . Incorporating these boundary conditions, the system of equations that results from the dis- cretized form of the ODE is: 4y 2 – y 3 = 0 – y 1 + 2y 2 – 2y 4 + y 5 y 4 – y 2 4 ------------------------------------------------+ --------------- – y 3 = 0 3 2h 2h – y i – 2 + 2y i – 1 – 2y i + 1 + y i + 2 y i + 1 – y i – 1 4 ----------------------------------------------------------------------- + -------------------------- – y i = 0 for i = 4 5 } n – 2 3 2h 2h Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 – y n – 3 + 2y n – 2 – 2y n + y n + 1 y n – y n – 2 4 -------------------------------------------------------------------- + ---------------------- – y n – 1 = 0 3 2h 2h Substituting for y 1 and y n + 1 yields the following simplified system of equations: 4y 2 – y 3 = 0 2y 2 – 2y 4 + y 5 y 4 – y 2 4 --------------------------------- + --------------- – y 3 = 0 3 2h 2h – y i – 2 + 2y i – 1 – 2y i + 1 + y i + 2 y i + 1 – y i – 1 4 ----------------------------------------------------------------------- + -------------------------- – y i = 0 for i = 4 5 } n – 2 3 2h 2h – y n – 3 + 2y n – 2 – 2y n + 10 y n – y n – 2 4 -------------------------------------------------------------- + ---------------------- – y n – 1 = 0 3 2h 2h This is a system of n – 1 equations in the n – 1 unknowns y 2 through y n . Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 Consider the following boundary value problem. 9.6 2 d y 1--- dy --------- + ------ = – Q for 0 d x d 1 , with the boundary conditions: dy -----dx 2 x dx dx dy = 0 and -----dx x=0 = ay 1 + by 4 1 . x=1 where a and b are constants. Discretize the ODE using second-order accurate central differences for the derivatives. When the boundary conditions are discretized, make sure that the order of the truncation error is compatible with that of the ODE. Solution Using second order accurate central differences, y i – 1 – 2y i + y i + 1 1 § y i + 1 – y i – 1· ---------------------------------------- + ---- -------------------------- = – Q 2 ¹ xi © 2h h Collecting like terms, 2 2x i – h y i – 1 – 4x i y i + 2x i + h y i + 1 = – 2Qh x i Note that the entire equation has been multiplied by x i to accommodate the singularity at x = 0 . The boundary conditions are also discretized using the one-sided, second-order accurate difference formulae given in Table 6-1: – 3y 1 + 4y 2 – y 3 -------------------------------------- = 0 2h where y 1 is the value of y at x = 0 , y 2 is the value of y at x = h , and y 3 is the value of y at x = 2h . This boundary condition can be simplified to: 4y 2 – y 3 y 1 = -----------------3 The second order backward difference formula is used for the other boundary condition: y N – 2 – 4y N – 1 + 3y N 4 ----------------------------------------------- = ay N + by N 2h Collecting terms, 4 y N – 2 – 4y N – 1 + 3 – 2ah y N – 2hby N = 0 Note that since y N is unknown, and the associated boundary condition at x = x N is nonlinear, the solution methods described in Chapter 3 will have to be used in solving this set of equations. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.7 Consider the following boundary value problem. 2 d u – --------- + S 2 u = 2S 2 cos Sx for 0 d x d 1 , with the boundary conditions: u 0 = 1 and u 1 = – 1 dx 2 What are the diagonal elements of the resulting tridiagonal matrix when the finite difference method with first-order accurate central differences is applied to solve the problem with a step size of 1/8? Solution The discretized form of the ODE is: u i – 1 – 2u i + u i + 1· 2 + S u i = 2S 2 cos Sx i – § ----------------------------------------2 © ¹ h Multiplying through by h 2 and collecting like terms yields: 2 2 2 – u i – 1 + 2 + h S u i – u i + 1 = 2S 2 h cos Sx i 2 The diagonal elements of this tridiagonal system are 2 + h 2 S . For a step size of h = 1--- , the diagonal ele- 8 2 S · ments are §© 2 + ----- = 2.154213 . ¹ 64 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.8 Consider the second-order ODE of the form: 2 dy d y -------- + p ------ + qy = r x dx dx 2 where p and q are constants, and r x is a given function. Using second-order accurate central differences for the derivatives, discretize the ODE. Solution Using Table 6-1, y i + 1 – y i – 1· y i – 1 – 2y i + y i + 1 ---------------------------------------- + p § -------------------------+ qy i = r x i 2 © ¹ 2h h Multiplying through by h 2 and collecting like terms, 2 2 2 – ph y i – 1 + 2 + ph y i + 1 + 2qh – 4 y i = 2h r x i Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.9 Write a user-defined MATLAB function that solves with the shooting method, a second-order boundary value problem of the form: 2 d y- + f x dy ------ + g x y = h x ------2 dx dx for a d x d b with y a = Y a and y b = Y b where Y a and Y b are constants. The function should first calculate two solutions using two assumed values for the slope at x = a , which are specified by the user, and use these solutions for calculating a new initial slope using interpolation (Eq. (9.14)), which is then used for calculating the final solution of the problem. Name the function [x,y]=BVPShootInt(fOFx,gOFx,hOFx,a,b,n,Ya,Yb,WL,WH), where fOFx, gOFx, and hOFx are the names (strings) of the user-defined functions that calculate f x , g x and h x , respectively, a and b define the domain of the solution, n is the number of subintervals, Ya and Yb are the boundary conditions, and WL and WH are the assumed slopes at x = a . Once the first two solutions are calculated, the program should confirm that at x = b the given boundary condition Y b is between the two solutions, and then calculate the final solution with the interpolated value for the slope. If the boundary condition at x = b is not between the first two solutions, the program should stop and display an error message. Use the user-defined function Sys2ODEsRK4 that was written in Problem 8.21 for solving the system of the two first-order ODEs within the user-defined function BVPShootInt. Use BVPShootInt to solve the boundary value problem in Example 9-6. Use n = 100 , W L = – 5 , and W H = – 1.5 . Solution First, the second order ODE is written as a system of two first order ODEs: dy ------ = z dx dz- = h x – g x y – f x z ----dx The system is solved as an initial value problem with y a = Y a and z a must be guessed to be of the correct value so that y b = Y b is satisfied upon integration of the ODE. The listing of the user-defined function BVPShootInt is: function [x,y]=BVPShootInt(fOFx,gOFx,hOFx,a,b,n,Ya,Yb,WL,WH) %The function solves a general second order linear ODE of the form: % d2y/dx2 + f(x)(dy/dx) + g(x)y = h(x) %By defining dy/dx = z, this ODE can be reduced to 2 first order ODEs %of the form: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % dy/dx = z % dz/dx = h(x) - g(x)y - f(x)z % %The function uses the user-defined function Sys2ODEsRK4 (Program 8-6) % to solve the system. %Input arguments: % fOFx Name of a user-defined function that calculates f(x). % gOFx Name of a user-defined function that calculates g(x). % hOFx Name of a user-defined function that calculates h(x). % a First value of x (first point in the domain). % b Last value of x (last point in the domain). % n Number of subintervals. % Ya Boundary condition at x=a. % Yb Boundary condition at x=b. % WL Assumed slope (dy/dx) at x=a. % WH Assumed slope (dy/dx) at x=a. %Output arguments: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. h=(b-a)/n; [x YL zL] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WL); [x YH zH] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WH); iLast=length(x); % Checking if Yb is between the two solutions. if (Yb < YL(iLast) & Yb < YH(iLast)) | (Yb > YH(iLast) & Yb > YL(iLast)) disp('ERROR: The boundary condition at x=b is not between the two guessed solutions') stop end Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 % Use Eq.(9.14): WN=WL+((Yb-YL(iLast))*(WH-WL)/(YH(iLast)-YL(iLast))); [x y zN] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WN); function dydz=ODE1(x,y,z) dydz=z; end function dzdx=ODE2(x,y,z) dzdx=hOFx(x)-gOFx(x)*y-fOFx(x)*z; end end Note that the user-defined function BVPShootInt has two nested functions. One, called ODE1, which caldz culates dy ------ = z , and the second, called ODE2, which calculates ------ = h x – g x y – f x z . dx dx The problem in Example 9-6 is: 2 dy d y -------2- + 2x ------ + 5y – cos 3x = 0 , dx dx for 0dxdS (1.1) with the boundary conditions: y 0 = 1.5 and y S = 0 . In the notation used in the present problem: f x = 2x , g x = 5 , and h x = cos 3x . In order to solve the problem, the following three user-defined functions that calculate f x , g x and h x are created: function fx=fOFx(x) fx=2*x; function gx=gOFx(x) gx=5; function hx=hOFx(x) hx=cos(3*x); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 The following program (script file) solves the problem: % Solution of Problem 9.9 clear all a=0; b=pi; n=100; Ya=1.5; Yb=0; WL=-5; WH=-1.5; [x,y]=BVPShootInt(@fOFx,@gOFx,@hOFx,a,b,n,Ya,Yb,WL,WH); plot(x,y) xlabel('x'); ylabel('y') When the script file is executed, the following figure is displayed in the Figure Window: 1.5 1 y 0.5 0 −0.5 −1 0 0.5 1 1.5 2 2.5 3 3.5 x This solution agrees with the solution in Example 9-6. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.10 Write a user-defined MATLAB function that solves with the shooting method in conjunction with the bisection method, a second-order boundary value problem of the form: 2 dy d y -------- + f x ------ + g x y = h x for a d x d b with y a = Y a and y b = Y b 2 dx dx and Y b are constants. For the function name and arguments where Y a use [x,y]=BVPShootBisec(fOFx,gOFx,hOFx,a,b,n,Ya,Yb,WL,WH), where fOFx, gOFx, and hOFx are the names (strings) of the user-defined functions that calculate f x , g x and h x , respectively, a and b define the domain of the solution, n is the number of subintervals, Ya and Yb are the boundary conditions, and WL and WH are the assumed slopes at x = a that are used in the first two solutions. Once the first two solutions are calculated, the program should confirm that at x = b the value of the boundary condition Y b is between the first two solutions. If the boundary condition is not between the first the two solutions, the program should stop and display an error message. Within the user-defined function BVPShootBisec, use the user-defined function Sys2ODEsRK4 that was written in Example 8-8 for solving the system of the two first-order ODEs. Stop the iterations when the absolute value of the true error at x = b is smaller than 0.001. Use BVPShootBisec to solve the boundary value problem in Example 9-6. Use n = 100 , W L = – 5 , and W H = – 1.5 . Solution First, the second order ODE is written as a system of two first order ODEs: dy ------ = z dx dz ------ = h x – g x y – f x z dx The system is solved as an initial value problem with y a = Y a and z a must be guessed to be of the correct value so that y b = Y b is satisfied upon integration of the ODE. The listing of the user-defined function BVPShootBisec is: function [x,Yi]=BVPShootBisec(fOFx,gOFx,hOFx,a,b,n,Ya,Yb,WL,WH) %The function solves a general second order linear ODE of the form: % d2y/dx2 + f(x)(dy/dx) + g(x)y = h(x) %By defining dy/dx = z, this ODE can be reduced to 2 first order ODEs %of the form: % dy/dx = z % dz/dx = h(x) - g(x)y - f(x)z Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % %The function uses the shooting method in conjuction with the bisection %method, and uses the user-defined function Sys2ODEsRK4 (Program 8-6) %to solve the system. %Input arguments: % fOFx Name of a user-defined function that calculates f(x). % gOFx Name of a user-defined function that calculates g(x). % hOFx Name of a user-defined function that calculates h(x). % a First value of x (first point in the domain). % b Last value of x (last point in the domain). % n Number of subintervals. % Ya Boundary condition at x=a. % Yb Boundary condition at x=b. % WL Assumed slope (dy/dx) at x=a. % WH Assumed slope (dy/dx) at x=a. %Output arguments: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. h=(b-a)/n; [x YL zL] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WL); [x YH zH] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WH); iLast=length(x); % Checking if Yb is between the two solutions. if (Yb < YL(iLast) & Yb < YH(iLast)) | (Yb > YH(iLast) & Yb > YL(iLast)) disp('ERROR: The boundary condition at x=b is not between the two guessed solutions') else % Start iterations using the bisection method imax=15; tol=0.001; for i = 1:imax + 1 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 Wi = (WH + WL)/2; [x Yi zi] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,Wi); E = Yi(iLast) - Yb; if abs(E) < tol break end if YL(iLast)*Yi(iLast) > 0 WL = Wi; YL(iLast)=Yi(iLast); else WH = Wi; YH(iLast)=Yi(iLast); end end if i > imax fprintf('Solution was not obtained in %i iterations.\n',imax) end end function dydx=ODE1(x,y,z) dydx=z; end function dzdx=ODE2(x,y,z) dzdx=hOFx(x)-gOFx(x)*y-fOFx(x)*z; end end Note that the user-defined function BVPShootBisect has two nested functions. One, called ODE1, which dz- = h x – g x y – f x z . calculates dy ------ = z , and the second, called ODE2, which calculates ----dx dx The problem in Example 9-6 is: 2 d y dy -------- + 2x ------ + 5y – cos 3x = 0 , 2 dx dx for 0dxdS (1.1) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 with the boundary conditions: y 0 = 1.5 and y S = 0 . In the notation used in the present problem: f x = 2x , g x = 5 , and h x = cos 3x . In order to solve the problem, the following three user-defined functions that calculate f x , g x and h x are created: function fx=fOFx(x) fx=2*x; function gx=gOFx(x) gx=5; function hx=hOFx(x) hx=cos(3*x); The following program (script file) solves the problem: % Solution of Problem 9.10 clear all a=0; b=pi; n=100; Ya=1.5; Yb=0; WL=-5; WH=-1.5; [x,y]=BVPShootBisec(@fOFx,@gOFx,@hOFx,a,b,n,Ya,Yb,WL,WH); plot(x,y) xlabel('x'); ylabel('y') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 When the script file is executed, the following figure is displayed in the Figure Window: 1.5 1 y 0.5 0 −0.5 −1 0 0.5 1 1.5 2 2.5 3 3.5 x This solution agrees with the solution in Example 9-6. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.11 Write a user-defined MATLAB function that solves, with the shooting method in conjunction with the secant method, a second-order boundary value problem of the form: 2 dy d y -------- + f x ------ + g x y = h x for a d x d b with y a = Y a and y b = Y b 2 dx dx and Y b are constants. For the function name and arguments where Y a use [x,y]=BVPShootSecant(fOFx,gOFx,hOFx,a,b,n,Ya,Yb,WL,WH), where fOFx, gOFx, and hOFx are the names (strings) of the user-defined functions that calculate f x , g x and h x , respectively, a and b define the domain of the solution, n is the number of subintervals, Ya and Yb are the boundary conditions, and WL and WH are the assumed slopes at x = a that are used in the first two solutions. Within the user-defined function BVPShootSecant, use the user-defined function Sys2ODEsRK4 that was written in Problem 8.21 for solving the system of the two first-order ODEs. Stop the iterations when the absolute value of the true error at x = b is smaller than 0.001. Use BVPShootSecant to solve the boundary value problem in Example 9-6. Use n = 100 , W L = – 5 , and W H = – 1.5 . Solution First, the second order ODE is written as a system of two first order ODEs: dy ------ = z dx dz ------ = h x – g x y – f x z dx The system is solved as an initial value problem with y a = Y a and z a must be guessed to be of the correct value so that y b = Y b is satisfied upon integration of the ODE. The listing of the user-defined function BVPShootSecant is: function [x,Yi]=BVPShootSecant(fOFx,gOFx,hOFx,a,b,n,Ya,Yb,WL,WH) %The function solves a general second order linear ODE of the form: % d2y/dx2 + f(x)(dy/dx) + g(x)y = h(x) %By defining dy/dx = z, this ODE can be reduced to 2 first order ODEs %of the form: % dy/dx = z % dz/dx = h(x) - g(x)y - f(x)z %The function uses the shooting method in conjunction with the secant %method, and uses the user-defined function Sys2ODEsRK4 (Program 8-6) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 %to solve the system. %Input arguments: % fOFx Name of a user-defined function that calculates f(x). % gOFx Name of a user-defined function that calculates g(x). % hOFx Name of a user-defined function that calculates h(x). % a First value of x (first point in the domain). % b Last value of x (last point in the domain). % n Number of subintervals. % Ya Boundary condition at x=a. % Yb Boundary condition at x=b. % WL Assumed slope (dy/dx) at x=a. % WH Assumed slope (dy/dx) at x=a. %Output arguments: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. h=(b-a)/n; [x YL zL] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WL); [x YH zH] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,WH); iLast=length(x); % Start iterations using the secant method imax=15; tol=0.001; W(1)=WL; W(2)=WH; E(1)=YL(iLast) - Yb; E(2)=YH(iLast) - Yb; for i = 2:imax + 1 W(i+1)=W(i)-(W(i-1)-W(i))*E(i)/(E(i-1)-E(i)); [x Yi zi] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ya,W(i+1)); E = Yi(iLast) - Yb; if abs(E) < tol break end Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 end if i > imax fprintf('Solution was not obtained in %i iterations.\n',imax) end function dydz=ODE1(x,y,z) dydz=z; end function dzdx=ODE2(x,y,z) dzdx=hOFx(x)-gOFx(x)*y-fOFx(x)*z; end end Note that the user-defined function BVPShootSecant has two nested functions. One, called ODE1, which dz- = h x – g x y – f x z . calculates dy ------ = z , and the second, called ODE2, which calculates ----dx dx The problem in Example 9-6 is: 2 dy d y -------- + 2x ------ + 5y – cos 3x = 0 , 2 dx dx for 0dxdS (1.1) with the boundary conditions: y 0 = 1.5 and y S = 0 . In the notation used in the present problem: f x = 2x , g x = 5 , and h x = cos 3x . In order to solve the problem, the following three user-defined functions that calculate f x , g x and h x are created: function fx=fOFx(x) fx=2*x; function gx=gOFx(x) gx=5; function hx=hOFx(x) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 hx=cos(3*x); The following program (script file) solves the problem: % Solution of Problem 9.11 clear all a=0; b=pi; n=100; Ya=1.5; Yb=0; WL=-5; WH=-1.5; [x,y]=BVPShootSecant(@fOFx,@gOFx,@hOFx,a,b,n,Ya,Yb,WL,WH); plot(x,y) xlabel('x'); ylabel('y') When the script file is executed, the following figure is displayed in the Figure Window: 1.5 1 y 0.5 0 −0.5 −1 0 0.5 1 1.5 2 2.5 3 3.5 x This solution agrees with the solution in Example 9-6. Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.12 Write a user-defined MATLAB function that uses the finite difference method to solve a secondorder ODE of the form: 2 dy d y -------- + p ------ + qy = r x for a d x d b with y a = Y a and y b = Y b 2 dx dx where p, q, Y a and Y b are constants. Discretize the ODE using second-order accurate central differences. Name the function [x,y]=BVP2ndConst(a,b,n,Ya,Yb,p,q,rOFx), where a and b define the domain of the solution, Ya and Yb are the boundary conditions, n is the number of subintervals, and rOFx is the name (string) of the user-defined function that calculates r x . Within the program, use MATLAB’s left division operation to solve the system of linear equations. Use BVP2ndConst with 50 subintervals to solve the ODE in Problem 9.1 with the boundary conditions y 0 = 1 , y 2 = 0 . Plot the solution. Solution Using central difference formulas for the derivatives gives: yi + 1 – yi – 1 y i – 1 – 2y i + y i + 1 --------------------------------------- + p -------------------------+ qy i = r x i 2 2h h Rewriting the equation gives: p · 2 2 § 1 – p--- h· y § i – 1 + qh – 2 y i + © 1 + --- h¹ y i + 1 = h r x i © ¹ 2 2 For the first interior point: i = 2 y1 = Ya p p 2 2 qh – 2 y 2 + § 1 + --- h· y 3 = h r x 2 – § 1 – --- h· Y a © © 2 ¹ 2 ¹ For the last interior point: i = n p p 2 2 y n + 1 = Y b § 1 – --- h· y n – 1 + qh – 2 y n = h r x n – § 1 + --- h· Y b © © 2 ¹ 2 ¹ For all the other interior points: i = 3}n – 1 p 2 2 § 1 – p--- h· y + qh – 2 y i + § 1 + --- h· y i + 1 = h r x i © © 2 ¹ 2 ¹ i–1 The last three equations are used in user-defined function BVP2ndConst, which is listed next. Notice that in the program the system of linear equations that is solved has n – 1 equations, and that the ith equation in the system corresponds to the i + 1 point in the domain. function [x,y]=BVP2ndConst(a,b,n,Ya,Yb,p,q,rOFx) %The function solves a general second order linear ODE of the form: % d2y/dx2 + p(dy/dx) + qy = r(x) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 %using the finite difference method. %The boundary conditions are assumed to be of the constant Dirichlet form: % y(a) = Ya and y(b) = Yb %Second order central differences are used. %Input arguments: % a First value of x (first point in the domain). % b Last value of x (last point in the domain). % n Number of subintervals. % Ya Boundary condition at x=a. % Yb Boundary condition at x=b. % p The constant p. % q The constant q. % rOFx Name of a user-defined function that calculates r(x). %Output arguments: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. h=(b-a)/n; A=zeros(n-1,n-1); x(1)=a; for i=2:n+1 x(i)=x(i-1)+h; end % The equation for the first interior point. A(1,1)=h*h*q-2; A(1,2)=1+h*p/2; rhs(1)=h*h*rOFx(x(2))-Ya*(1-(h*p/2)); % The equations for interior points: second through one before the last. for i=2:n-2 A(i,i)=h*h*q-2; A(i,i+1)=1+h*p/2; A(i,i-1)=1-h*p/2; rhs(i)=h*h*rOFx(x(i+1)); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 end % The equation for the last interior point. A(n-1,n-2)=1-h*p/2; A(n-1,n-1)=h*h*q-2; rhs(n-1)=h*h*rOFx(x(n))-Yb*(1+(h*p/2)); y=A\rhs'; y=[Ya; y; Yb]; The ODE that is solved is from Problem 9.1: 2 d--------y- = y + x x – 4 dx 2 In the notation that is used in Problem 9.12: p = 1 , q = – 1 , and r x = x x – 4 . In order to solve the problem, the following user-defined function that calculate r x , is created: function rx=rOFx(x) rx=x*(x-4); The following program (script file) solves the problem: % Solution of Problem 9.12 clear all a=0; b=2; n=50; Ya=1; Yb=0; p=0; q=-1; [x,y]=BVP2ndConst(a,b,n,Ya,Yb,p,q,@rOFx); plot(x,y) xlabel('x'); ylabel('y') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 When the script file is executed, the following figure is displayed in the Figure Window: 1.4 1.2 1 y 0.8 0.6 0.4 0.2 0 0 0.5 1 1.5 2 2.5 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.13 Write a user-defined MATLAB function that uses the finite difference method to solve a boundary value problem of the form: 2 d-------y- + p x dy ------ + q x y = r x for a d x d b with y a = Y a and y b = Y b 2 dx dx where Y a and Y b are constants. Discretize the ODE using second-order accurate central differences. For the function name and arguments use [x,y]=BVP2ndVar(a,b,Ya,Yb,n,pOFx,qOFx,rOFx), where a and b define the domain of the solution, Ya and Yb are the boundary conditions, n is the number of subintervals, pOFx, qOFx, and rOFx are the names (strings) of the user-defined functions that calculate p x , q x and r x , respectively. Within the program, use MATLAB’s left division operation to solve the system of linear equations. Use the BVP2ndVar with 50 subintervals to solve the boundary value problem in Problem 9.3. Plot the solution. Solution Using central difference formulas for the derivatives gives: yi + 1 – yi – 1 y i – 1 – 2y i + y i + 1 --------------------------------------- + p x i -------------------------+ q xi yi = r xi 2 2h h Rewriting the equation gives: p xi p xi · 2 2 § 1 – -----------h y + q x i h – 2 y i + § 1 + ------------ h· y i + 1 = h r x i © © 2 ¹ i–1 2 ¹ For the first interior point: i = 2 y1 = Ya p x2 p x2 2 2 q x 2 h – 2 y 2 + § 1 + ------------- h· y 3 = h r x 2 – § 1 – ------------- h· Y a © ¹ © 2 2 ¹ For the last interior point: i = n p xn · p xn · -h Y - h y n – 1 + q x n h 2 – 2 y n = h 2 r x n – § 1 + -----------y n + 1 = Y b § 1 – -----------© © ¹ 2 ¹ b 2 For all the other interior points: i = 3}n – 1 p xi p xi · 2 2 § 1 – -----------h y + q x i h – 2 y i + § 1 + ------------ h· y i + 1 = h r x i © © 2 ¹ i–1 2 ¹ The last three equations are used in user-defined function BVP2ndVar, which is listed next. Notice that in the program the system of linear equations that is solved has n – 1 equations, and that the ith equation in the system corresponds to the i + 1 point in the domain. function [x,y]=BVP2ndVar(a,b,Ya,Yb,n,pOFx,qOFx,rOFx) %The function solves a general second order linear ODE of the form: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 % d2y/dx2 + p(x)(dy/dx) + q(x)y = r(x) %using the finite difference method. %The boundary conditions are assumed to be of the constant Dirichlet form: % y(a) = Ya and y(b) = Yb %Second order central differences are used. %Input arguments: % a First value of x (first point in the domain). % b Last value of x (last point in the domain). % n Number of subintervals. % Ya Boundary condition at x=a. % Yb Boundary condition at x=b. % pOFx Name of a user-defined function that calculates p(x). % qOFx Name of a user-defined function that calculates q(x). % rOFx Name of a user-defined function that calculates r(x). %Output arguments: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. h=(b-a)/n; A=zeros(n-1,n-1); x(1)=a; for i=2:n+1 x(i)=x(i-1)+h; end % The constants of the equation for the first interior point. A(1,1)=h*h*qOFx(x(2))-2; A(1,2)=1+h*pOFx(x(2))/2; rhs(1)=h*h*rOFx(x(2))-Ya*(1-(h*pOFx(x(2))/2)); % The constants of the equations for interior points: second through one % before the last. for i=2:n-2 A(i,i)=h*h*qOFx(x(i+1))-2; A(i,i+1)=1+h*pOFx(x(i+1))/2; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 A(i,i-1)=1-h*pOFx(x(i+1))/2; rhs(i)=h*h*rOFx(x(i+1)); end % The constants of the equation for the last interior point A(n-1,n-2)=1-h*pOFx(x(n))/2; A(n-1,n-1)=h*h*qOFx(x(n))-2; rhs(n-1)=h*h*rOFx(x(n))-Yb*(1+(h*pOFx(x(n))/2)); y=A\rhs'; y=[Ya; y; Yb]; The ODE that is solved is from Problem 9.1: 2 dy d y --------- + x ------ + y = 2x 2 dx dx In the notation that is used in Problem 9.13: p x = x , q x = 1 , and r x = 2x . In order to solve the problem, the following three user-defined functions that calculate p x , q x and r x are created: function px=pOFx(x) px=x; function qx=pOFx(x) qx=1; function rx=rOFx(x) rx=2*x; The following program (script file) solves the problem: % Solution of Problem 9.13 clear all a=0; b=1; n=50; Ya=1; Yb=1; [x,y]=BVP2ndVar(a,b,Ya,Yb,n,@pOFx,@qOFx,@rOFx); plot(x,y) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 xlabel('x'); ylabel('y') When the script file is executed, the following figure is displayed in the Figure Window: 1.025 1.02 1.015 1.01 y 1.005 1 0.995 0.99 0.985 0.98 0 0.2 0.4 0.6 0.8 1 1.2 1.4 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.14 Write a user-defined MATLAB function that uses the finite difference method to solve a boundary value problem of the form (see Eq. (9.24)): 2 dy dy d y = Db -------- + p x ------ + q x y = r x for a d x d b with y a = Y a and -----2 dx dx x = b dx where Y a and D b are constants. Discretize the ODE using second-order accurate central differences. For function name and arguments use [x,y]=BVP2ndDriv(a,b,Ya,Db,n,pOFx,qOFx,rOFx), where a and b define the domain of the solution, Ya and Db are the boundary conditions, n is the number of subintervals, pOFx, qOFx, and rOFx are the names (strings) of the user-defined functions that calculate p x , q x and r x , respectively. Within the program, use MATLAB’s left division operation to solve the system of linear equations. Use BVP2ndConst with 50 subintervals to solve the following boundary value problem: 2 1 dy d -------y- + --- ------ = – 10 , 2 x dx dx y 1 = 1 , dy -----dx = – 1.2 x=3 Plot the solution. Solution Using central difference formulas for the derivatives gives: yi + 1 – yi – 1 y i – 1 – 2y i + y i + 1 --------------------------------------- + p x i -------------------------+ q xi yi = r xi 2 2h h Rewriting the equation gives: p xi · p xi · 2 § 1 – ----------- h y i – 1 + q x i h 2 – 2 y i + § 1 + -----------h y = h r xi © ¹ © 2 2 ¹ i+1 For the first interior point: i = 2 p x2 p x2 2 2 q x 2 h – 2 y 2 + § 1 + ------------- h· y 3 = h r x 2 – § 1 – ------------- h· Y a Eq. (P9.14.1) © © 2 ¹ 2 ¹ y1 = Ya For the third and up to one point before the last interior point: i = 3 } n – 1 p xi · p xi · 2 § 1 – -----------h y - h y i – 1 + q x i h 2 – 2 y i + § 1 + ----------= h r xi © © ¹ 2 ¹ i+1 2 Eq. (P9.14.2) For the last interior point: i = n p xn · xn · 2 § 1 – p------------ h y n – 1 + q x n h 2 – 2 y n + § 1 + ------------h y = h r xn © © 2 ¹ 2 ¹ n+1 Eq. (P9.14.3) The value of the derivative at the last point is approximated by the three point backward formula: y i – 2 – 4y i – 1 + 3y i dy ------ = ------------------------------------------dx 2h Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The boundary condition at the last point x n + 1 = b is dy -----dx = D b . The last two equations give: x=b y n – 1 – 4y n + 3y n + 1 D b = ---------------------------------------------, which can be solved for y n + 1 : 2h – y n – 1 + 4y n + 2hD b y n + 1 = ------------------------------------------------3 Eq. (P9.14.4) The last expression is substituted in the Eq. (P9.14.3) (this eliminate y n + 1 from the equation): p x n · § – y n – 1 + 4y n + 2hD b· xn · § 1 – p------------ h -------------------------------------------------- = h 2 r x n - h y n – 1 + q x n h 2 – 2 y n + § 1 + -----------© © ¹ ¹ 2 ¹© 2 3 This equation can be written as: p x n · 2hD b 2 2 - h --------------- > 1 – p x n h @y n – 1 + – 2--- + p x n h 2 + --- p x n h y n = h 2 r x n – § 1 + -----------© 3 3 2 ¹ 3 3 Eq. (P9.14.5) The solution for y 2 } y n is obtained by solving a system of n – 1 equations made of Eqs. (P9.14.1), (P9.14.2), and (P9.14.5). Then, the solution at the last point y n + 1 is calculated by Eq. (P9.14.4). The user-defined function BVP2ndDeriv is listed in the following. Notice that in the program, the system of linear equations that is solved has n – 1 equations, and that the ith equation in the system corresponds to the i + 1 point in the domain. function [x,y]=BVP2ndDeriv(a,b,Ya,Db,n,pOFx,qOFx,rOFx) %The function solves a general second order linear ODE of the form: % d2y/dx2 + p(x)(dy/dx) + q(x)y = r(x) %using the finite difference method. %The boundary conditions are assumed to be of the constant Dirichlet form: % y(a) = Ya and dy/dx(b) = Db %Second order central differences are used. %Input arguments: % a First value of x (first point in the domain). % b Last value of x (last point in the domain). % n Number of subintervals. % Ya Boundary condition at x=a. % Db Boundary condition at x=b (value of derivative). Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 % pOFx Name of a user-defined function that calculates p(x). % qOFx Name of a user-defined function that calculates q(x). % rOFx Name of a user-defined function that calculates r(x). %Output arguments: % x A vector with the x coordinate of the solution points. % y A vector with the y coordinate of the solution points. h=(b-a)/n; A=zeros(n-1,n-1); x(1)=a; for i=2:n+1 x(i)=x(i-1)+h; end A(1,1)=h*h*qOFx(x(2))-2; A(1,2)=1+h*pOFx(x(2))/2; rhs(1)=h*h*rOFx(x(2))-Ya*(1-(h*pOFx(x(2))/2)); for i=2:n-2 A(i,i-1)=1-h*pOFx(x(i+1))/2; A(i,i)=h*h*qOFx(x(i+1))-2; A(i,i+1)=1+h*pOFx(x(i+1))/2; rhs(i)=h*h*rOFx(x(i+1)); end A(n-1,n-2)=2*(1-pOFx(x(n))*h)/3; A(n-1,n-1)=-2/3+qOFx(x(n))*h*h+2*pOFx(x(n))*h/3; rhs(n-1)=h*h*rOFx(x(n))-(1+h*pOFx(x(n))/2)*2*h*Db/3; y=A\rhs'; y=[Ya; y] yb=(-y(n-1)+4*y(n)+2*h*Db)/3; y=[y; yb]; The ODE that is solved is: 2 d y 1 dy -------- + --- ------ = – 10 dx 2 x dx In the notation that is used in Problem 9.14: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 1 p x = --- , q x = 0 , and r x = – 10 . x In order to solve the problem, the following three user-defined functions that calculate p x , q x and r x are written: function px=pOFx(x) px=1/x; function qx=pOFx(x) qx=0; function rx=rOFx(x) rx=-10; The following program (script file) solves the problem: % Solution of Problem 9.14 clear all a=1; b=3; n=50; Ya=1; Db=-1.2; [x,y]=BVP2ndDeriv(a,b,Ya,Db,n,@pOFx,@qOFx,@rOFx); plot(x,y) xlabel('x'); ylabel('y') Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 When the script file is executed, the following figure (solution) is displayed in the Figure Window: 30 25 y 20 15 10 5 0 1 1.5 2 2.5 3 3.5 x Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.15 A flexible cable of uniform density is suspended between two points as shown in the figure. The shape of the cable, y x , is governed by the differential equation: 2 dy 2 d y -------- = C 1 + § ------· © dx¹ dx 2 where C is a constant equal to the ratio of the weight per unit length of the cable to the magnitude of the horizontal component of tension in the cable at its lowest point. The cable hangs between two points specified by y 0 = 15 m and y 20 = 10 m, and C = 0.041 m-1. Use MATLAB’s built-in functions to determine and plot the shape of the cable between x = 0 and x = 20 m. Solution The ODE is transforming into a system of two first-order ODEs: dy ------ = w dx 2 dw ------- = C 1 + w dx The problem is solved by using MATLAB’s built-in function bvp4c (see Section 9.4). To use bvp4c two dw dy user-defined functions are needed. One, named here odefunHW9_15 calculates the value of ------ and ------- : dx dx function dydx = odefunHW9_15(x,yw) C=0.041; dydx = [yw(2) C*sqrt(1+yw(2)^2)]; The second user-defined function, named bcfunHW9_15 that computes the residual in the boundary conditions: function res = bcfunHW9_15(ya,yb) BCa = 15; BCb = 10; res = [ya(1) - BCa yb(1) - BCb]; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The program in the following script file solves the problem: % Solution of HW 9_15 (script file) clear all solinit =bvpinit(linspace(0,20,20),[12,12]); sol = bvp4c('odefunHW9_15','bcfunHW9_15',solinit); plot(sol.x,sol.y(1,:),'r') xlabel('x'); ylabel('y') Solution=[sol.x;sol.y(1,:)] When the script file is executed, the following numerical solution is displayed in the Command Window, and the following plot of the solution is displayed in the Figure Window: Solution = Columns 1 through 7 0 1.0526 2.1053 15.0000 14.2930 13.6402 Columns 8 through 14 7.3684 8.4211 9.4737 11.1479 10.7974 10.4944 Columns 15 through 20 14.7368 15.7895 16.8421 9.6765 9.6502 9.6693 3.1579 13.0402 4.2105 12.4919 5.2632 11.9944 6.3158 11.5466 10.5263 10.2384 11.5789 10.0290 12.6316 9.8658 13.6842 9.7483 17.8947 9.7339 18.9474 9.8440 20.0000 10.0000 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 15 14 y 13 12 11 10 9 0 5 10 x 15 20 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.16 A simply supported beam of length L = 4 m is loaded by a distributed load as shown in the figure. The deflection of the beam, y, is determined from the solution of the following ODE (when the deflections are small): 2 3 d y 1 EI -------2- = --- q 0 § Lx – x-----· 6 © dx L¹ where EI = 1.2 u 10 N–m2 7 y 0 = 0 and y L = 0 is the flexural rigidity, and 3 q 0 = 30 u 10 N/m. Determine and plot the deflection of the beam as a function of x. (a) Use the user-defined function BVPShootBisec that was written in Problem 9.10. Use n = 100 , W L = 0 , and W H = – 0.005 . (b) Use the user-defined function BVP2ndConst with 50 subintervals that was written in Problem 9.12. (c) Use MATLAB built-in functions to solve the ODE. Solution (a) The form of the ODE in Problem 9.10 is: 2 dy d y -------- + f x ------ + g x y = h x 2 dx dx In this form, for the problem that is being solved: 3 1 f x = 0 , g x = 0 , and h x = --------- q 0 § Lx – x-----· 6EI © L¹ The following MATLAB program (script file) solves the problem: % Solution of Problem 9.16a (Script File) clear all a=0; b=4; n=100; Ya=0; Yb=0; WL=0; WH=-0.005; [x,y]=BVPShootBisec(@fOFx,@gOFx,@hOFx,a,b,n,Ya,Yb,WL,WH); plot(x,y) xlabel('x (m)'); ylabel('y (m)') The three user-defined function fOFx, gOFx and hOFx are: function fx=fOFx(x) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 fx=0; function gx=gOFx(x) gx=0; function hx=hOFx(x) EI=1.2E7; L=4; q0=30E3; hx=q0*(L*x-x^3/L)/(6*EI); The user-defined function BVPShootBisec is listed in the solution of Problem 9.10. When the script file is executed, the following plot is displayed: 0 x 10 −3 −0.5 −1 y (m) −1.5 −2 −2.5 −3 −3.5 −4 −4.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 x (m) (b) The form of the ODE in Problem 9.12 is: 2 d y- + p dy ------ + qy = r x ------dx dx 2 In this form, for the present problem that is being solved: 1 x3 p = 0 , q = 0 , and r x = --------- q 0 § Lx – -----· 6EI © L¹ The following MATLAB program (script file) solves the problem: % Solution of Problem 9.16b Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 clear all a=0; b=4; n=50; Ya=0; Yb=0; p=0; q=0; [x,y]=BVP2ndConst(a,b,n,Ya,Yb,p,q,@rOFx); plot(x,y) xlabel('x (m)'); ylabel('y (m)') The user-defined function rOFx is: function rx=rOFx(x) EI=1.2E7; L=4; q0=30E3; rx=q0*(L*x-x^3/L)/(6*EI); The user-defined function BVP2ndConst is listed in the solution of Problem 9.12. When the script file is executed, the following plot is displayed: 0 x 10 −3 −0.5 −1 y (m) −1.5 −2 −2.5 −3 −3.5 −4 −4.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 x(m) (c) The ODE is transforming into a system of two first-order ODEs: dy ------ = w dx Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 3 1 dw = -------- q 0 § Lx – x-----· ------© 6EI L¹ dx The problem is solved by using MATLAB’s built-in function bvp4c (see Section 9.4). To use bvp4c two dw dy user-defined functions are needed. One, named here odefunHW9_16 calculates the value of ------ and ------- : dx dx function dydx = odefunHW9_16(x,yw) EI=1.2E7; q0=30E3; L=4; C=q0/(6*EI); dydx = [yw(2) C*(L*x-x^3/L)]; The second user-defined function, named bcfunHW9_16 that computes the residual in the boundary conditions: function res = bcfunHW9_16(ya,yb) BCa = 0; BCb = 0; res = [ya(1) - BCa yb(1) - BCb]; The program in the following script file solves the problem: % Solution HW9_16c (Script file) clear all solinit =bvpinit(linspace(0,4,20),[-0.005,-0.005]); sol = bvp4c('odefunHW9_16','bcfunHW9_16',solinit); plot(sol.x,sol.y(1,:),'r') xlabel('x (m)'); ylabel('y (m)') % Display a numerical solution. Solution=[sol.x;sol.y(1,:)] When the script file is executed, the following numerical solution is displayed in the Command Window, and the following plot of the solution is displayed in the Figure Window: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 5 Solution = Columns 1 through 7 0 0.2105 0.4211 0 -0.0007 -0.0013 Columns 8 through 14 1.4737 1.6842 1.8947 -0.0037 -0.0040 -0.0041 Columns 15 through 20 2.9474 3.1579 3.3684 -0.0032 -0.0027 -0.0021 0 x 10 0.6316 -0.0019 0.8421 -0.0025 1.0526 -0.0030 1.2632 -0.0034 2.1053 -0.0042 2.3158 -0.0041 2.5263 -0.0039 2.7368 -0.0036 3.5789 -0.0015 3.7895 -0.0007 4.0000 0 −3 −0.5 −1 y (m) −1.5 −2 −2.5 −3 −3.5 −4 −4.5 0 0.5 1 1.5 2 x (m) 2.5 3 3.5 4 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.17 A simply supported beam of length L = 4 m is loaded by a uniform distributed load as shown in the figure. For large deflections, the deflection of the beam, y, is determined from the solution of the following ODE: d y dy 2 3 e 2 1 --- q Lx – x 2 EI -------2- = 1 + § ------ · y 0 = 0 and y L = 0 © dx ¹ 2 dx where EI = 1.4 u 10 7 N–m2 is the flexural rigidity, and 2 3 q = 10 u 10 N/m. Use MATLAB’s built-in functions to determine and plot the deflection of the beam as a function of x. Solution The ODE is transforming into a system of two first-order ODEs: dy ------ = w dx dy 2 3 e 2 1 1 dw --- q Lx – x 2 ------- = ------ 1 + § ------ · © dx ¹ EI 2 dx The problem is solved by using MATLAB’s built-in function bvp4c (see Section 9.4). To use bvp4c two dw dy user-defined functions are needed. One, named here odefunHW9_17 calculates the value of ------ and ------- : dx dx function dydx = odefunHW9_17(x,yw) EI=1.4E5; q0=10E3; L=4; C=q0/(2*EI); dydx = [yw(2) C*(L*x-x^2)*(1+yw(2)^2)^1.5]; The second user-defined function, named bcfunHW9_15 that computes the residual in the boundary conditions: function res = bcfunHW24(ya,yb) BCa = 0; BCb = 0; res = [ya(1) - BCa yb(1) - BCb]; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 The program in the following script file solves the problem: % Solution of HW9_17 (Script file) clear all solinit =bvpinit(linspace(0,4,20),[0.005,0.005]); sol = bvp4c('odefunHW9_17','bcfunHW9_17',solinit); plot(sol.x,sol.y(1,:),'r') xlabel('x'); ylabel('y') Solution=[sol.x;sol.y(1,:)] When the script file is executed, the following numerical solution is displayed in the Command Window, and the following plot of the solution is displayed in the Figure Window: Solution = Columns 1 through 7 0 0.2105 0.4211 0 -0.0406 -0.0799 Columns 8 through 14 1.4737 1.6842 1.8947 -0.2214 -0.2339 -0.2402 Columns 15 through 20 2.9474 3.1579 3.3684 -0.1790 -0.1500 -0.1167 0.6316 -0.1167 0.8421 -0.1500 1.0526 -0.1790 1.2632 -0.2030 2.1053 -0.2402 2.3158 -0.2339 2.5263 -0.2214 2.7368 -0.2030 3.5789 -0.0799 3.7895 -0.0406 4.0000 0 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 0 −0.05 y (m) −0.1 −0.15 −0.2 −0.25 0 0.5 1 1.5 2 x (m) 2.5 3 3.5 4 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.18 The temperature distribution in a straight fin, T x with a triangular profile is given by the solution of the equation: 2 2h LW + bL – bx T – T f 1 dT d T --------------------- – -------------------------------------------------------------------------2 – - = 0 L – x dx kbW L – x dx where T f = 300 K is the ambient temperature, x is the coordinate measured along the fin, k = 237 W/m/K is the thermal conductivity of alu- minum, h = 15 W/m2/K is the convective heat transfer coefficient, L = 0.01 m is the length of the fin, W = 0.1 m is its width, and b = 0.01 m is the height of the base. The boundary conditions are: T x = 0 = 673 K, and dT -----dx = 0. x=L Find and plot the temperature distribution, T x , along the fin. Write a program in a script file that solves the problem with the shooting method. Note that a derivative boundary condition is prescribed at x = L . Use the three-point backward difference formula to calculate the value of the derivative from the numerical solution at x = L . Compare the numerical solution with the prescribed boundary condition and use the bisection method to calculate the new estimate for the slope at x = 0 . Stop the iterations when the true relative error at x = L is smaller than 0.01. Important note: The point x = L is a singular point of the ODE. Therefore, the problem cannot be solved as specified. An approximate solution can however be obtained by using L = 0.00999999 m for the length of the fin. Solution First, the second order ODE is written as a system of two first order ODEs: dT ------ = w dx 2h LW + bL – bx T – T f 1 dw ------- = ---------------- w + ----------------------------------------------------------------L – x kbW L – x dx The system is solved as an initial value problem with y a = Y a and w a must be guessed to be of the -----correct value so that dT dx = 0 is satisfied upon integration of the ODE. The value of the derivative from x=L the numerical solution at x = L is calculated with the three-point backward difference formula: f x i – 2 – 4f x i – 1 + 3f x i f c x i = --------------------------------------------------------------2h The following MATLAB program (script file) solves the problem. The program is similar to the program BVPShootBisec that was written in Problem 9.10. The program solves the system of two first-order Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 ODEs with the user-defined function Sys2ODEsRK4 that was written in Example 8-8. The values of the dw dT two first-order ODEs, ------ and ------- , are calculated in two user-defined functions named ODE1 and ODE2 dx dx (they are listed following the script file). The listing of the script file with the solution is: % Solution of HW9_18 (script file) clear all a=0; b=0.00999999; n=40; Ta=673; DTb=0; % Assume two slopes at x=a. WH=-40; WL=-70; h=(b-a)/n; % Calculate the first two solutions. [x TH zL] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ta,WH); [x TL zH] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ta,WL); iLast=length(x); % Calculate dT/dx at x=L (using the three-point backward difference formula) % for the first two solutions. DTH=(TH(iLast-2)-4*TH(iLast-1)+3*TH(iLast))/(2*h); DTL=(TL(iLast-2)-4*TL(iLast-1)+3*TL(iLast))/(2*h); % Check if the prescribed dT/dx at x=L is between the value in the first % two solutions. if (DTb < DTL & DTb < DTH) | (DTb > DTH & DTb > DTL) disp('ERROR: The boundary condition at x=b is not between the two guessed solutions') else % Start iterations using the bisection method imax=100; tol=0.01; for i = 1:imax + 1 Wi = (WH + WL)/2; Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 [x Ti zi] = Sys2ODEsRK4(@ODE1,@ODE2,a,b,h,Ta,Wi); DTi=(Ti(iLast-2)-4*Ti(iLast-1)+3*Ti(iLast))/(2*h); E = DTi - DTb; if abs(E) < tol break end if DTL*DTi > 0 WL = Wi; DTL=DTi; else WH = Wi; DTH=DTi; end end if i > imax fprintf('Solution was not obtained in %i iterations.\n',imax) end end plot(x,Ti) xlabel('x (m)'), ylabel('T (K)') The two user-defined functions ODE1 and ODE2 are: function dTdx=ODE1(x,T,w) dTdx=w; function dwdx=ODE2(x,T,w) Tinf=300; k=237; h=15; L=0.00999999; W=0.1; b=0.01; dwdx=w/(L-x)+2*h*(L*W+b*L-b*x)*(T-Tinf)/(k*b*W*(L-x)); When the script file is executed the following plot (solution) is displayed: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 4 673 672.95 672.9 672.85 T (K) 672.8 672.75 672.7 672.65 672.6 672.55 672.5 0 0.002 0.004 0.006 0.008 0.01 x (m) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.19 Solve Problem 9.18 using the finite difference method with 100 subintervals. Use second-order accurate central differences for all the derivatives in the ODE, and use appropriate one-sided differences for the boundary condition. As explained in Problem 9.18, use L = 0.00999999 m for the length of the fin. Solution The problem is solved by using the user-defined function BVP2ndDriv that was written in Problem 9.14. The equation that is being solved is: 2 2h LW + bL – bx T – T f 1 dT d T --------------------- – ------------------------------------------------------------------------- – - = 0 L – x dx kbW L – x dx 2 In the notation that is used in Problem 9.14: –1 p x = ---------------- , L–x 2h LW + bL – bx q x = – --------------------------------------------kbW L – x 2h LW + bL – bx T kbW L – x , and r x = – ---------------------------------------------------f- . In order to solve the problem, the following three user-defined functions that calculate p x , q x and r x are written: function px=pOFx(x) L=0.00999999; px=-1/(L-x); function qx=pOFx(x) Tinf=300; k=237; h=15; L=0.00999999; W=0.1; b=0.01; qx=-2*h*(L*W+b*L-b*x)/(k*b*W*(L-x)); function rx=rOFx(x) Tinf=300; k=237; h=15; L=0.00999999; W=0.1; b=0.01; rx=-2*h*(L*W+b*L-b*x)*Tinf/(k*b*W*(L-x)); The following program (script file) solves the problem: % Solution of Problem 9.19 clear all a=0; b=0.00999999; n=100; Ta=673; Db=0; [x,T]=BVP2ndDeriv(a,b,Ta,Db,n,@pOFx,@qOFx,@rOFx); Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 plot(x,T) xlabel('x (m)'); ylabel('T (K)') The listing of the user-defined function is given in the solution of Problem 9.14. When the program is executed, the following plot (solution is displayed: 673 672.95 672.9 672.85 T (K) 672.8 672.75 672.7 672.65 672.6 672.55 672.5 0 0.002 0.004 0.006 0.008 0.01 x (m) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.20 The fuel rod of a nuclear reactor is a cylindrical structure with the fuel retained inside a cladding as shown in the figure. The fuel causes heat to be generated by nuclear reactions within the cylinder as well as in the cladding. The outer surface of the cladding is cooled by flowing water at T f = 473 with a heat transfer coefficient of h = 10 4 W/m2/K. The thermal conductivity of the cladding material is k = 16.75 W/m/K. The dimensions of the fuel rod are R = 1.5 u 10 –2 m, and w = 3.0 u 10 –3 m. The temperature distribution in the cladding is determined by the solution of the following boundary value problem: –r e R e 1 d § dT· --- ----- rk ------ = – 10 8 ------------ , for R d r d R + w , with dT -----r r dr © dr ¹ dr 5 dT 6.32 u 10 = – ------------------------- and -----dr k r=R h = – --- T k r = R+w r = R+w – Tf Use MATLAB’s built-in function bvp4c to solve the boundary value problem. Plot the temperature distribution in the cladding as a function of r. Solution The ODE can be written in the form: –r e R 2 1 dT 8e d T --------- = – --- ------ – 10 -----------2 dr r kr dr The ODE is transforming into a system of two first-order ODEs: dT ------ = w dr –r e R 1 8e dw ------- = – --- w – 10 -----------r kr dr The problem is solved by using MATLAB’s built-in function bvp4c (see Section 9.4). To use bvp4c two dT dw user-defined functions are needed. One, named here odefunHW9_20 calculates the value of ------ and ------- : dr dx function dTdx = odefunHW9_20(r,Tw) k=16.75; R=1.5E-2; dTdx = [Tw(2) -Tw(2)/r-1E8*exp(-r/R)/(k*r)]; The second user-defined function, named bcfunHW9_20 that computes the residual in the boundary conditions: Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 function res = bcfunHW9_20(ya,yb) Tinf=473; h=1E4; k=16.75; BCa = -6.32E5/k; BCb = -h*(yb(1)-Tinf)/k; res = [ya(2) - BCa yb(2) - BCb]; The program in the following script file solves the problem: % Solution HW9_20 (Script file) clear all R=1.5E-2; w=3E-3; solinit =bvpinit(linspace(R,R+w,20),[1000,-30000]); sol = bvp4c('odefunHW9_20','bcfunHW9_20',solinit); plot(sol.x,sol.y(1,:),'r') xlabel('r (m)'); ylabel('T (K)') % Display a numerical solution. Solution=[sol.x;sol.y(1,:)] When the script file is executed, the following numerical solution is displayed in the Command Window, and the following plot of the solution is displayed in the Figure Window: >> format short g Solution = Columns 1 through 5 0.015 0.015158 1730.6 1722.8 Columns 6 through 10 0.015789 0.015947 1658.2 1634.1 Columns 11 through 15 0.016579 0.016737 1509.1 1471 Columns 16 through 20 0.017368 0.017526 0.015316 1711.6 0.015474 1697 0.015632 1679.2 0.016105 1607.1 0.016263 1577.2 0.016421 1544.5 0.016895 1430.4 0.017053 1387.4 0.017211 1342 0.017684 0.017842 0.018 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 1294.2 1244.2 1192 1137.7 1081.4 1800 1700 1600 T (K) 1500 1400 1300 1200 1100 1000 0.015 0.0155 0.016 0.0165 r (m) 0.017 0.0175 0.018 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 1 9.21 The axial temperature variation of a current-carrying bare wire is described by: 4 4 2 2 4HV SB T – T f I Ue 4h d T -------------- – - T – T f – ------------------------------------- = – -----------------------kD dx 2 kD 1 2· 2 § k --- SD ©4 ¹ where T is the temperature in K, x is the coordinate along the wire, k = 72 W/m/K is the thermal conductivity, h = 2000 W/m2/K2, is the convective heat coefficient, H = 0.1 is the radiative emissivity, V SB = 5.67 u 10 W/m2/K4 –8 is the Stefan-Boltzmann constant, I = 2 A is the current, U e = 32 u 10 ture, D = 7.62 u 10 conditions are: –5 –8 :m is the electrical resistivity, T f = 300 K is the ambient tempera- m is the wire diameter, and L = 4.0 u 10 –3 is the length of the wire. The boundary at x = 0 L T = 300 K, and at x = --2 dT ------ = 0 dx L 2 Use MATLAB’s built-in function bvp4c to solve the boundary value problem for 0 d x d --- , since the tem--- . Plot the temperature distribution along the wire. perature distribution is symmetric about x = L 2 Solution The ODE is transforming into a system of two first-order ODEs: dT ------ = w dx 4 4 2 4HV SB T – T f I Ue 4h dw ------- = ------- T – T f + ------------------------------------- – -----------------------kD dx kD 1 2 2 k § --- SD · ©4 ¹ The problem is solved by using MATLAB’s built-in function bvp4c (see Section 9.4). To use bvp4c two dT dw user-defined functions are needed. One, named here odefunHW9_21 calculates the value of ------ and ------- : dx dx function dTdx = odefunHW9_21(x,Tw) k=72; h=2000; eps=0.1; sSB=5.67E-8; I=2; roe=32E-8; Tinf=300; D=7.62E-5; L=4E-3; dTdx = [Tw(2) Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 2 4*h*(Tw(1)-Tinf)/(k*D)+4*eps*sSB*(Tw(1)^4-Tinf^4)/(k*D)-I^2*roe/ (k*(pi*D^2/4)^2)]; The second user-defined function, named bcfunHW9_21 that computes the residual in the boundary conditions: function res = bcfunHW9_21(ya,yb) Tinf=473; h=1E4; k=16.75; BCa = 300; BCb = 0; res = [ya(1) - BCa yb(2) - BCb]; The program in the following script file solves the problem: % Solution HW9_21 (Script file) clear all L=4E-3; a=0; b=L/2; solinit =bvpinit(linspace(a,b,20),[320,20]); sol = bvp4c('odefunHW9_21','bcfunHW9_21',solinit); plot(sol.x,sol.y(1,:),'r') xlabel('x (m)'); ylabel('T (K)') % Display a numerical solution. Solution=[sol.x;sol.y(1,:)] When the script file is executed, the following numerical solution is displayed in the Command Window, and the following plot of the solution is displayed in the Figure Window: Solution = Columns 1 0 300.0000 Columns 8 0.0007 635.6896 through 7 0.0001 0.0002 368.7415 429.1112 through 14 0.0008 0.0009 662.6236 685.9484 0.0003 482.0864 0.0004 528.5250 0.0005 569.1796 0.0006 604.7091 0.0011 706.0425 0.0012 723.2322 0.0013 737.7969 0.0014 749.9733 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. 3 Columns 15 through 20 0.0015 0.0016 0.0017 759.9591 767.9169 773.9759 0.0018 778.2347 0.0019 780.7626 0.0020 781.6007 800 750 700 650 T (K) 600 550 500 450 400 350 300 0 0.5 1 x (m) 1.5 2 −3 x 10 Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis for testing or instructional purposes only to students enrolled in courses for which the textbook has been adopted. Any other reproduction or translation of this work beyond that permitted by Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful.