numerical analysis chapra 5ESM2.docx
- 文档编号:23317230
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:24
- 大小:176.67KB
numerical analysis chapra 5ESM2.docx
《numerical analysis chapra 5ESM2.docx》由会员分享,可在线阅读,更多相关《numerical analysis chapra 5ESM2.docx(24页珍藏版)》请在冰豆网上搜索。
numericalanalysischapra5ESM2
CHAPTER2
2.1Twopossibleversionscanbedeveloped:
IFx10THEN
DO
x=x–5
IFx<50EXIT
ENDDO
ELSE
IFx<5THEN
x=5
ELSE
x=7.5
ENDIF
ENDIF
IFx10THEN
DO
x=x–5
IFx<50EXIT
ENDDO
ELSEIFx<5
x=5
ELSE
x=7.5
ENDIF
2.2
DO
i=i+1
IFz>50EXIT
x=x+5
IFx>5THEN
y=x
ELSE
y=0
ENDIF
z=x+y
ENDDO
2.3Notethatthisalgorithmismadesimplerbyrecognizingthatconcentrationcannotbydefinitionbenegative.Therefore,themaximumcanbeinitializedaszeroatthestartofthealgorithm.
Step1:
Start
Step2:
Initializesum,countandmaximumtozero
Step3:
Examinetopcard.
Step4:
Ifitsays“endofdata”proceedtostep9;otherwise,proceedtonextstep.
Step5:
Addvaluefromtopcardtosum.
Step6:
Increasecountby1.
Step7:
Ifvalueisgreaterthanmaximum,setmaximumtovalue.
Step7:
Discardtopcard
Step8:
ReturntoStep3.
Step9:
Isthecountgreaterthanzero?
Ifyes,proceedtostep10.
Ifno,proceedtostep11.
Step10:
Calculateaverage=sum/count
Step11:
End
2.4Flowchart:
2.5Studentscouldimplementthesubprograminanynumberoflanguages.ThefollowingFortran90programisoneexample.ItshouldbenotedthattheavailabilityofcomplexvariablesinFortran90wouldallowthissubroutinetobemadeevenmoreconcise.However,wedidnotexploitthisfeature,inordertomakethecodemorecompatiblewithlanguagessuchasVisualBASICorC.
PROGRAMRootfind
IMPLICITNONE
INTEGER:
:
ier
REAL:
:
a,b,c,r1,i1,r2,i2
DATAa,b,c/1.,6.,2./
CALLRoots(a,b,c,ier,r1,i1,r2,i2)
IF(ier==0)THEN
PRINT*,r1,i1,"i"
PRINT*,r2,i2,"i"
ELSE
PRINT*,"Noroots"
ENDIF
END
SUBROUTINERoots(a,b,c,ier,r1,i1,r2,i2)
IMPLICITNONE
INTEGER:
:
ier
REAL:
:
a,b,c,d,r1,i1,r2,i2
r1=0.
r2=0.
i1=0.
i2=0.
IF(a==0.)THEN
IF(b/=0)THEN
r1=-c/b
ELSE
ier=1
ENDIF
ELSE
d=b**2-4.*a*c
IF(d>=0)THEN
r1=(-b+SQRT(d))/(2*a)
r2=(-b-SQRT(d))/(2*a)
ELSE
r1=-b/(2*a)
r2=r1
i1=SQRT(ABS(d))/(2*a)
i2=-i1
ENDIF
ENDIF
END
Theanswersforthe3testcasesare:
(a)0.3542,5.646;(b)0.4;(c)0.4167+1.4696i;0.41671.4696i.
Severalfeaturesofthissubroutinebearmention:
∙Thesubroutinedoesnotinvolveinputoroutput.Rather,informationispassedinandoutviathearguments.Thisisoftenthepreferredstyle,becausetheI/Oislefttothediscretionoftheprogrammerwithinthecallingprogram.
∙Notethatanerrorcodeispassed(IER=1)forthecasewherenorootsarepossible.
2.6Thedevelopmentofthealgorithmhingesonrecognizingthattheseriesapproximationofthecosinecanberepresentedconciselybythesummation,
wherei=theorderoftheapproximation.Thefollowingalgorithmimplementsthissummation:
Step1:
Start
Step2:
Inputvaluetobeevaluatedxandmaximumordern
Step3:
Setorder(i)equaltoone
Step4:
Setaccumulatorforapproximation(approx)tozero
Step5:
Setaccumulatorforfactorialproduct(factor)equaltoone
Step6:
Calculatetruevalueofcos(x)
Step7:
Iforderisgreaterthannthenproceedtostep13
Otherwise,proceedtonextstep
Step8:
Calculatetheapproximationwiththeformula
Step9:
Determinetheerror
Step10:
Incrementtheorderbyone
Step11:
Determinethefactorialforthenextiteration
Step12:
Returntostep7
Step13:
End
2.7(a)Structuredflowchart
(b)Pseudocode:
SUBROUTINECoscomp(n,x)
i=1
approx=0
factor=1
truth=cos(x)
DO
IFi>nEXIT
approx=approx+(-1)i-1•x2i-2/factor
error=(true-approx)/true)*100
DISPLAYi,true,approx,error
i=i+1
factor=factor•(2•i-3)•(2•i-2)
ENDDO
END
2.8Studentscouldimplementthesubprograminanynumberoflanguages.ThefollowingMATLABM-fileisoneexample.ItshouldbenotedthatMATLABallowsdirectcalculationofthefactorialthroughitsintrinsicfunctionfactorial.However,wedidnotexploitthisfeature,inordertomakethecodemorecompatiblewithlanguagessuchasVisualBASICandFortran.
functioncoscomp(x,n)
i=1;
tru=cos(x);
approx=0;
f=1;
fprintf('\n');
fprintf('ordertruevalueapproximationerror\n');
while
(1)
ifi>n,break,end
approx=approx+(-1)^(i-1)*x^(2*i-2)/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-3)*(2*i-2);
end
Hereisarunoftheprogramshowingtheoutputthatisgenerated:
>>coscomp(1.25,6)
ordertruevalueapproximationerror
10.31532236241.0000000000-217.13576938
20.31532236240.218750000030.62655045
30.31532236240.3204752604-1.63416828
40.31532236240.31517706980.04607749
50.31532236240.3153248988-0.00080437
60.31532236240.31532233230.00000955
2.9(a)Thefollowingpseudocodeprovidesanalgorithmforthisproblem.Noticethattheinputofthequizzesandhomeworksisdonewithlogicalloopsthatterminatewhentheuserentersanegativegrade:
INPUTWQ,WH,WF
nq=0
sumq=0
DO
INPUTquiz(enternegativetosignalendofquizzes)
IFquiz<0EXIT
nq=nq+1
sumq=sumq+quiz
ENDDO
AQ=sumq/nq
nh=0
sumh=0
DO
INPUThomework(enternegativetosignalendofhomeworks)
IFhomework<0EXIT
nh=nh+1
sumh=sumh+homework
ENDDO
AH=sumh/nh
DISPLAY"Isthereafinalgrade(yorn)"
INPUTanswer
IFanswer="y"THEN
INPUTFE
AG=(WQ*AQ+WH*AH+WF*FE)/(WQ+WH+WF)
ELSE
AG=(WQ*AQ+WH*AH)/(WQ+WH)
ENDIF
DISPLAYAG
END
(b)Studentscouldimplementtheprograminanynumberoflanguages.ThefollowingVBAcodeisoneexample.
SubGrader()
DimWQAsDouble,WHAsDouble,WFAsDouble
DimnqAsInteger,sumqAsDouble,AQAsDouble
DimnhAsInteger,sumhAsDouble,AHAsDouble
DimanswerAsString,FEAsDouble
DimAGAsDouble
'enterweights
WQ=InputBox("enterquizweight")
WH=InputBox("enterhomeworkweight")
WF=InputBox("enterfinalexamweight")
'enterquizgrades
nq=0
sumq=0
Do
quiz=InputBox("enternegativetosignalendofquizzes")
Ifquiz<0ThenExitDo
nq=nq+1
sumq=sumq+quiz
Loop
AQ=sumq/nq
'enterhomeworkgrades
nh=0
sumh=0
Do
homework=InputBox("enternegativetosignalendofhomeworks")
Ifhomework<0ThenExitDo
nh=nh+1
sumh=sumh+homework
Loop
AH=sumh/nh
'determineanddisplaytheaveragegrade
answer=InputBox("Isthereafinalgrade(yorn)")
Ifanswer="y"Then
FE=InputBox("finalgrade:
")
AG=(WQ*AQ+WH*AH+WF*FE)/(WQ+WH+WF)
Else
AG=(WQ*AQ+WH*AH)/(WQ+WH)
EndIf
MsgBox"Averagegrade="&AG
EndSub
Theresultsshouldconformto:
AQ=437/5=87.4
AH=541/6=90.1667
withoutfinal
withfinal
2.10(a)Pseudocode:
IFa>0THEN
tol=10–5
x=a/2
DO
y=(x+a/x)/2
e=(y–x)/y
x=y
IFe ENDDO SquareRoot=x ELSE SquareRoot=0 ENDIF (b)Studentscouldimplementthefunctioninanynumberoflanguages.ThefollowingVBAandMATLABcodesaretwopossibleoptions. VBAFunctionProcedure MATLABM-File OptionExplicit FunctionSquareRoot(a) DimxAsDouble,yAsDouble DimeAsDouble,tolAsDouble Ifa>0Then tol=0.00001 x=a/2 Do y=(x+a/x)/2 e=Abs((y-x)/y) x=y Ife Loop SquareRoot=x Else SquareRoot=0 EndIf EndFunction functions=SquareRoot(a) ifa>0 tol=0.00001; x=a/2; while (1) y=(x+a/x)/2; e=abs((y-x)/y); x=y; ife end s=x; else s=0; end 2.11AMATLABM-filecanbewrittentosolvethisproblemas functionfutureworth(P,i,n) nn=0: n; F=P*(1+i).^nn; y=[nn;F]; fprintf('\nyearfutureworth\n'); fprintf('%5d%14.2f\n',y); Thisfunctioncanbeusedtoevaluatethetestcase, >>futureworth(100000,0.06,5) yearfutureworth 0100000.00 1106000.00 2112360.00 3119101.60 4126247.70 5133822.56 2.12AMATLABM-filecanbewrittentosolvethisproblemas functionannualpayment(P,i,n) nn=1: n; A=P*i*(1+i).^nn./((1+i).^nn-1); y=[nn;A]; fprintf('\nyearannualpayment\n'); fprintf('%5d%14.2f\n',y); Thisfunctioncanbeusedtoevaluatethetestcase, >>annualpayment(55000,0.066,5) yearannualpayment 158630.00 230251.49 320804.86 416091.17 513270.64 2.13Studentscouldimplementthefunctioninanynumberoflanguages.ThefollowingVBAandMATLABcodesaretwopossibleoptions. VBAFunctionProcedure MATLABM-File OptionExplicit Functionavgtemp(Tm,Tp,ts,te) DimpiAsDouble,wAsDouble DimTempAsDouble,tAsDouble DimsumAsDouble,iAsInteger DimnAsInteger pi=4*Atn (1) w=2*pi/365 sum=0 n=0 t=ts Fori=tsTote Temp=Tm+(Tp-Tm)*Cos(w*(t-205)) sum=sum+Temp n=n+1 t=t+1 Nexti avgtemp=sum/n EndFunction functionTa=avgtemp(Tm,Tp,ts,te) w=2*pi/365; t=ts: te; T=Tm+(Tp-Tm)*cos(w*(t-205)); Ta=mean(T); Thefunctioncanbeusedtoevaluatethetestcases.ThefollowingshowtheresultsforMATLAB, >>avgtemp(22.1,28.3,0,59) ans= 16.2148 >>avgtemp(10.7,22.9,180,242) ans= 22.2491 2.14TheprogramsarestudentspecificandwillbesimilartothecodesdevelopedforVBA,MATLABandFortranasoutlinedinsections2.4,2.5and2.6.Thenumericalresultsforthedifferenttimestepsaretabulatedbelowalongwithanestimateoftheabsolutevalueofthetruerelativeerroratt=12s: Step v(12) t(%) 2 49.96 5.2 1 48.70 2.6 0.5 48.09 1.3 Thegeneralconclusionisthattheerrorishalvedwhenthestepsizeishalved. 2.15Studentscouldimplementthesubprograminanynumberoflanguages.Thefo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- numerical analysis chapra 5ESM2 ESM2