Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80 views

Improved Euler's Method

How to adjust the code:

  • The function is given in the line that contains f(x,y)=1-x*4*y.
  • The initial condition is given by x0=0 and y0=1.
  • The number of iterations is given by n=80.
  • The step size is given by the line step=0.025.
  • The precision variable should be an integer. The numerical values will return with at least that many digits of precision.

Change these values to your desired values and "Evaluate".

That should be all!

2.7 #9 (a) ︠ac5f69f3-efe9-49aa-9b9c-5192dbbc0fdds︠ forget() var('x y a b') f(x,y)=(x+y)^(1/2) x0=0 y0=3 n=100 step=.025 precision=5 a=numerical_approx(x0,digits=precision) b=numerical_approx(y0,digits=precision) print "Improved Euler's Method" print "n", "(x(n),y(n))" for i in range(n+1): print i,a,b aux=f(a,b) a=numerical_approx(a+step,digits=precision) b=numerical_approx(b+(aux+f(a,b+step*aux))/2*step,digits=precision)
(x, y, a, b) Improved Euler's Method n (x(n),y(n)) 0 0.00000 3.0000 1 0.025000 3.0435 2 0.050000 3.0876 3 0.075000 3.1321 4 0.10000 3.1771 5 0.12500 3.2226 6 0.15000 3.2686 7 0.17500 3.3151 8 0.20000 3.3620 9 0.22500 3.4094 10 0.25000 3.4573 11 0.27500 3.5057 12 0.30000 3.5545 13 0.32500 3.6039 14 0.35000 3.6536 15 0.37500 3.7039 16 0.40000 3.7546 17 0.42500 3.8058 18 0.45000 3.8575 19 0.47500 3.9096 20 0.50000 3.9622 21 0.52500 4.0152 22 0.55000 4.0687 23 0.57500 4.1227 24 0.60000 4.1771 25 0.62500 4.2319 26 0.65000 4.2873 27 0.67500 4.3430 28 0.70000 4.3993 29 0.72500 4.4559 30 0.75000 4.5131 31 0.77500 4.5707 32 0.80000 4.6287 33 0.82500 4.6871 34 0.85000 4.7461 35 0.87500 4.8054 36 0.89999 4.8652 37 0.92499 4.9255 38 0.94999 4.9862 39 0.97499 5.0473 40 0.99999 5.1089 41 1.0250 5.1709 42 1.0500 5.2333 43 1.0750 5.2962 44 1.1000 5.3595 45 1.1250 5.4233 46 1.1500 5.4875 47 1.1750 5.5521 48 1.2000 5.6172 49 1.2250 5.6827 50 1.2500 5.7486 51 1.2750 5.8149 52 1.3000 5.8817 53 1.3250 5.9489 54 1.3500 6.0166 55 1.3750 6.0846 56 1.4000 6.1531 57 1.4250 6.2220 58 1.4500 6.2914 59 1.4750 6.3612 60 1.5000 6.4313 61 1.5250 6.5020 62 1.5500 6.5730 63 1.5750 6.6445 64 1.6000 6.7163 65 1.6250 6.7887 66 1.6500 6.8614 67 1.6750 6.9345 68 1.7000 7.0081 69 1.7250 7.0821 70 1.7500 7.1565 71 1.7750 7.2313 72 1.8000 7.3065 73 1.8250 7.3822 74 1.8500 7.4582 75 1.8750 7.5347 76 1.9000 7.6116 77 1.9250 7.6889 78 1.9500 7.7666 79 1.9750 7.8448 80 2.0000 7.9233 81 2.0250 8.0023 82 2.0500 8.0816 83 2.0750 8.1614 84 2.1000 8.2416 85 2.1250 8.3222 86 2.1500 8.4032 87 2.1750 8.4846 88 2.2000 8.5665 89 2.2250 8.6487 90 2.2500 8.7313 91 2.2750 8.8144 92 2.3000 8.8978 93 2.3250 8.9817 94 2.3500 9.0660 95 2.3750 9.1506 96 2.4000 9.2357 97 2.4250 9.3212 98 2.4500 9.4071 99 2.4750 9.4934 100 2.5000 9.5800
#9 (b) ︠2dbe308d-f967-41f7-88ab-ba4dd05b83aes︠ forget() var('x y a b') f(x,y)=(x+y)^(1/2) x0=0 y0=3 n=160 step=.0125 precision=5 a=numerical_approx(x0,digits=precision) b=numerical_approx(y0,digits=precision) print "Improved Euler's Method" print "n", "(x(n),y(n))" for i in range(n+1): print i,a,b aux=f(a,b) a=numerical_approx(a+step,digits=precision) b=numerical_approx(b+(aux+f(a,b+step*aux))/2*step,digits=precision)
(x, y, a, b) Improved Euler's Method n (x(n),y(n)) 0 0.00000 3.0000 1 0.012500 3.0217 2 0.025000 3.0435 3 0.037500 3.0655 4 0.050000 3.0876 5 0.062500 3.1098 6 0.075000 3.1321 7 0.087500 3.1546 8 0.10000 3.1771 9 0.11250 3.1998 10 0.12500 3.2226 11 0.13750 3.2456 12 0.15000 3.2686 13 0.16250 3.2918 14 0.17500 3.3151 15 0.18750 3.3385 16 0.20000 3.3620 17 0.21250 3.3857 18 0.22500 3.4094 19 0.23750 3.4333 20 0.25000 3.4573 21 0.26250 3.4815 22 0.27500 3.5057 23 0.28750 3.5301 24 0.30000 3.5546 25 0.31250 3.5792 26 0.32500 3.6039 27 0.33750 3.6287 28 0.35000 3.6537 29 0.36250 3.6787 30 0.37500 3.7039 31 0.38750 3.7292 32 0.40000 3.7546 33 0.41250 3.7802 34 0.42500 3.8058 35 0.43750 3.8316 36 0.45000 3.8575 37 0.46250 3.8835 38 0.47500 3.9096 39 0.48750 3.9358 40 0.50000 3.9622 41 0.51250 3.9887 42 0.52500 4.0152 43 0.53750 4.0419 44 0.55000 4.0687 45 0.56250 4.0957 46 0.57500 4.1227 47 0.58750 4.1498 48 0.60000 4.1771 49 0.61250 4.2045 50 0.62500 4.2320 51 0.63750 4.2596 52 0.64999 4.2873 53 0.66249 4.3151 54 0.67499 4.3431 55 0.68749 4.3711 56 0.69999 4.3993 57 0.71249 4.4276 58 0.72499 4.4560 59 0.73749 4.4845 60 0.74999 4.5131 61 0.76249 4.5418 62 0.77499 4.5707 63 0.78749 4.5996 64 0.79999 4.6287 65 0.81249 4.6579 66 0.82499 4.6872 67 0.83749 4.7166 68 0.84999 4.7461 69 0.86249 4.7757 70 0.87499 4.8055 71 0.88749 4.8353 72 0.89999 4.8653 73 0.91249 4.8953 74 0.92499 4.9255 75 0.93749 4.9558 76 0.94999 4.9862 77 0.96249 5.0167 78 0.97499 5.0473 79 0.98749 5.0781 80 0.99999 5.1089 81 1.0125 5.1399 82 1.0250 5.1709 83 1.0375 5.2021 84 1.0500 5.2334 85 1.0625 5.2648 86 1.0750 5.2963 87 1.0875 5.3279 88 1.1000 5.3596 89 1.1125 5.3914 90 1.1250 5.4233 91 1.1375 5.4554 92 1.1500 5.4875 93 1.1625 5.5198 94 1.1750 5.5521 95 1.1875 5.5846 96 1.2000 5.6172 97 1.2125 5.6499 98 1.2250 5.6827 99 1.2375 5.7156 100 1.2500 5.7486 101 1.2625 5.7817 102 1.2750 5.8150 103 1.2875 5.8483 104 1.3000 5.8818 105 1.3125 5.9153 106 1.3250 5.9490 107 1.3375 5.9827 108 1.3500 6.0166 109 1.3625 6.0506 110 1.3750 6.0847 111 1.3875 6.1189 112 1.4000 6.1532 113 1.4125 6.1876 114 1.4250 6.2221 115 1.4375 6.2567 116 1.4500 6.2915 117 1.4625 6.3263 118 1.4750 6.3612 119 1.4875 6.3963 120 1.5000 6.4314 121 1.5125 6.4667 122 1.5250 6.5020 123 1.5375 6.5375 124 1.5500 6.5731 125 1.5625 6.6088 126 1.5750 6.6445 127 1.5875 6.6804 128 1.6000 6.7164 129 1.6125 6.7525 130 1.6250 6.7887 131 1.6375 6.8251 132 1.6500 6.8615 133 1.6625 6.8980 134 1.6750 6.9346 135 1.6875 6.9713 136 1.7000 7.0082 137 1.7125 7.0451 138 1.7250 7.0822 139 1.7375 7.1193 140 1.7500 7.1566 141 1.7625 7.1939 142 1.7750 7.2314 143 1.7875 7.2689 144 1.8000 7.3066 145 1.8125 7.3444 146 1.8250 7.3823 147 1.8375 7.4202 148 1.8500 7.4583 149 1.8625 7.4965 150 1.8750 7.5348 151 1.8875 7.5732 152 1.9000 7.6117 153 1.9125 7.6503 154 1.9250 7.6890 155 1.9375 7.7278 156 1.9500 7.7667 157 1.9625 7.8057 158 1.9750 7.8449 159 1.9875 7.8841 160 2.0000 7.9234

