HSG-MCS-HS21_Julia/JuliaTutorial-master/Tutorial_21_FindingRoots.ipynb

389 lines
31 KiB
Plaintext
Raw Normal View History

2021-11-15 20:14:51 +00:00
{
"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
" 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
}