Homework 8 31

Homework 8 31 - 31.1) For this problem we modified...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: 31.1) For this problem we modified mymodeuler.m to myRK2.m which does the Runga‐Kutta 2 method. The following code is the modification to create myRK2.m function [T , Y] = myRK2(f,tspan,y0,n) % Math 344 % Homework Problem 31.1 % Writen by Astrom and Elbert % % Solves dy/dt = f(t,y) with initial condition y(a) = y0 % on the interval [a,b] using n steps of the Runga-Kutta 2 method. % Inputs: f -- a function f(t,y) that returns a column vector of % the same length as y % tspan -- a vector [a,b] with the start and end times % y0 -- a column vector of the initial values, y(a) = y0 % n -- number of steps to use % Outputs: T -- a n+1 column vector contianing the times % Y -- a (n+1) by d matrix where d is the length of y % Y(t,i) gives the ith component of y at time t % parse starting and ending points format long a = tspan(1); b = tspan(2); h = (b-a)/n; % step size t = a; y = y0; % t and y are the current variables T = a; Y = y0'; % T and Y will record all steps for i = 1:n k1 = h*f(t,y); k2 = h*f(t+(h/2),y+(k1/2)); y = y + k2; t = a + i*h; T = [T; t]; Y = [Y; y']; end In order to conserve paper the output for n=1000 is not shown >> [T Y] = mymodeuler(f,[0 4*pi],[1;0],10) T = 0 1.256637061435917 2.513274122871835 3.769911184307752 5.026548245743669 6.283185307179586 7.539822368615504 8.796459430051421 10.053096491487338 11.309733552923255 12.566370614359172 >> [T Y] = myRK2(f,[0 4*pi],[1;0],10) T = 0 1.256637061435917 2.513274122871835 3.769911184307752 5.026548245743669 6.283185307179586 7.539822368615504 8.796459430051421 10.053096491487338 11.309733552923255 12.566370614359172 >> [T Y] = mymodeuler(f,[0 4*pi],[1;0],100) T = 3.015928947446202 3.141592653589794 0 3.267256359733385 0.125663706143592 3.392920065876977 0.251327412287183 3.518583772020569 0.376991118430775 3.644247478164160 0.502654824574367 3.769911184307752 0.628318530717959 3.895574890451344 0.753982236861550 4.021238596594936 0.879645943005142 4.146902302738527 1.005309649148734 4.272566008882119 1.130973355292326 4.398229715025710 1.256637061435917 4.523893421169302 1.382300767579509 4.649557127312894 1.507964473723101 4.775220833456486 1.633628179866693 4.900884539600078 1.759291886010284 5.026548245743669 1.884955592153876 5.152211951887261 2.010619298297468 5.277875658030853 2.136283004441060 5.403539364174445 2.261946710584651 5.529203070318037 2.387610416728243 5.654866776461629 2.513274122871835 5.780530482605220 2.638937829015426 5.906194188748811 2.764601535159018 6.031857894892403 2.890265241302610 6.157521601035995 Y = 1.000000000000000 0 0.210431647912851 ‐1.256637061435917 ‐1.534855225730979 ‐0.528872415332646 ‐0.987582792336354 1.817464466698040 2.076074532253907 1.623485180836554 2.477003431943297 ‐2.267239537567966 ‐2.327857315991120 ‐3.589793265987505 ‐5.000922112042981 2.169865664380975 1.674381331077382 6.740952474852795 8.823273532011317 ‐0.685579897822754 0.995170881147465 ‐11.231920231186775 Y = 1.000000000000000 0 0.210431647912851 ‐1.256637061435917 ‐1.534855225730979 0.528872415332646 ‐0.987582792336354 1.817464466698040 2.076074532253907 1.623485180836554 2.477003431943297 ‐2.267239537567967 ‐2.327857315991122 ‐3.589793265987506 ‐5.000922112042982 2.169865664380977 1.674381331077384 6.740952474852797 8.823273532011321 ‐0.685579897822755 0.995170881147463 ‐11.231920231186779 6.283185307179587 6.408849013323178 6.534512719466770 6.660176425610362 6.785840131753954 6.911503837897546 7.037167544041138 7.162831250184729 7.288494956328321 7.414158662471913 7.539822368615504 7.665486074759096 7.791149780902687 7.916813487046279 8.042477193189871 8.168140899333462 8.293804605477055 8.419468311620646 8.545132017764239 8.670795723907830 8.796459430051421 8.922123136195014 9.047786842338605 9.173450548482197 9.299114254625788 9.424777960769379 9.550441666912972 9.676105373056563 9.801769079200156 9.927432785343747 10.053096491487338 10.178760197630931 10.304423903774522 10.430087609918115 10.555751316061706 10.681415022205298 10.807078728348889 10.932742434492480 11.058406140636073 11.184069846779664 11.309733552923257 11.435397259066848 11.561060965210439 11.686724671354032 11.812388377497623 11.938052083641216 12.063715789784807 12.189379495928398 12.315043202071990 12.440706908215581 12.566370614359174 Y = 1.000000000000000 0 ‐0.416067288203912 0.910499761277239 0.992104316479129 ‐0.125663706143592 ‐0.298365378127913 0.955595300757183 0.968479607734776 ‐0.249343010579644 ‐0.175925932462082 0.985543921888860 0.929499432445245 ‐0.369077013912410 ‐0.050689775184403 0.999869883665659 0.875780813673185 ‐0.482967242155164 0.075357810480986 0.998345092515442 0.808174471939602 ‐0.589207748475781 0.200218553394360 0.980992733868418 0.727751352717532 ‐0.686113749920342 0.321912873699591 0.948086920247274 0.635785661703520 ‐0.772148345025150 0.438511267672101 0.900148361210300 0.533734676603100 ‐0.845946888624287 0.548164900630993 0.837936123546951 0.423215655287360 ‐0.906338637275541 0.649134922854865 0.762435612113595 0.305980206230955 ‐0.952365321974273 0.739820043655710 0.674842961622127 0.183886527396983 ‐0.983296353517274 0.818781926350961 0.576546086605781 0.058869953552444 ‐0.998640419245255 0.884768001393351 0.469102689782093 ‐0.067087721156843 ‐0.998153287087645 0.936731335824900 0.354215577272437 ‐0.191989659097295 ‐0.981841692950187 0.973847243866990 0.233705671855537 ‐0.313855635492201 ‐0.949963249571644 0.995526375103888 0.109483151951485 ‐0.430753433364495 ‐0.903022378046566 1.001424072543893 ‐0.016482826226442 ‐0.540829479335052 ‐0.841762326266586 0.991445851965569 ‐0.142195343424303 ‐0.642338234538175 ‐0.767153400573641 0.965748915439309 ‐0.265661374193167 ‐0.733669874621649 ‐0.680377596960952 0.924739674782063 ‐0.384923383977076 ‐0.813375819894023 ‐0.582809875249797 0.869067323958898 ‐0.498090365508544 ‐0.880191710737585 ‐0.475996372906520 0.799613562086368 ‐0.603367822434661 ‐0.933057463882247 ‐0.361629903700815 0.717478629726209 ‐0.699086224756492 ‐0.971134091392040 ‐0.241523129473126 0.623963879616069 ‐0.783727484862235 ‐0.993817015519071 ‐0.117579830193123 0.520551157939782 ‐0.855949034287443 ‐1.000745668116678 0.008235272333210 0.408879322821677 ‐0.914605119346765 ‐0.991809222193852 0.133927658791845 0.290718272135433 ‐0.958764977849088 ‐0.967148364503410 0.257504631031606 0.167940892227722 ‐0.987727608533296 ‐0.927153080814993 0.377006903834034 0.042493372143838 ‐1.001032898861887 ‐0.872456488735971 0.490539668933838 ‐0.083635646116992 ‐0.998468934528627 ‐0.803924815606505 0.596302638777090 ‐0.208446592306299 ‐0.980075374559392 ‐0.722643680115968 0.692618593648562 ‐0.329960667849647 ‐0.946142838253059 ‐0.629900894897574 0.777959979497059 ‐0.446251218438067 ‐0.897208313433073 ‐0.527166062526956 0.850973134663041 ‐0.555474281895318 ‐0.834046658557889 >> [T Y] = myRK2(f,[0 4*pi],[1;0],100) T = 0 6.534512719466770 3.267256359733385 3.392920065876977 0.125663706143592 6.660176425610362 3.518583772020569 0.251327412287183 6.785840131753954 3.644247478164160 0.376991118430775 6.911503837897546 3.769911184307752 0.502654824574367 7.037167544041138 3.895574890451344 0.628318530717959 7.162831250184729 4.021238596594936 0.753982236861550 7.288494956328321 4.146902302738527 0.879645943005142 7.414158662471913 4.272566008882119 1.005309649148734 7.539822368615504 7.665486074759096 4.398229715025710 1.130973355292326 7.791149780902687 4.523893421169302 1.256637061435917 7.916813487046279 4.649557127312894 1.382300767579509 8.042477193189871 4.775220833456486 1.507964473723101 8.168140899333462 4.900884539600078 1.633628179866693 8.293804605477055 5.026548245743669 1.759291886010284 8.419468311620646 5.152211951887261 1.884955592153876 8.545132017764239 5.277875658030853 2.010619298297468 8.670795723907830 5.403539364174445 2.136283004441060 8.796459430051421 5.529203070318037 2.261946710584651 8.922123136195014 5.654866776461629 2.387610416728243 9.047786842338605 5.780530482605220 2.513274122871835 9.173450548482197 5.906194188748811 2.638937829015426 9.299114254625788 6.031857894892403 2.764601535159018 9.424777960769379 6.157521601035995 2.890265241302610 9.550441666912972 6.283185307179587 3.015928947446202 9.676105373056563 6.408849013323178 3.141592653589794 ‐0.655897826972552 ‐0.757658333169860 ‐0.745929219445450 ‐0.669253550965301 ‐0.824140479963797 ‐0.570233106495347 ‐0.889290933085243 ‐0.462166179258127 ‐0.940346888259421 ‐0.346765466881270 ‐0.976498040560802 ‐0.225860041459536 ‐0.997170330953333 ‐0.101366359233335 ‐1.002035061983266 0.024742116902862 ‐0.991014124149178 0.150466200552802 ‐0.964281249849372 0.273812674733880 ‐0.922259274779302 0.392825892131025 ‐0.865613449947109 0.505618781702023 ‐0.795240910090433 0.610402770027589 ‐0.712256465219528 0.705516142974155 ‐0.617974940314195 0.789450397934585 ‐0.513890342940652 0.860874168740814 ‐0.401652188861106 0.918654343798467 ‐0.283039360785769 0.961874042467733 ‐0.159931914549241 0.989847164502319 ‐0.034281279559986 1.002129281670275 0.091920674143812 0.998524698656060 0.216673211900781 0.979089571063569 0.337998452938550 0.944131030842378 0.453972728544426 0.894202332753554 0.562757082728789 0.830094098573495 0.662626431779103 0.752821797604360 0.751996920333856 0.663609661736797 0.829451040168771 0.563871289848611 0.893760113329884 0.455187248825196 0.943903783008133 0.339280026121386 0.979086202966255 0.217986730794420 0.998748668649734 0.093229975657521 1.002578489509985 ‐0.033012597933433 9.801769079200156 9.927432785343747 10.053096491487338 10.178760197630931 10.304423903774522 10.430087609918115 10.555751316061706 10.681415022205298 10.807078728348889 10.932742434492480 11.058406140636073 11.184069846779664 11.309733552923257 11.435397259066848 11.561060965210439 11.686724671354032 11.812388377497623 11.938052083641216 12.063715789784807 12.189379495928398 12.315043202071990 12.440706908215581 12.566370614359174 Y = 1.000000000000000 0 0.992104316479129 ‐0.125663706143592 0.968479607734776 ‐0.249343010579644 0.929499432445245 ‐0.369077013912410 0.875780813673185 ‐0.482967242155164 0.808174471939602 ‐0.589207748475781 0.727751352717532 ‐0.686113749920342 0.635785661703520 ‐0.772148345025150 0.533734676603100 ‐0.845946888624287 0.423215655287360 ‐0.906338637275541 0.305980206230955 ‐0.952365321974273 0.183886527396983 ‐0.983296353517274 0.058869953552444 ‐0.998640419245255 ‐0.067087721156843 ‐0.998153287087645 ‐0.191989659097295 ‐0.981841692950187 ‐0.313855635492201 ‐0.949963249571644 ‐0.430753433364495 ‐0.903022378046566 ‐0.540829479335052 ‐0.841762326266586 ‐0.642338234538175 ‐0.767153400573641 ‐0.733669874621649 ‐0.680377596960952 ‐0.813375819894023 ‐0.582809875249797 ‐0.880191710737585 ‐0.475996372906520 ‐0.933057463882247 ‐0.361629903700815 ‐0.971134091392040 ‐0.241523129473126 ‐0.993817015519071 ‐0.117579830193123 ‐1.000745668116678 0.008235272333210 ‐0.991809222193852 0.133927658791845 ‐0.967148364503410 0.257504631031606 ‐0.927153080814993 0.377006903834034 ‐0.872456488735971 0.490539668933838 ‐0.803924815606505 0.596302638777090 ‐0.722643680115968 0.692618593648562 ‐0.629900894897574 0.777959979497059 ‐0.527166062526956 0.850973134663041 ‐0.416067288203912 0.910499761277239 ‐0.298365378127913 0.955595300757183 ‐0.175925932462082 0.985543921888860 ‐0.050689775184403 0.999869883665659 0.075357810480986 0.998345092515442 0.200218553394360 0.980992733868418 0.321912873699591 0.948086920247274 0.438511267672101 0.900148361210300 0.548164900630993 0.837936123546951 0.649134922854865 0.762435612113594 0.739820043655710 0.674842961622127 0.818781926350961 0.576546086605781 0.884768001393351 0.469102689782093 0.936731335824901 0.354215577272437 0.973847243866990 0.233705671855537 0.995526375103888 0.109483151951485 1.001424072543893 ‐0.016482826226442 0.991445851965569 ‐0.142195343424303 0.965748915439309 ‐0.265661374193167 0.924739674782063 ‐0.384923383977076 0.869067323958898 ‐0.498090365508544 0.799613562086367 ‐0.603367822434662 0.717478629726209 ‐0.699086224756492 0.623963879616069 ‐0.783727484862235 0.520551157939781 ‐0.855949034287444 0.408879322821677 ‐0.914605119346765 0.290718272135432 ‐0.958764977849088 0.167940892227721 ‐0.987727608533296 0.042493372143838 ‐1.001032898861887 ‐0.083635646116993 ‐0.998468934528627 ‐0.208446592306300 ‐0.980075374559392 ‐0.329960667849647 ‐0.946142838253059 ‐0.446251218438067 ‐0.897208313433073 ‐0.555474281895318 ‐0.834046658557889 ‐0.655897826972552 ‐0.757658333169860 ‐0.745929219445451 ‐0.669253550965300 ‐0.824140479963797 ‐0.570233106495347 ‐0.889290933085243 ‐0.462166179258127 ‐0.940346888259421 ‐0.346765466881270 ‐0.976498040560802 ‐0.225860041459536 ‐0.997170330953333 ‐0.101366359233335 ‐1.002035061983266 0.024742116902863 ‐0.991014124149178 0.150466200552802 ‐0.964281249849372 0.273812674733880 ‐0.922259274779302 0.392825892131025 ‐0.865613449947109 0.505618781702023 ‐0.795240910090433 0.610402770027589 ‐0.712256465219527 0.705516142974155 ‐0.617974940314195 0.789450397934585 ‐0.513890342940651 0.860874168740814 ‐0.401652188861105 0.918654343798467 ‐0.283039360785768 0.961874042467732 ‐0.159931914549240 0.989847164502319 ‐0.034281279559986 1.002129281670274 0.091920674143813 0.998524698656060 0.216673211900782 0.979089571063569 0.337998452938550 0.944131030842377 0.453972728544426 0.894202332753553 0.562757082728789 0.830094098573495 0.662626431779103 0.752821797604360 0.751996920333856 0.663609661736796 0.829451040168771 0.563871289848610 0.893760113329884 0.455187248825195 0.943903783008133 0.339280026121386 0.979086202966255 0.217986730794420 0.998748668649734 0.093229975657521 1.002578489509985 ‐0.033012597933433 Errors for n = 10 (yr: myRK2 output; e2:myRK2 error; ye: mymodeuler output; e1: mymodeuler error) >> yr = [0.995170881147463 ‐11.231920231186779] yr = 0.995170881147463 ‐11.231920231186779 >> e2 = abs(y ‐ yr) e2 = 0.004829118852537 11.231920231186779 >> ye = [0.995170881147465 ‐11.231920231186775] ye = 0.995170881147465 ‐11.231920231186775 >> e1 = abs(y ‐ ye) e1 = 0.004829118852535 11.231920231186775 Errors for n = 100 (yr: myRK2 output; e2:myRK2 error; ye:mymodeuler output; e1:mymodeuler error) >> yr = [1.002578489509985 ‐0.033012597933433] yr = 1.002578489509985 ‐0.033012597933433 >> e2 = abs(y ‐ yr) e2 = 0.002578489509985 0.033012597933433 >> ye = [1.002578489509985 ‐0.033012597933433] ye = 1.002578489509985 ‐0.033012597933433 >> e1 = abs(y ‐ ye) e1 = 0.002578489509985 0.033012597933433 Error for n = 1000 (yr: myRK2 output; e2:myRK2 error; ye:mymodeuler output; e1:mymodeuler error) >> ye = [1.000003062408412 ‐0.000330718973683] e = 1.000003062408412 ‐0.000330718973683 >> e1 = abs(y ‐ ye) e1 = 1.0e‐003 * 0.003062408411969 0.330718973683000 >> yr = [1.000003062408413 ‐0.000330718973683] yr = 1.000003062408413 ‐0.000330718973683 >> e2 = abs(y ‐ yr) e2 = 1.0e‐003 * 0.003062408413079 0.330718973683000 Thus, we can see that as the number of intervals increases, the Runga‐Kutta 2 method and the modified Euler method become very accurate. As is shown, we are at the worst, by n=1000, within 3 ten‐thousands of the exact answer. Continuing with more intervals would continue to decrease the error. ...
View Full Document

Ask a homework question - tutors are online