Runge- Kutta Method

How to adjust the code:

  • The function is given in the line that contains f(x,y)=1-x*4*y.
  • The initial condition is given by x0=0 and y0=1.
  • The number of iterations is given by n=80.
  • The step size is given by the line step=0.025.
  • The precision variable should be an integer. The numerical values will return with at least that many digits of precision.

 

Change these values to your desired values and "Evaluate".

That should be all!

#9 (c) ︠90ca95d5-d2f5-4b5d-b0d7-3fe2865d23a8s︠ forget() var('x y a b k') f(x,y)=(x+y)^(1/2) x0=0 y0=3 n=20 step=.1 precision=5 k=list(0,..,3) a=numerical_approx(x0,digits=precision) b=numerical_approx(y0,digits=precision) print "Runge- Kutta Method" print "n", "(x(n),y(n))"," ","k_(n1), k_(n2),k_(n3), k_(n4)" for i in range(n+1): k[0]=f(a,b) k[1]=f(a+step/2,b+step/2*k[0]) k[2]=f(a+step/2,b+step/2*k[1]) k[3]=f(a+step,b+step*k[2]) print i,a,b," ",k[0],k[1],k[2],k[3] a=numerical_approx(a+step,digits=precision) b=numerical_approx(b+step*(k[0]+2*k[1]+2*k[2]+k[3])/6,digits=precision)
(x, y, a, b, k) Runge- Kutta Method n (x(n),y(n)) k_(n1), k_(n2),k_(n3), k_(n4) 0 0.00000 3.0000 1.7320 1.7710 1.7716 1.8103 1 0.10000 3.1771 1.8103 1.8487 1.8492 1.8873 2 0.20000 3.3620 1.8873 1.9252 1.9257 1.9633 3 0.30000 3.5546 1.9633 2.0007 2.0011 2.0383 4 0.40000 3.7546 2.0383 2.0752 2.0757 2.1124 5 0.50000 3.9622 2.1124 2.1489 2.1493 2.1857 6 0.60000 4.1771 2.1857 2.2218 2.2222 2.2582 7 0.70000 4.3993 2.2582 2.2939 2.2943 2.3300 8 0.80000 4.6287 2.3300 2.3654 2.3658 2.4011 9 0.90000 4.8653 2.4011 2.4363 2.4366 2.4716 10 1.0000 5.1089 2.4716 2.5065 2.5068 2.5416 11 1.1000 5.3596 2.5416 2.5762 2.5765 2.6110 12 1.2000 5.6172 2.6110 2.6453 2.6456 2.6799 13 1.3000 5.8817 2.6799 2.7140 2.7143 2.7483 14 1.4000 6.1532 2.7483 2.7822 2.7825 2.8163 15 1.5000 6.4314 2.8163 2.8500 2.8502 2.8838 16 1.6000 6.7164 2.8838 2.9173 2.9176 2.9510 17 1.7000 7.0081 2.9510 2.9842 2.9845 3.0177 18 1.8000 7.3066 3.0177 3.0508 3.0511 3.0841 19 1.9000 7.6117 3.0841 3.1170 3.1173 3.1501 20 2.0000 7.9234 3.1501 3.1829 3.1832 3.2159
#9 (d) ︠43507292-1cdf-4e0f-86ef-127801ebb115s︠ forget() var('x y a b k') f(x,y)=(x+y)^(1/2) x0=0 y0=3 n=40 step=.05 precision=5 k=list(0,..,3) a=numerical_approx(x0,digits=precision) b=numerical_approx(y0,digits=precision) print "Runge- Kutta Method" print "n", "(x(n),y(n))"," ","k_(n1), k_(n2),k_(n3), k_(n4)" for i in range(n+1): k[0]=f(a,b) k[1]=f(a+step/2,b+step/2*k[0]) k[2]=f(a+step/2,b+step/2*k[1]) k[3]=f(a+step,b+step*k[2]) print i,a,b," ",k[0],k[1],k[2],k[3] a=numerical_approx(a+step,digits=precision) b=numerical_approx(b+step*(k[0]+2*k[1]+2*k[2]+k[3])/6,digits=precision)
(x, y, a, b, k) Runge- Kutta Method n (x(n),y(n)) k_(n1), k_(n2),k_(n3), k_(n4) 0 0.00000 3.0000 1.7320 1.7517 1.7518 1.7713 1 0.050000 3.0876 1.7713 1.7908 1.7909 1.8103 2 0.10000 3.1771 1.8103 1.8296 1.8297 1.8489 3 0.15000 3.2686 1.8489 1.8681 1.8682 1.8873 4 0.20000 3.3620 1.8873 1.9064 1.9065 1.9254 5 0.25000 3.4573 1.9254 1.9443 1.9445 1.9633 6 0.30000 3.5546 1.9633 1.9821 1.9822 2.0009 7 0.35000 3.6537 2.0009 2.0196 2.0197 2.0383 8 0.40000 3.7546 2.0383 2.0568 2.0570 2.0755 9 0.45000 3.8575 2.0755 2.0939 2.0940 2.1124 10 0.50000 3.9622 2.1124 2.1307 2.1308 2.1491 11 0.55000 4.0687 2.1491 2.1674 2.1675 2.1857 12 0.60000 4.1771 2.1857 2.2038 2.2039 2.2220 13 0.65000 4.2873 2.2220 2.2401 2.2402 2.2582 14 0.70000 4.3993 2.2582 2.2761 2.2762 2.2941 15 0.75000 4.5131 2.2941 2.3120 2.3121 2.3300 16 0.80000 4.6287 2.3300 2.3478 2.3479 2.3656 17 0.85000 4.7461 2.3656 2.3833 2.3834 2.4011 18 0.90000 4.8653 2.4011 2.4187 2.4188 2.4364 19 0.95000 4.9862 2.4364 2.4540 2.4541 2.4716 20 1.0000 5.1089 2.4716 2.4891 2.4892 2.5067 21 1.0500 5.2334 2.5067 2.5241 2.5242 2.5416 22 1.1000 5.3596 2.5416 2.5589 2.5590 2.5763 23 1.1500 5.4875 2.5763 2.5936 2.5937 2.6110 24 1.2000 5.6172 2.6110 2.6282 2.6283 2.6455 25 1.2500 5.7486 2.6455 2.6627 2.6627 2.6799 26 1.3000 5.8817 2.6799 2.6970 2.6971 2.7141 27 1.3500 6.0166 2.7141 2.7312 2.7313 2.7483 28 1.4000 6.1532 2.7483 2.7653 2.7654 2.7823 29 1.4500 6.2914 2.7823 2.7993 2.7994 2.8163 30 1.5000 6.4314 2.8163 2.8332 2.8332 2.8501 31 1.5500 6.5731 2.8501 2.8669 2.8670 2.8838 32 1.6000 6.7164 2.8838 2.9006 2.9007 2.9174 33 1.6500 6.8614 2.9174 2.9342 2.9342 2.9510 34 1.7000 7.0081 2.9510 2.9676 2.9677 2.9844 35 1.7500 7.1565 2.9844 3.0010 3.0011 3.0177 36 1.8000 7.3066 3.0177 3.0343 3.0344 3.0509 37 1.8500 7.4583 3.0509 3.0675 3.0676 3.0841 38 1.9000 7.6117 3.0841 3.1006 3.1007 3.1172 39 1.9500 7.7667 3.1172 3.1336 3.1337 3.1501 40 2.0000 7.9234 3.1501 3.1666 3.1666 3.1830
#15 (a)
var('x','y') plot_slope_field(x^2+y^2, (x,-5,5), (y,-5,5))
(x, y)
︠3810ceb7-9865-4833-9773-4455b0e90acfs︠ #15 (b) forget() var('x y a b k') f(x,y)=x^2+y^2 x0=0 y0=1 n=80 step=.0125 precision=5 k=list(0,..,3) a=numerical_approx(x0,digits=precision) b=numerical_approx(y0,digits=precision) print "Runge- Kutta Method" print "n", "(x(n),y(n))"," ","k_(n1), k_(n2),k_(n3), k_(n4)" for i in range(n+1): k[0]=f(a,b) k[1]=f(a+step/2,b+step/2*k[0]) k[2]=f(a+step/2,b+step/2*k[1]) k[3]=f(a+step,b+step*k[2]) print i,a,b," ",k[0],k[1],k[2],k[3] a=numerical_approx(a+step,digits=precision) b=numerical_approx(b+step*(k[0]+2*k[1]+2*k[2]+k[3])/6,digits=precision)
(x, y, a, b, k) Runge- Kutta Method n (x(n),y(n)) k_(n1), k_(n2),k_(n3), k_(n4) 0 0.00000 1.0000 1 1.0126 1.0127 1.0256 1 0.012500 1.0127 1.0256 1.0389 1.0390 1.0526 2 0.025000 1.0256 1.0526 1.0665 1.0666 1.0809 3 0.037500 1.0390 1.0809 1.0955 1.0957 1.1106 4 0.050000 1.0527 1.1106 1.1259 1.1262 1.1419 5 0.062500 1.0667 1.1419 1.1580 1.1582 1.1747 6 0.075000 1.0812 1.1747 1.1916 1.1918 1.2091 7 0.087500 1.0961 1.2091 1.2269 1.2271 1.2453 8 0.10000 1.1115 1.2453 1.2640 1.2643 1.2834 9 0.11250 1.1273 1.2834 1.3030 1.3033 1.3233 10 0.12500 1.1436 1.3233 1.3439 1.3442 1.3653 11 0.13750 1.1604 1.3653 1.3870 1.3873 1.4095 12 0.15000 1.1777 1.4095 1.4322 1.4325 1.4559 13 0.16250 1.1956 1.4559 1.4798 1.4801 1.5047 14 0.17500 1.2141 1.5047 1.5298 1.5302 1.5560 15 0.18750 1.2332 1.5560 1.5825 1.5829 1.6101 16 0.20000 1.2530 1.6100 1.6379 1.6383 1.6669 17 0.21250 1.2735 1.6669 1.6963 1.6967 1.7269 18 0.22500 1.2947 1.7269 1.7578 1.7583 1.7901 19 0.23750 1.3167 1.7900 1.8226 1.8232 1.8567 20 0.25000 1.3395 1.8567 1.8911 1.8916 1.9270 21 0.26250 1.3631 1.9270 1.9633 1.9639 2.0012 22 0.27500 1.3877 2.0012 2.0396 2.0402 2.0797 23 0.28750 1.4132 2.0797 2.1202 2.1209 2.1627 24 0.30000 1.4397 2.1626 2.2055 2.2063 2.2505 25 0.31250 1.4672 2.2505 2.2959 2.2967 2.3435 26 0.32500 1.4960 2.3435 2.3916 2.3925 2.4422 27 0.33750 1.5259 2.4421 2.4932 2.4942 2.5469 28 0.35000 1.5570 2.5468 2.6011 2.6021 2.6581 29 0.36250 1.5896 2.6581 2.7157 2.7169 2.7764 30 0.37500 1.6235 2.7764 2.8378 2.8391 2.9024 31 0.38750 1.6590 2.9024 2.9678 2.9692 3.0368 32 0.40000 1.6961 3.0368 3.1066 3.1081 3.1802 33 0.41250 1.7350 3.1802 3.2548 3.2564 3.3336 34 0.42500 1.7757 3.3336 3.4133 3.4151 3.4978 35 0.43750 1.8183 3.4978 3.5832 3.5852 3.6738 36 0.45000 1.8631 3.6738 3.7656 3.7677 3.8629 37 0.46250 1.9102 3.8629 3.9616 3.9639 4.0664 38 0.47500 1.9598 4.0664 4.1726 4.1752 4.2857 39 0.48750 2.0120 4.2857 4.4003 4.4032 4.5225 40 0.50000 2.0670 4.5225 4.6464 4.6497 4.7788 41 0.51250 2.1251 4.7787 4.9130 4.9166 5.0567 42 0.52500 2.1866 5.0566 5.2024 5.2065 5.3587 43 0.53750 2.2516 5.3587 5.5174 5.5219 5.6879 44 0.55000 2.3206 5.6878 5.8610 5.8661 6.0474 45 0.56250 2.3939 6.0473 6.2368 6.2426 6.4413 46 0.57500 2.4720 6.4412 6.6490 6.6556 6.8739 47 0.58750 2.5551 6.8738 7.1026 7.1100 7.3508 48 0.60000 2.6440 7.3507 7.6032 7.6117 7.8780 49 0.61250 2.7391 7.8779 8.1577 8.1675 8.4631 50 0.62500 2.8412 8.4629 8.7741 8.7854 9.1148 51 0.63750 2.9510 9.1146 9.4620 9.4751 9.8437 52 0.64999 3.0694 9.8435 10.233 10.248 10.663 53 0.66249 3.1974 10.662 11.101 11.119 11.587 54 0.67499 3.3364 11.587 12.084 12.105 12.637 55 0.68749 3.4876 12.636 13.202 13.227 13.834 56 0.69999 3.6529 13.834 14.481 14.512 15.209 57 0.71249 3.8342 15.209 15.956 15.992 16.800 58 0.72499 4.0340 16.799 17.666 17.711 18.652 59 0.73749 4.2552 18.651 19.666 19.721 20.828 60 0.74999 4.5016 20.827 22.025 22.095 23.409 61 0.76249 4.7776 23.407 24.836 24.924 26.500 62 0.77499 5.0889 26.498 28.220 28.334 30.247 63 0.78749 5.4428 30.244 32.347 32.495 34.850 64 0.79999 5.8486 34.846 37.451 37.648 40.592 65 0.81249 6.3186 40.585 43.865 44.135 47.882 66 0.82499 6.8696 47.872 52.083 52.461 57.333 67 0.83749 7.5244 57.318 62.848 63.394 69.892 68 0.84999 8.3154 69.869 77.332 78.151 87.091 69 0.86249 9.2903 87.053 97.469 98.753 111.53 70 0.87499 10.522 111.47 126.63 128.76 147.95 71 0.88749 12.126 147.83 171.10 174.92 205.66 72 0.89999 14.304 205.42 243.81 251.35 305.20 73 0.91249 17.431 304.69 374.71 391.83 499.45 74 0.92499 22.301 498.17 646.75 694.81 960.99 75 0.93749 30.930 957.56 1363.6 1557.4 2540.9 76 0.94999 50.390 2540.0 4391.9 6059.9 15912. 77 0.96249 132.38 17525. 58523. 248150. 1.0461e7 78 0.97499 23240. 5.4009e8 1.1552e13 5.2127e21 4.2457e39 79 0.98749 8.8452e36 7.8238e73 2.3911e143 2.2333e282 7.7930e560 80 0.99999 1.6235e558 2.6359e1116 2.7140e2228 2.8774e4452 1.2936e8901