HSG-MCS-HS21_Julia/JuliaTutorial-master/Tutorial_21_FindingRoots.ipynb
2021-11-15 21:14:51 +01:00

389 lines
31 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Solving Non-linear Equations\n",
"\n",
"The [Roots](https://github.com/JuliaMath/Roots.jl) package provides methods for solving a non-linear equation (one variable, one function). \n",
"\n",
"For a system of non-linear equations, use [NLsolve](https://github.com/JuliaNLSolvers/NLsolve.jl)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Packages and Extra Functions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Printf, Roots, NLsolve\n",
"\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"\n",
"#pyplot(size=(600,400))\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Defining and Plotting the Function\n",
"\n",
"The next few cells define a fairly simple function and then plots it. \n",
"\n",
"If possible, plot your function before trying to find the roots. Maybe you see something strange, perhaps there are several roots? It also helps you set the initial guesses (or brackets) for root solving."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fn1 (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fn1(x,c) = 2*(x - 1.1)^2 - c"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
"<defs>\n",
" <clipPath id=\"clip840\">\n",
" <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip840)\" d=\"\n",
"M0 1280 L1920 1280 L1920 0 L0 0 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip841\">\n",
" <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip840)\" d=\"\n",
"M170.645 1106.38 L1872.76 1106.38 L1872.76 123.472 L170.645 123.472 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip842\">\n",
" <rect x=\"170\" y=\"123\" width=\"1703\" height=\"984\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 218.818,1106.38 218.818,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 620.259,1106.38 620.259,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1021.7,1106.38 1021.7,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1423.14,1106.38 1423.14,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1824.58,1106.38 1824.58,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,1106.38 1872.76,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 218.818,1106.38 218.818,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 620.259,1106.38 620.259,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1021.7,1106.38 1021.7,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1423.14,1106.38 1423.14,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1824.58,1106.38 1824.58,1094.58 \n",
" \"/>\n",
"<path clip-path=\"url(#clip840)\" d=\"M 0 0 M191.087 1146.38 L220.763 1146.38 L220.763 1150.32 L191.087 1150.32 L191.087 1146.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M226.642 1159.28 L234.281 1159.28 L234.281 1132.91 L225.971 1134.58 L225.971 1130.32 L234.235 1128.65 L238.911 1128.65 L238.911 1159.28 L246.549 1159.28 L246.549 1163.21 L226.642 1163.21 L226.642 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M620.259 1131.73 Q616.648 1131.73 614.82 1135.3 Q613.014 1138.84 613.014 1145.97 Q613.014 1153.07 614.82 1156.64 Q616.648 1160.18 620.259 1160.18 Q623.894 1160.18 625.699 1156.64 Q627.528 1153.07 627.528 1145.97 Q627.528 1138.84 625.699 1135.3 Q623.894 1131.73 620.259 1131.73 M620.259 1128.03 Q626.07 1128.03 629.125 1132.63 Q632.204 1137.22 632.204 1145.97 Q632.204 1154.69 629.125 1159.3 Q626.07 1163.88 620.259 1163.88 Q614.449 1163.88 611.371 1159.3 Q608.315 1154.69 608.315 1145.97 Q608.315 1137.22 611.371 1132.63 Q614.449 1128.03 620.259 1128.03 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1012.08 1159.28 L1019.72 1159.28 L1019.72 1132.91 L1011.41 1134.58 L1011.41 1130.32 L1019.68 1128.65 L1024.35 1128.65 L1024.35 1159.28 L1031.99 1159.28 L1031.99 1163.21 L1012.08 1163.21 L1012.08 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1417.79 1159.28 L1434.11 1159.28 L1434.11 1163.21 L1412.17 1163.21 L1412.17 1159.28 Q1414.83 1156.52 1419.41 1151.89 Q1424.02 1147.24 1425.2 1145.9 Q1427.45 1143.37 1428.33 1141.64 Q1429.23 1139.88 1429.23 1138.19 Q1429.23 1135.43 1427.29 1133.7 Q1425.36 1131.96 1422.26 1131.96 Q1420.06 1131.96 1417.61 1132.73 Q1415.18 1133.49 1412.4 1135.04 L1412.4 1130.32 Q1415.23 1129.18 1417.68 1128.61 Q1420.13 1128.03 1422.17 1128.03 Q1427.54 1128.03 1430.73 1130.71 Q1433.93 1133.4 1433.93 1137.89 Q1433.93 1140.02 1433.12 1141.94 Q1432.33 1143.84 1430.23 1146.43 Q1429.65 1147.1 1426.54 1150.32 Q1423.44 1153.51 1417.79 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1828.83 1144.58 Q1832.19 1145.3 1834.06 1147.56 Q1835.96 1149.83 1835.96 1153.17 Q1835.96 1158.28 1832.44 1161.08 Q1828.92 1163.88 1822.44 1163.88 Q1820.27 1163.88 1817.95 1163.44 Q1815.66 1163.03 1813.21 1162.17 L1813.21 1157.66 Q1815.15 1158.79 1817.46 1159.37 Q1819.78 1159.95 1822.3 1159.95 Q1826.7 1159.95 1828.99 1158.21 Q1831.31 1156.48 1831.31 1153.17 Q1831.31 1150.11 1829.15 1148.4 Q1827.03 1146.66 1823.21 1146.66 L1819.18 1146.66 L1819.18 1142.82 L1823.39 1142.82 Q1826.84 1142.82 1828.67 1141.45 Q1830.5 1140.06 1830.5 1137.47 Q1830.5 1134.81 1828.6 1133.4 Q1826.72 1131.96 1823.21 1131.96 Q1821.28 1131.96 1819.09 1132.38 Q1816.89 1132.8 1814.25 1133.67 L1814.25 1129.51 Q1816.91 1128.77 1819.22 1128.4 Q1821.56 1128.03 1823.62 1128.03 Q1828.95 1128.03 1832.05 1130.46 Q1835.15 1132.86 1835.15 1136.99 Q1835.15 1139.86 1833.51 1141.85 Q1831.86 1143.81 1828.83 1144.58 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1038.32 1208.4 L1025.42 1225.74 L1038.98 1244.04 L1032.08 1244.04 L1021.7 1230.04 L1011.32 1244.04 L1004.42 1244.04 L1018.26 1225.39 L1005.6 1208.4 L1012.5 1208.4 L1021.96 1221.1 L1031.41 1208.4 L1038.32 1208.4 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 170.645,1026 1872.76,1026 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 170.645,815.73 1872.76,815.73 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 170.645,605.464 1872.76,605.464 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 170.645,395.198 1872.76,395.198 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip842)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 170.645,184.933 1872.76,184.933 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,1106.38 170.645,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,1026 191.071,1026 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,815.73 191.071,815.73 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,605.464 191.071,605.464 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,395.198 191.071,395.198 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip840)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 170.645,184.933 191.071,184.933 \n",
" \"/>\n",
"<path clip-path=\"url(#clip840)\" d=\"M 0 0 M129.901 1011.79 Q126.29 1011.79 124.461 1015.36 Q122.655 1018.9 122.655 1026.03 Q122.655 1033.14 124.461 1036.7 Q126.29 1040.24 129.901 1040.24 Q133.535 1040.24 135.341 1036.7 Q137.169 1033.14 137.169 1026.03 Q137.169 1018.9 135.341 1015.36 Q133.535 1011.79 129.901 1011.79 M129.901 1008.09 Q135.711 1008.09 138.767 1012.7 Q141.845 1017.28 141.845 1026.03 Q141.845 1034.76 138.767 1039.36 Q135.711 1043.95 129.901 1043.95 Q124.091 1043.95 121.012 1039.36 Q117.956 1034.76 117.956 1026.03 Q117.956 1017.28 121.012 1012.7 Q124.091 1008.09 129.901 1008.09 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M125.526 829.074 L141.845 829.074 L141.845 833.01 L119.901 833.01 L119.901 829.074 Q122.563 826.32 127.146 821.69 Q131.753 817.038 132.933 815.695 Q135.179 813.172 136.058 811.436 Q136.961 809.676 136.961 807.987 Q136.961 805.232 135.017 803.496 Q133.095 801.76 129.993 801.76 Q127.794 801.76 125.341 802.524 Q122.91 803.288 120.132 804.839 L120.132 800.116 Q122.956 798.982 125.41 798.403 Q127.864 797.825 129.901 797.825 Q135.271 797.825 138.466 800.51 Q141.66 803.195 141.66 807.686 Q141.66 809.815 140.85 811.737 Q140.063 813.635 137.956 816.227 Q137.378 816.899 134.276 820.116 Q131.174 823.311 125.526 829.074 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M132.262 592.258 L120.456 610.707 L132.262 610.707 L132.262 592.258 M131.035 588.184 L136.915 588.184 L136.915 610.707 L141.845 610.707 L141.845 614.596 L136.915 614.596 L136.915 622.744 L132.262 622.744 L132.262 614.596 L116.66 614.596 L116.66 610.082 L131.035 588.184 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M130.317 393.335 Q127.169 393.335 125.318 395.488 Q123.489 397.641 123.489 401.39 Q123.489 405.117 125.318 407.293 Q127.169 409.446 130.317 409.446 Q133.466 409.446 135.294 407.293 Q137.146 405.117 137.146 401.39 Q137.146 397.641 135.294 395.488 Q133.466 393.335 130.317 393.335 M139.6 378.682 L139.6 382.942 Q137.841 382.108 136.035 381.668 Q134.253 381.229 132.493 381.229 Q127.864 381.229 125.41 384.354 Q122.98 387.479 122.632 393.798 Q123.998 391.784 126.058 390.719 Q128.118 389.631 130.595 389.631 Q135.804 389.631 138.813 392.803 Q141.845 395.951 141.845 401.39 Q141.845 406.715 138.697 409.932 Q135.549 413.15 130.317 413.15 Q124.322 413.15 121.151 408.566 Q117.98 403.96 117.98 395.233 Q117.98 387.039 121.868 382.178 Q125.757 377.293 132.308 377.293 Q134.067 377.293 135.85 377.641 Q137.655 377.988 139.6 378.682 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M129.993 185.801 Q126.66 185.801 124.739 187.583 Q122.841 189.366 122.841 192.491 Q122.841 195.616 124.739 197.398 Q126.66 199.18 129.993 199.18 Q133.327 199.18 135.248 197.398 Q137.169 195.592 137.169 192.491 Q137.169 189.366 135.248 187.583 Q133.35 185.801 129.993 185.801 M125.318 183.81 Q122.308 183.069 120.618 181.009 Q118.952 178.949 118.952 175.986 Q118.952 171.843 121.892 169.435 Q124.855 167.028 129.993 167.028 Q135.155 167.028 138.095 169.435 Q141.035 171.843 141.035 175.986 Q141.035 178.949 139.345 181.009 Q137.679 183.069 134.692 183.81 Q138.072 184.597 139.947 186.889 Q141.845 189.18 141.845 192.491 Q141.845 197.514 138.767 200.199 Q135.711 202.884 129.993 202.884 Q124.276 202.884 121.197 200.199 Q118.142 197.514 118.142 192.491 Q118.142 189.18 120.04 186.889 Q121.938 184.597 125.318 183.81 M123.605 176.426 Q123.605 179.111 125.271 180.616 Q126.961 182.12 129.993 182.12 Q133.003 182.12 134.692 180.616 Q136.405 179.111 136.405 176.426 Q136.405 173.741 134.692 172.236 Q133.003 170.731 129.993 170.731 Q126.961 170.731 125.271 172.236 Q123.605 173.741 123.605 176.426 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M76.9143 613.239 Q83.28 615.722 85.2216 618.077 Q87.1631 620.432 87.1631 624.379 L87.1631 629.058 L82.2615 629.058 L82.2615 625.62 Q82.2615 623.201 81.1157 621.865 Q79.9699 620.528 75.7048 618.905 L73.0312 617.854 L37.9562 632.273 L37.9562 626.066 L65.838 614.926 L37.9562 603.786 L37.9562 597.579 L76.9143 613.239 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M624.712 14.324 L624.712 27.2059 L640.065 27.2059 L640.065 32.9987 L624.712 32.9987 L624.712 57.6282 Q624.712 63.1779 626.211 64.7578 Q627.75 66.3376 632.409 66.3376 L640.065 66.3376 L640.065 72.576 L632.409 72.576 Q623.78 72.576 620.499 69.3758 Q617.218 66.1351 617.218 57.6282 L617.218 32.9987 L611.749 32.9987 L611.749 27.2059 L617.218 27.2059 L617.218 14.324 L624.712 14.324 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M685.597 45.1919 L685.597 72.576 L678.143 72.576 L678.143 45.4349 Q678.143 38.994 675.632 35.7938 Q673.12 32.5936 668.097 32.5936 Q662.061 32.5936 658.578 36.4419 Q655.094 40.2903 655.094 46.9338 L655.094 72.576 L647.6 72.576 L647.6 9.54393 L655.094 9.54393 L655.094 34.2544 Q657.767 30.163 661.373 28.1376 Q665.018 26.1121 669.758 26.1121 Q677.576 26.1121 681.587 30.9732 Q685.597 35.7938 685.597 45.1919 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M732.223 48.0275 L732.223 51.6733 L697.952 51.6733 Q698.438 59.3701 702.57 63.421 Q706.743 67.4314 714.156 67.4314 Q718.45 67.4314 722.46 66.3781 Q726.511 65.3249 730.481 63.2184 L730.481 70.267 Q726.471 71.9684 722.258 72.8596 Q718.045 73.7508 713.71 73.7508 Q702.854 73.7508 696.494 67.4314 Q690.175 61.1119 690.175 50.3365 Q690.175 39.1965 696.17 32.6746 Q702.206 26.1121 712.414 26.1121 Q721.569 26.1121 726.876 32.0264 Q732.223 37.9003 732.223 48.0275 M724.769 45.84 Q724.688 39.7232 721.326 36.0774 Q718.004 32.4315 712.495 32.4315 Q706.257 32.4315 702.489 35.9558 Q698.762 39.4801 698.195 45.8805 L724.769 45.84 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M789.381 9.54393 L789.381 15.7418 L782.252 15.7418 Q778.241 15.7418 776.661 17.3622 Q775.122 18.9825 775.122 23.1955 L775.122 27.2059 L787.396 27.2059 L787.396 32.9987 L775.122 32.9987 L775.122 72.576 L767.628 72.576 L767.628 32.9987 L760.498 32.9987 L760.498 27.2059 L767.628 27.2059 L767.628 24.0462 Q767.628 16.471 771.152 13.0277 Q774.676 9.54393 782.333 9.54393 L789.381 9.54393 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M834.913 45.1919 L834.913 72.576 L827.46 72.576 L827.46 45.4349 Q827.46 38.994 824.948 35.7938 Q822.437 32.5936 817.413 32.5936 Q811.378 32.5936 807.894 36.4419 Q804.41 40.2903 804.41 46.9338 L804.41 72.576 L796.916 72.576 L796.916 27.2059 L804.41 27.2059 L804.41 34.2544 Q807.084 30.163 810.689 28.1376 Q814.335 26.1121 819.074 26.1121 Q826.893 26.1121 830.903 30.9732 Q834.913 35.7938 834.913 45.1919 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M845.203 65.6895 L858.571 65.6895 L858.571 19.5497 L844.028 22.4663 L844.028 15.0127 L858.49 12.096 L866.672 12.096 L866.672 65.6895 L880.04 65.6895 L880.04 72.576 L845.203 72.576 L845.203 65.6895 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M905.764 9.62495 Q900.335 18.942 897.702 28.0566 Q895.069 37.1711 895.069 46.5287 Q895.069 55.8863 897.702 65.0818 Q900.376 74.2369 905.764 83.5134 L899.282 83.5134 Q893.206 73.9938 890.168 64.7983 Q887.17 55.6027 887.17 46.5287 Q887.17 37.4952 890.168 28.3401 Q893.165 19.1851 899.282 9.62495 L905.764 9.62495 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M951.296 27.2059 L934.89 49.2833 L952.147 72.576 L943.356 72.576 L930.15 54.752 L916.944 72.576 L908.154 72.576 L925.775 48.8377 L909.653 27.2059 L918.443 27.2059 L930.474 43.369 L942.505 27.2059 L951.296 27.2059 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M961.869 62.2867 L970.416 62.2867 L970.416 69.2543 L963.773 82.2172 L958.547 82.2172 L961.869 69.2543 L961.869 62.2867 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M996.787 17.4837 Q990.468 17.4837 987.268 23.7221 Q984.108 29.92 984.108 42.3968 Q984.108 54.833 987.268 61.0714 Q990.468 67.2693 996.787 67.2693 Q1003.15 67.2693 1006.31 61.0714 Q1009.51 54.833 1009.51 42.3968 Q1009.51 29.92 1006.31 23.7221 Q1003.15 17.4837 996.787 17.4837 M996.787 11.0023 Q1006.96 11.0023 1012.3 19.0636 Q1017.69 27.0843 1017.69 42.3968 Q1017.69 57.6687 1012.3 65.73 Q1006.96 73.7508 996.787 73.7508 Q986.62 73.7508 981.232 65.73 Q975.885 57.6687 975.885 42.3968 Q975.885 27.0843 981.232 19.0636 Q986.62 11.0023 996.787 11.0023 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1026.56 62.2867 L1035.11 62.2867 L1035.11 72.576 L1026.56 72.576 L1026.56 62.2867 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1044.06 12.096 L1076.19 12.096 L1076.19 18.9825 L1051.56 18.9825 L1051.56 33.8088 Q1053.34 33.2012 1055.12 32.9176 Q1056.9 32.5936 1058.69 32.5936 Q1068.81 32.5936 1074.73 38.1433 Q1080.64 43.6931 1080.64 53.1722 Q1080.64 62.9348 1074.56 68.3631 Q1068.49 73.7508 1057.43 73.7508 Q1053.62 73.7508 1049.65 73.1026 Q1045.72 72.4545 1041.51 71.1582 L1041.51 62.9348 Q1045.16 64.9198 1049.04 65.892 Q1052.93 66.8642 1057.27 66.8642 Q1064.28 66.8642 1068.37 63.1779 Q1072.46 59.4916 1072.46 53.1722 Q1072.46 46.8528 1068.37 43.1664 Q1064.28 39.4801 1057.27 39.4801 Q1053.99 39.4801 1050.71 40.2093 Q1047.46 40.9384 1044.06 42.4778 L1044.06 12.096 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1087.28 9.62495 L1093.77 9.62495 Q1099.84 19.1851 1102.84 28.3401 Q1105.88 37.4952 1105.88 46.5287 Q1105.88 55.6027 1102.84 64.7983 Q1099.84 73.9938 1093.77 83.5134 L1087.28 83.5134 Q1092.67 74.2369 1095.31 65.0818 Q1097.98 55.8863 1097.98 46.5287 Q1097.98 37.1711 1095.31 28.0566 Q1092.67 18.942 1087.28 9.62495 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1163.04 9.54393 L1163.04 15.7418 L1155.91 15.7418 Q1151.9 15.7418 1150.32 17.3622 Q1148.78 18.9825 1148.78 23.1955 L1148.78 27.2059 L1161.05 27.2059 L1161.05 32.9987 L1148.78 32.9987 L1148.78 72.576 L1141.28 72.576 L1141.28 32.9987 L1134.15 32.9987 L1134.15 27.2059 L1141.28 27.2059 L1141.28 24.0462 Q1141.28 16.471 1144.81 13.0277 Q1148.33 9.54393 1155.99 9.54393 L1163.04 9.54393 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1170.09 54.671 L1170.09 27.2059 L1177.54 27.2059 L1177.54 54.3874 Q1177.54 60.8284 1180.05 64.0691 Q1182.56 67.2693 1187.59 67.2693 Q1193.62 67.2693 1197.1 63.421 Q1200.63 59.5726 1200.63 52.9291 L1200.63 27.2059 L1208.08 27.2059 L1208.08 72.576 L1200.63 72.576 L1200.63 65.6084 Q1197.91 69.7404 1194.31 71.7658 Q1190.74 73.7508 1186.01 73.7508 Q1178.19 73.7508 1174.14 68.8897 Q1170.09 64.0286 1170.09 54.671 M1188.84 26.1121 L1188.84 26.1121 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1253.61 45.1919 L1253.61 72.576 L1246.16 72.576 L1246.16 45.4349 Q1246.16 38.994 1243.65 35.7938 Q1241.14 32.5936 1236.11 32.5936 Q1230.08 32.5936 1226.6 36.4419 Q1223.11 40.2903 1223.11 46.9338 L1223.11 72.576 L1215.62 72.576 L1215.62 27.2059 L1223.11 27.2059 L1223.11 34.2544 Q1225.79 30.163 1229.39 28.1376 Q1233.04 26.1121 1237.78 26.1121 Q1245.59 26.1121 1249.6 30.9732 Q1253.61 35.7938 1253.61 45.1919 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1294.08 28.9478 L1294.08 35.9153 Q1290.92 34.1734 1287.72 33.3227 Q1284.56 32.4315 1281.32 32.4315 Q1274.07 32.4315 1270.06 37.0496 Q1266.05 41.6271 1266.05 49.9314 Q1266.05 58.2358 1270.06 62.8538 Q1274.07 67.4314 1281.32 67.4314 Q1284.56 67.4314 1287.72 66.5807 Q1290.92 65.6895 1294.08 63.9476 L1294.08 70.8341 Q1290.96 72.2924 1287.6 73.0216 Q1284.28 73.7508 1280.51 73.7508 Q1270.26 73.7508 1264.23 67.3098 Q1258.19 60.8689 1258.19 49.9314 Q1258.19 38.832 1264.27 32.472 Q1270.39 26.1121 1281 26.1121 Q1284.44 26.1121 1287.72 26.8413 Q1291 27.5299 1294.08 28.9478 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1309.27 14.324 L1309.27 27.2059 L1324.63 27.2059 L1324.63 32.9987 L1309.27 32.9987 L1309.27 57.6282 Q1309.27 63.1779 1310.77 64.7578 Q1312.31 66.3376 1316.97 66.3376 L1324.63 66.3376 L1324.63 72.576 L1316.97 72.576 Q1308.34 72.576 1305.06 69.3758 Q1301.78 66.1351 1301.78 57.6282 L1301.78 32.9987 L1296.31 32.9987 L1296.31 27.2059 L1301.78 27.2059 L1301.78 14.324 L1309.27 14.324 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1332.45 27.2059 L1339.9 27.2059 L1339.9 72.576 L1332.45 72.576 L1332.45 27.2059 M1332.45 9.54393 L1339.9 9.54393 L1339.9 18.9825 L1332.45 18.9825 L1332.45 9.54393 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1365.3 32.4315 Q1359.3 32.4315 1355.82 37.1306 Q1352.34 41.7891 1352.34 49.9314 Q1352.34 58.0738 1355.78 62.7728 Q1359.26 67.4314 1365.3 67.4314 Q1371.25 67.4314 1374.74 62.7323 Q1378.22 58.0333 1378.22 49.9314 Q1378.22 41.8701 1374.74 37.1711 Q1371.25 32.4315 1365.3 32.4315 M1365.3 26.1121 Q1375.02 26.1121 1380.57 32.4315 Q1386.12 38.7509 1386.12 49.9314 Q1386.12 61.0714 1380.57 67.4314 Q1375.02 73.7508 1365.3 73.7508 Q1355.54 73.7508 1349.99 67.4314 Q1344.48 61.0714 1344.48 49.9314 Q1344.48 38.7509 1349.99 32.4315 Q1355.54 26.1121 1365.3 26.1121 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip840)\" d=\"M 0 0 M1431.65 45.1919 L1431.65 72.576 L1424.2 72.576 L1424.2 45.4349 Q1424.2 38.994 1421.69 35.7938 Q1419.18 32.5936 1414.15 32.5936 Q1408.12 32.5936 1404.63 36.4419 Q1401.15 40.2903 1401.15 46.9338 L1401.15 72.576 L1393.65 72.576 L1393.65 27.2059 L1401.15 27.2059 L1401.15 34.2544 Q1403.82 30.163 1407.43 28.1376 Q1411.07 26.1121 1415.81 26.1121 Q1423.63 26.1121 1427.64 30.9732 Q1431.65 35.7938 1431.65 45.1919 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><polyline clip-path=\"url(#clip842)\" style=\"stroke:#ff0000; stroke-width:8; stroke-opacity:1; fill:none\" points=\"\n",
" 218.818,151.29 258.962,237.499 299.106,319.503 339.251,397.301 379.395,470.894 419.539,540.282 459.683,605.464 499.827,666.441 539.971,723.213 580.115,775.779 \n",
" 620.259,824.14 660.403,868.296 700.548,908.247 740.692,943.992 780.836,975.532 820.98,1002.87 861.124,1026 901.268,1044.92 941.412,1059.64 981.556,1070.15 \n",
" 1021.7,1076.46 1061.84,1078.56 1101.99,1076.46 1142.13,1070.15 1182.28,1059.64 1222.42,1044.92 1262.57,1026 1302.71,1002.87 1342.85,975.532 1383,943.992 \n",
" 1423.14,908.247 1463.29,868.296 1503.43,824.14 1543.57,775.779 1583.72,723.213 1623.86,666.441 1664.01,605.464 1704.15,540.282 1744.29,470.894 1784.44,397.301 \n",
" 1824.58,319.503 \n",
" \"/>\n",
"</svg>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = -1:0.1:3\n",
"\n",
"p1 = plot( x,fn1.(x,0.5),\n",
" linecolor = :red,\n",
" linewidth = 2,\n",
" legend = nothing,\n",
" title = \"the fn1(x,0.5) function\",\n",
" xlabel = \"x\",\n",
" ylabel = \"y\" )\n",
"display(p1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There seems to be two roots: around 0.6 and 1.6."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solving a Non-Linear Equation\n",
"\n",
"The Roots package wants a function with only one input. An easy way to turn ```fn1(a,0.5)``` into that form is by defining an anonymous function:\n",
"```\n",
"x->fn1(x,0.5)\n",
"```\n",
"\n",
"Then, running \n",
"```\n",
"find_zero(x->fn1(x,0.5),(x₀,x₁))\n",
"```\n",
"searches for a root in the `[x₀,x₁]` interval. Alternatively, you can also do \n",
"```\n",
"find_zero(x->fn1(x,0.5),x₂)\n",
"``` \n",
"where `x₂` is a single starting guess.\n",
"\n",
"Instead, running\n",
"```\n",
"find_zeros(x->fn1(x,0.5),x₀,x₁)\n",
"```\n",
"searches for all roots between x₀ and x₁. (Notice the *s* in `find_zeros`.)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"at which x is fn1(x,0.5) = 0? 0.600\n",
"at which x is fn1(x,0.5) = 0? 1.600\n",
"\n",
"\u001b[34m\u001b[1myes, there are several roots. Just look at it (in the plot)\u001b[22m\u001b[39m\n"
]
}
],
"source": [
"xRoot1 = find_zero(x->fn1(x,0.5),(-1,1)) #searches for roots in [-1,1]\n",
"printlnPs(\"at which x is fn1(x,0.5) = 0? \",xRoot1)\n",
"\n",
"xRoot2 = find_zero(x->fn1(x,0.5),2) #searches for roots around 2\n",
"printlnPs(\"at which x is fn1(x,0.5) = 0? \",xRoot2)\n",
"\n",
"printblue(\"\\nyes, there are several roots. Just look at it (in the plot)\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"at which x is fn1(x,0.5) = 0? 0.600 1.600\n"
]
}
],
"source": [
"xRootsAll = find_zeros(x->fn1(x,0.5),-1,3) #find_zeros (notice the \"s\")\n",
"\n",
"printlnPs(\"at which x is fn1(x,0.5) = 0? \",xRootsAll)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solving a System of Non-Linear Equations\n",
"\n",
"The NLsolve package has many options. The cells below illustrate a very simple case (2 non-linear equations with 2 unknowns, no information about the derivatives).\n",
"\n",
"The two equations are\n",
"\n",
"$ \n",
"y-x^2-1=0\n",
"$\n",
"\n",
"$\n",
"y-x-1=0\n",
"$\n",
"\n",
"and the roots are at $(x,y)=(0,1)$ and also at $(1,2)$."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fn2 (generic function with 1 method)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function fn2(p) #p is a vector with 2 elements, the output too\n",
" (x,y) = (p[1],p[2])\n",
" z = [y-x^2-1;y-x-1] #equal to [0,0]\n",
" return z\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There is a solution at -0.000 1.000\n",
"There is a another solution at 1.000 2.000\n"
]
}
],
"source": [
"Sol2a = nlsolve(fn2,[0.0,0.5])\n",
"printlnPs(\"There is a solution at \",Sol2a.zero)\n",
"\n",
"Sol2b = nlsolve(fn2,[1.0,0.0]) #try again, using another starting guess\n",
"printlnPs(\"There is a another solution at \",Sol2b.zero)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.6.0",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 1
}