ATILIM UNIVERSITY Department of Computer Engineering COMPE 350 Numerical Methods Fall, 2011 Instructor: Fügen Selbes Assistant: İsmail Onur Kaya Homework: 1 Due date: Nov 14, 2011 You are designing a spherical tank to hold water for a small village in a developing country. The volume of liquid that is hold can be computed as V = π.h 2.[3R - h] / 3 where V = volume [m 3 ], h = depth of water in tank [m] and R = the tank radius [m]. For example, if depth of the water in tank is 4 m. and the tank radius is 2 m., then the volume of water is 33.5103 m 3. You are going to write the following MATLAB functions that will calculate the depth(h) of the water in the tank when the volume of water(v) and tank radius(r) is given. a. Write a MATLAB function called fixed_pt that takes volume(v), radius(r) and accuracy as inputs and gives the depth(h) of water and relative error as outputs. Use fixed-point method in determining the depth(h) of water and take the initial value of h as 2R (i.e. h 0 =2R).
The fixed_pt function should have the form: where function [h,rel_err]=fixed_pt(v,r,acc) V : Volume of the water R : Radius of the tank acc: Iterations for h are performed until relative error is less than this number. h: The water depth estimates when fixed-point method is used. It is a vector (h 1, h 2, h 3, h 4, ). rel_err: The relative error estimates determined at each step of iteration. It is a vector. b. Write a MATLAB function called newton_raphson that takes volume(v), radius(r) and accuracy as inputs and gives the depth(h) of water and relative error as outputs. Use Newton-Raphson method in determining the depth(h) of water and take the initial value of h as 2R (i.e. h 0 =2R). The newton_raphson function should have the form: where function [h,rel_err]= newton_raphson (V,R,acc) V : Volume of the water R : Radius of the tank acc: Iterations for h are performed until relative error is less than this number. h: The water depth estimates when Newton-Raphson method is used. It is a vector (h 1, h 2, h 3, h 4, ). rel_err: The relative error estimates determined at each step of iteration. It is a vector. c. Test your functions with datas given below: V = 24 R = 4 acc = 10-12 V = 40 R = 4 acc = 10-12 V = 122 R = 4.6 acc = 10-12 V = 256 R = 5 acc = 10-12 V = 972 R = 9 acc = 10-12 For each data give the results as a table: Fixed-Point Method Newton_Raphson Method i h i h (h i -h i-1 )/h i i (h i -h i-1 )/h i...............
NOTES: This is a group assignment with 2 students in each. Mail your homework to ismailonurkaya@gmail.com Attach the report(word document) and the script file(s) to your mail. Cheating is strictly forbidden. Late homework will be graded out of 10-d 2 (d is the number of late days). Don t forget to write your name. Don t forget to insert page numbers.
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % HOMEWORK PART 1 % PROGRAM İLE İLGİLİ BİLGİLER AŞAĞIDA YER ALMAKTADIR... % % % % % % % % % % % % % % % % % % % % % % % % % % % % fixed_pt isimli programdır. % h değerini hesaplamak için fixed point methodu kullanıyor. % format long satırı, sayı formatını ayarlıyor. % h(0) yerine h(1) kullanıldı, programın devamı da ona göre kodlandı. % rel_err değerini başlangıç olarak random yerine 1 yapıldı. (önemsiz) % kaçıncı adımda olduğumuzu göstermek için i diye değişken tanımladık. % n=2 den başlattığımız için i'yi n-1 olarak tanımladık. % yani h(1)'de n=1 ancak eşit olduğu değer o h(0) değeridir. % mutlak relative error, acc'den büyük olduğunda döngü çalışır. % döngü içerisine de ana formülden çıkarılan, converge formül kullanıldı. % % % % % % % % % % % % % % % % % % % % % % % % % % % % function [i,h,rel_err] = fixed_pt(v,r,acc) format long; h(1)=2*r; rel_err=1; n=2; while (abs(rel_err)>abs(acc)) i=n-1 h(n)=sqrt((3*v+pi*h(n-1)^3)/(3*pi*r)); height=h(n) rel_err=(h(n)-h(n-1))/(h(n)) end n=n+1; end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % HOMEWORK PART 2 % PROGRAM İLE İLGİLİ BİLGİLER AŞAĞIDA YER ALMAKTADIR... % % % % % % % % % % % % % % % % % % % % % % % % % % % % newton_raphson isimli programdır. % h değerini hesaplamak için newton-raphson methodu kullanıyor. % format long satırı, sayı formatını ayarlıyor. % h(0) yerine h(1) kullanıldı, programın devamı da ona göre kodlandı. % rel_err değerini başlangıç olarak random yerine 1 yapıldı. (önemsiz) % kaçıncı adımda olduğumuzu göstermek için i diye değişken tanımladık. % n=2 den başlattığımız için i'yi n-1 olarak tanımladık. % yani h(1)'de n=1 ancak eşit olduğu değer o h(0) değeridir. % mutlak relative error, acc'den büyük olduğunda döngü çalışır. % döngü içinde h(n+1)=h(n)-f(h(n))/f'(h(n))'e uygun formül yazıldı. % % % % % % % % % % % % % % % % % % % % % % % % % % % % function [i,h,rel_err] = newton_raphson(v,r,acc) format long; h(1)=2*r; rel_err=1; n=2; while (abs(rel_err)>abs(acc)) i=n-1 h(n)=h(n-1)-(3*r*pi*h(n-1)^2-pi*h(n-1)^3-3*v)/(3*r*pi*2*h(n-1)-pi*3*h(n-1)^2); height=h(n) rel_err=(h(n)-h(n-1))/(h(n)) end n=n+1; end
ATILIM UNIVERSITY DEPARTMENT OF COMPUTER ENGINERING COMPE 350: NUMERICAL METHODS HOMEWORK: 1 Instructor: Fügen Selbes Asistant: İsmail Onur Kaya Submitted to: ismailonurkaya@gmail.com Submitted by: 2011-2012 FALL SEMESTER 1
PART 1: Matlab script of fixed_pt function(attached to email); Note: Instructions are given in the program. PART 2: Matlab script of newton_raphson function(attached to email); Note: Instructions are given in the program. Data table of V=24, R=4, acc=10-12 ; i 0 8-8 - 1 6.676565433197626-0.198220863712638 Inf NaN 2 5.168304076455702-0.291829067026616 3 3.662544412666732-0.411123933018088 4 2.450315127855570-0.494723830021024 5 1.770831071371559-0.383709133789781 6 1.540328954688349-0.149644733991154 7 1.488089288092308-0.035105196317225 8 1.477992760551278-0.006831242892736 9 1.476113469952023-0.001273134239007 10 1.475766231685239-2.352935440104576e-004 11 1.475702159907828-4.341782451192433e-005 12 1.475690340489799-8.009416138484517e-006 13 1.475688160245599-1.477442361184524e-006 14 1.475687758074881-2.725310392274012e-007 15 1.475687683890059-5.027135708800363e-008 16 1.475687670205854-9.273103457149981e-009 17 1.475687667681653-1.710525587329136e-009 18 1.475687667216036-3.155252067807547e-010 19 1.475687667130148-5.820199309184297e-011 20 1.475687667114305-1.073608254583232e-011 21 1.475687667111383-1.980316777423989e-012 22 1.475687667110843-3.653376746134185e-013 2
Data table of V=40, R=4, acc=10-12 ; i 0 8-8 - 1 6.771245493150029-0.181466542321204 Inf NaN 2 5.390247401085649-0.256203099654801 3 4.029160054062480-0.337809203099496 4 2.938354094800044-0.371230261591963 5 2.301570380422386-0.276673578959074 6 2.049169044988807-0.123172529885136 7 1.974880572816743-0.037616690950637 8 1.955750565204880-0.009781414845128 9 1.951022265344633-0.002423498667460 10 1.949865998567737-5.929980715313285e-004 11 1.949583990363046-1.446504516271327e-004 12 1.949515254374658-3.525798951004434e-005 13 1.949498503479871-8.592412231726048e-006 14 1.949494421459983-2.093886416370682e-006 15 1.949493426723271-5.102539449645735e-007 16 1.949493184319048-1.243421754600044e-007 17 1.949493125248367-3.030053312230587e-008 18 1.949493110853630-7.383835424548332e-009 19 1.949493107345826-1.799342055733439e-009 20 1.949493106491020-4.384756913365548e-010 21 1.949493106282716-1.068505880328371e-010 22 1.949493106231955-2.603813928884029e-011 23 1.949493106219585-6.345065192433856e-012 24 1.949493106216571-1.546173978376943e-012 25 1.949493106215836-3.768905852266102e-013 3
Data table of V=122, R=4.6, acc=10-12 ; i 0 9.2-9.2-1 8.054116862243280-0.142272971370515 Inf NaN 2 6.804529084309755-0.183640596204503 3 5.592192483552245-0.216790928481670 4 4.595081734325325-0.216995215074955 5 3.933556311526628-0.168174895796002 6 3.585041437799593-0.097213624939561 7 3.432350809433996-0.044485729123585 8 3.372287458904286-0.017810863178676 9 3.349801633555711-0.006712584149261 10 3.341549098764428-0.002469673360279 11 3.338542919292028-9.004465556002520e-004 12 3.337450860354090-3.272134882676707e-004 13 3.337054544892310-1.187620569122103e-004 14 3.336910771862002-4.308566819376852e-005 15 3.336858621623565-1.562854299493914e-005 16 3.336839706277301-5.668640968527946e-006 17 3.336832845635871-2.056033894116648e-006 18 3.336830357280516-7.457242618499118e-007 19 3.336829454755842-2.704737195425004e-007 20 3.336829127411073-9.810054905834517e-008 21 3.336829008683498-3.558095862079221e-008 22 3.336828965621146-1.290517191804491e-008 23 3.336828950002483-4.680690405663046e-009 24 3.336828944337612-1.697680908782529e-009 25 3.336828942282970-6.157468139896262e-010 26 3.336828941537755-2.233305638538774e-010 27 3.336828941267465-8.100179217243471e-011 28 3.336828941169432-2.937926485930801e-011 29 3.336828941133875-1.065575949595211e-011 30 3.336828941120979-3.864852193985527e-012 31 3.336828941116302-1.401807443502294e-012 32 3.336828941114605-5.083930916340535e-013 4
Data table of V=256, R=5, acc=10-12 ; i 0 10-10 - 1 9.108464900260458-0.097879841389524 Inf NaN 2 8.165528701136461-0.115477666374838 3 7.252152014743063-0.125945606840090 4 6.459512696541984-0.122708841276124 5 5.853700398506544-0.103492194132450 6 5.446979399108880-0.074669090810992 7 5.203022253431635-0.046887584521927 8 5.068302150331197-0.026580913904597 9 4.997699139018342-0.014127103162661 10 4.961784003177117-0.007238351330535 11 4.943801802598251-0.003637322307180 12 4.934871317860543-0.001809669221847 13 4.930454281625397-8.958680038080412e-004 14 4.928274048453577-4.423928439011681e-004 15 4.927198977125835-2.181911736735283e-004 16 4.926669124015713-1.075479389390053e-004 17 4.926408047926959-5.299522212007837e-005 18 4.926279422697130-2.611001504224559e-005 19 4.926216056252837-1.286310701135021e-005 20 4.926184840072870-6.336786170348229e-006 21 4.926169462284171-3.121652394836775e-006 22 4.926161886895390-1.537787217530939e-006 23 4.926158155129088-7.575409038423808e-007 24 4.926156316800253-3.731771216163076e-007 25 4.926155411210312-1.838330028405650e-007 26 4.926154965102531-9.055902303082254e-008 27 4.926154745342900-4.461078514534888e-008 28 4.926154637085898-2.197596493832026e-008 29 4.926154583756824-1.082570057090419e-008 30 4.926154557486097-5.332907569304207e-009 31 4.926154544544731-2.627072716430074e-009 32 4.926154538169613-1.294136688878792e-009 33 4.926154535029133-6.375116212855979e-010 34 4.926154533482084-3.140479456833788e-010 35 4.926154532719983-1.547049731759994e-010 36 4.926154532344561-7.621002495988032e-011 37 4.926154532159622-3.754230076987496e-011 38 4.926154532068518-1.849394139571696e-011 39 4.926154532023639-9.110304412352989e-012 40 4.926154532001530-4.487991003845482e-012 41 4.926154531990640-2.210820572443957e-012 42 4.926154531985275-1.089003120011094e-012 43 4.926154531982633-5.363881261646927e-013 5
Data table of V=972, R=9, acc=10-12 ; i 0 18-18 - 1 15.823320375567492-0.137561496118949 Inf NaN 2 13.457745277109963-0.175777966497931 3 11.164653374265894-0.205388544182622 4 9.269343979853627-0.204470715352846 5 7.992173259205075-0.159802681852218 6 7.299645764081001-0.094871383832317 7 6.984511964083788-0.045118943401875 8 6.855438286178838-0.018827925001553 9 6.805163875316950-0.007387685555118 10 6.785989119780408-0.002825639003848 11 6.778735926832403-0.001069991961081 12 6.776000924690076-4.036307215308987e-004 13 6.774970854268591-1.520405686818002e-004 14 6.774583078827683-5.723974987032678e-005 15 6.774437123515801-2.154500945550686e-005 16 6.774382190716064-8.108901770073291e-006 17 6.774361516306858-3.051860925369712e-006 18 6.774353735394082-1.148583773352507e-006 19 6.774350807020334-4.322737087810594e-007 20 6.774349704918042-1.626875405168680e-007 21 6.774349290138722-6.122792056609587e-008 22 6.774349134035377-2.304329779649780e-008 23 6.774349075285455-8.672408464266831e-009 24 6.774349053174761-3.263884625197530e-009 25 6.774349044853342-1.228371767418474e-009 26 6.774349041721553-4.623011053942921e-010 27 6.774349040542896-1.739882555222743e-010 28 6.774349040099305-6.548096933459872e-011 29 6.774349039932358-2.464404238005143e-011 30 6.774349039869527-9.274784812271301e-012 31 6.774349039845880-3.490647161374211e-012 32 6.774349039836981-1.313712611065788e-012 33 6.774349039833632-4.944122012306923e-013 6