DOC PREVIEW
TAMU PETE 301 - Numerical Methods for Engineers Ch. 2 Solutions

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission. CHAPTER 2 2.1 IF x < 100 THEN IF x < 50 THEN x = 0 ELSE x = 75 END IF ELSE DO IF x  500 EXIT x = x – 50 END DO ENDIF 2.2 DO j = j + 1 x = x + 5 IF x > 5 THEN y = x ELSE y = 0 ENDIF z = x + y IF z > 50 EXIT ENDDO 2.3 Students could implement the subprogram in any number of languages. The following VBA program is one example. It should be noted that the availability of complex variables in languages such as Fortran 90 would allow this subroutine to be made even more concise. However, we did not exploit this feature, in order to make the code more compatible with languages that do not support complex variables. This version is then followed by a MATLAB script and function that does accommodate complex variables. Option Explicit Sub Rootfind() Dim ier As Integer Dim a As Double, b As Double, c As Double Dim r1 As Double, i1 As Double, r2 As Double, i2 As Double a = 1: b = 7: c = 2 Call Roots(a, b, c, ier, r1, i1, r2, i2) If ier = 0 Then MsgBox "No roots" ElseIf ier = 1 Then MsgBox "single root=" & r1 ElseIf ier = 2 Then MsgBox "real roots = " & r1 & ", " & r2 ElseIf ier = 3 Then MsgBox "complex roots =" & r1 & "," & i1 & " i" & "; "_ & r2 & "," & i2 & " i" End If End Sub Sub Roots(a, b, c, ier, r1, i1, r2, i2) Dim d As Double r1 = 0: r2 = 0: i1 = 0: i2 = 0 If a = 0 Then2 PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission. If b <> 0 Then r1 = -c / b ier = 1 Else ier = 0 End If Else d = b ^ 2 - 4 * a * c If (d >= 0) Then r1 = (-b + Sqr(d)) / (2 * a) r2 = (-b - Sqr(d)) / (2 * a) ier = 2 Else r1 = -b / (2 * a) r2 = r1 i1 = Sqr(Abs(d)) / (2 * a) i2 = -i1 ier = 3 End If End If End Sub The answers for the 3 test cases are: (a) 0.2984, 6.702; (b) 0.32; (c) 0.4167 + 1.5789i; 0.4167  1.5789i. Several features of this subroutine bear mention: - The subroutine does not involve input or output. Rather, information is passed in and out via the arguments. This is often the preferred style, because the I/O is left to the discretion of the programmer within the calling program. - Note that a variable is passed (IER) in order to distinguish among the various cases. MATLAB: function [r1,r2]=quadroots(a,b,c) r1 = 0; r2 = 0; if a == 0 if b ~= 0 r1=-c/b; else r1='Trivial solution'; end else discr=b^2-4*a*c; if discr >= 0 r1=(-b+sqrt(discr))/(2*a); r2=(-b-sqrt(discr))/(2*a); else r1 =-b/(2*a); i1=sqrt(abs(discr))/(2*a); r2=r1-i1*i; r1=r1+i1*i; end end Script: clc format compact disp('(a)'),[r1,r2]=quadroots(1,7,2) disp('(b)'),[r1,r2]=quadroots(0,-5,1.6) disp('(c)'),[r1,r2]=quadroots(3,2.5,8) Output when script is run3 PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission. (a) r1 = -0.2984 r2 = -6.7016 (b) r1 = 0.3200 r2 = 0 (c) r1 = -0.4167 + 1.5789i r2 = -0.4167 - 1.5789i 2.4 The development of the algorithm hinges on recognizing that the series approximation of the sine can be represented concisely by the summation, 2111(1)(2 1)!niiixi where i = the order of the approximation. (a) Structured flowchart: startINPUTx, ni > nendi = 1true = sin(x)approx = 0factor = 1approx approxxfactorii-=+-(1)-121errortrue approxtrue100%OUTPUTi,approx,errori = i + 1FTfactor=factor(2i-2)(2i-1)4 PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this Manual, you are using it without permission. (b) Pseudocode: SUBROUTINE Sincomp(n,x) i = 1; truth = SIN(x); approx = 0 factor = 1 DO IF i > n EXIT approx = approx + (-1)i-1x2-i-1 / factor error = (truth - approx) / truth) * 100 PRINT i, truth, approx, error i = i + 1 factor = factor(2i-2)(2i-1) END DO END 2.5 Students could implement the subprogram in any number of languages. The following MATLAB M-file is one example. It should be noted that MATLAB allows direct calculation of the factorial through its intrinsic function factorial. However, we did not exploit this feature, in order to make the code more compatible with languages such as Visual BASIC and Fortran. function sincomp(x,n) i = 1; tru = sin(x); approx = 0; f = 1; fprintf('\n'); fprintf('order true value approximation error\n'); while (1) if i > n, break, end approx = approx + (-1)^(i - 1) * x^(2*i-1) / f; er = (tru - approx) / tru * 100; fprintf('%3d %14.10f %14.10f %12.8f \n',i,tru,approx,er); i = i + 1; f = f*(2*i-2)*(2*i-1); end Here is a run of the program showing the output that is generated: >> sincomp(1.5,8) order true value approximation error 1 0.9974949866 1.5000000000 -50.37669564 2 0.9974949866 0.9375000000 6.01456523 3 0.9974949866 1.0007812500 -0.32945162 4 0.9974949866 0.9973911830 0.01040643 5 0.9974949866 0.9974971226 -0.00021414 6 0.9974949866 0.9974949557 0.00000310 7


View Full Document

TAMU PETE 301 - Numerical Methods for Engineers Ch. 2 Solutions

Documents in this Course
Load more
Download Numerical Methods for Engineers Ch. 2 Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Numerical Methods for Engineers Ch. 2 Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Numerical Methods for Engineers Ch. 2 Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?