11-方程式求根
一、Symbolic approach(通过符号方法求根)
(一)Problem Statement
1、Suppose you have a mathematical function
2、How do you solve problem using MATLAB?
(1)Analytical Solutions(解析解)
(2)Graphical Illustration(图解法)
(3)Numerical Solutions(数值解)
(二)Symbolic Root Finding Approach(符号寻根法)
1、Performing mathematics on symbols,NOT numbers (对符号而不是数字进行数学运算)
2、The symbols math are performed using “symbolic variables”(符号数学是使用“符号变量”来执行的)
3、Using sym or syms to create symbolic variables
(1)示例代码:
syms
输出结果:
![e5a9d9bb9287903ae94d31e36b252708.png](https://i-blog.csdnimg.cn/blog_migrate/157c49ccd55db6ddc4b198beba1aa97d.png)
(2)示例代码:
x
输出结果:
![1564f6388e4055ec2f9b02a5d4ea62db.png](https://i-blog.csdnimg.cn/blog_migrate/decfd2c783009acc2ca1a8eeddfe43e0.png)
(三)Symbolic Root Finding:solve()
1、Function solve finds roots for equations(函数解法求方程根)
示例代码:
syms
输出结果:
![380fc90309862f52dd5ac2b488b316d6.png](https://i-blog.csdnimg.cn/blog_migrate/a2cab2b5a1b15aa4d3694d778360a104.png)
2、小练习
(1)
答案代码:
syms
输出结果:
![474b934038cebb31c31ba3c38bf8ded6.png](https://i-blog.csdnimg.cn/blog_migrate/25d81f5e6aa8920dad4ec3e1425fc48a.png)
(2)
答案代码:
syms
输出结果:
![1452e159d4508b1631e4e003e09b64e5.png](https://i-blog.csdnimg.cn/blog_migrate/4d4e675f81cd1f7c1fe19eabed201609.png)
(四)Solving Multiple Equations
1、Solve this equation using symbolic approach:
示例代码:
syms
输出结果:
![8d824320728d655bd2087fb4f8ed7afb.png](https://i-blog.csdnimg.cn/blog_migrate/d4e2b65c70f97dede624afbbad1cea51.png)
(五)Solving Equations Expressed in Symbols
1、What if we are given a function expressed in symbols
示例代码:
syms
输出结果:
![1d442de24d4989daf1e23ef04e5ee16e.png](https://i-blog.csdnimg.cn/blog_migrate/e68d52f485966026ccaac383afb87af4.png)
2、What if one wants to express b in terms of a and x(若想通过a和x来表达x)
示例代码:
syms
输出结果:
![fcc36d90a877734497377956398c1b1a.png](https://i-blog.csdnimg.cn/blog_migrate/13e7c4540196840c5d40ab5ac1afe0b9.png)
(六)Excise
1、Solve this equation for x using symbolic approach(用符号方法解x的这个方)
答案代码:
syms
输出结果:
![4750be9dfde6e2e0f95e429f875f0b67.png](https://i-blog.csdnimg.cn/blog_migrate/e7da7176218675562ec182ff83d52414.png)
2、Find the matrix inverse using symbolic approach(用符号法求矩阵逆)
![682531fd8be3c8adf5c46c7a27afca54.png](https://i-blog.csdnimg.cn/blog_migrate/4b94eb89d5bf05a6d1140c9975a04537.png)
答案代码:
syms
输出结果:
![20f193a21f8c9b856443719ee1dcc5cb.png](https://i-blog.csdnimg.cn/blog_migrate/60a930a095fd2d08d21282d109580db8.png)
(七)Symbolic Differentiation:diff()
1、Calculate the derivative of a symbolic function:
示例代码:
syms
输出结果:
![e01bc07d3b4b6d55403ba2be8aae54ab.png](https://i-blog.csdnimg.cn/blog_migrate/2311e4e3ae36e7e6ec14de178cfe6df2.png)
2、练习
(1)
(2)
答案代码:
syms
输出结果:
![bd103a1490ad78d004134dfc0569fd70.png](https://i-blog.csdnimg.cn/blog_migrate/6fe50fb10034cde9822b0ff51bbe8764.png)
(八)Symbolic Integration:int()
1、Calculate the integral of a symbolic function:
示例代码:
syms
输出结果:
![160bb112b498fc88dffc3f98f0e1b855.png](https://i-blog.csdnimg.cn/blog_migrate/ba9019f6a7b49e8cd5292875f5b6206e.png)
2、Excise:
答案代码:
syms
输出结果:
![f39fc27a437ca4bcd4a87a2ae3cee311.png](https://i-blog.csdnimg.cn/blog_migrate/c290858732a33bd4bc71144bfe6b1e8c.png)
(九)Symbolic vs. Numeric
![6873ea71f96eb3d86c58221f239e10dc.png](https://i-blog.csdnimg.cn/blog_migrate/bb185b150a08069b7351fcd283dababd.png)
二、Numeric root solvers(利用数值求根)
(一)Review of Function Handles(@)(回顾函数句柄)
1、A handle is a pointer to a function(句柄是指向函数的指针)
2、Can be used to pass functions to other functions(可用于将函数传递给其他函数)
3、For example,the input of the following function is another function
示例代码:
function
输出结果:
xy_plot
![cfa916a74a92790045b7eda333e050ea.png](https://i-blog.csdnimg.cn/blog_migrate/5ba96c9f89a95606f71112882db15a5f.png)
xy_plot
![e2f6a1ba3b3b664f3591a15d3e2b9ff4.png](https://i-blog.csdnimg.cn/blog_migrate/1fda1bfe5c7643a05855085d29a5820d.png)
(二)fsolve()
1、A numeric root solver
2、For example,solve this equation:
![8a2ce76a3fc931c65c29b5de3137c57f.png](https://i-blog.csdnimg.cn/blog_migrate/40a8c93c5a299cb04dfe643d2c44dd76.png)
示例代码:
f2
输出结果:
![8f0fb0110edc8891e12803fae89c10d0.png](https://i-blog.csdnimg.cn/blog_migrate/dc4e0bf10c0fd7b3174c36ac8c940dbd.png)
注意:
(1)f2 =@(x)(1.2*x +0.3+ x *sin(x));其中的@(x)为匿名函数,第一个括号里面是自变量,第二个括号里面是表达式,@是函数指针。
@在匿名函数中表示函数句柄。例如ln(x),在matlab中是没有定义的,正确表示是log(x);
但如果要直观表示自然对数,意义用以下语句表示:
ln=@(x) log(x);
(2)fsolve:求解非线性方程组
matlab中利用 solve,fzero,fsolve解方程问题www.cnblogs.com3、Excise
(1)Find the root for this equation
![33e7f75b76162a83bb5b61a1475fb473.png](https://i-blog.csdnimg.cn/blog_migrate/10bf27c396ffe82a7971ce4d0ce94492.png)
答案代码:
f
输出结果:
![0f1922e6634c34b67e210460440c76c2.png](https://i-blog.csdnimg.cn/blog_migrate/04720b2a432b2285bc3ce5a68bfd57b7.png)
(三)fzero()
1、Anther numeric root solver(另一种数值根解法)
2、Find the zero if and only if the function crosses the x-axis
![7d307ac28cd290a47c08c1c727a9504c.png](https://i-blog.csdnimg.cn/blog_migrate/5167c39bb90f02687fa065fba693c749.png)
![1e1834a715b27a7c18af4419d8dbe021.png](https://i-blog.csdnimg.cn/blog_migrate/3552f916ff6b102012d8a62f9d34abba.png)
3、示例代码:
f
输出结果:
![2b3d086230c80b42a2c926974065969f.png](https://i-blog.csdnimg.cn/blog_migrate/5d97a0fd117ecff15bd80672c805ff60.jpeg)
4、Option:(似乎失败了,没有出现准确的值,和上面的值相同)
示例代码:
f
输出结果:
![d1adce75bca9b30b6c087bbfd0cdf4dd.png](https://i-blog.csdnimg.cn/blog_migrate/fcb176b5e056ddd9a9237406d3266c9c.jpeg)
(四)Finding Roots of Polynomials:roots()
1、Find the roots of this polynomial:
示例代码:
roots
输出结果:
![aa93e6d5186f81e966c8d7f807968464.png](https://i-blog.csdnimg.cn/blog_migrate/406be34bdced17242addaba8a84be0d9.png)
2、roots only works for polynomials
3、Excise:Find the roots of the polynomial:
答案代码:
roots
输出结果:
![9a948bb1e4fe74e6c9aec7432997c0a5.png](https://i-blog.csdnimg.cn/blog_migrate/7eac2498a79f2e1f2254963b232cc5ee.png)
(五)Hwo Do These Solvers Find the Roots?
1、Now we are going to introduce more details of some numeric methods
![45ae2f945a2baf486e29ab98ab204a48.png](https://i-blog.csdnimg.cn/blog_migrate/5c5dbb30315482c8bf89cb41ab15dcb4.png)
(六)Numeric Root Finding Methods(数值求根方法)
1、Two major types:(两种)
(1)Bracketing methods(e.g.,bisection method)(包夹,如二分法)
Start with an interval that contains the root(从包含根目录的间隔开始)
(2)Open method(e.g.,Newton-Raphson method)(无包夹,如牛顿法)
Start with one or more initial guess points(从一个或多个初始猜测点开始)
2、Roots are found iteratively until some criteria are satisfied(迭代找到根,直到满足某些条件)
(1)Accuracy(更精确)
(2)Number of iteration(迭代次数)
(七)Bisection Method(Bracketing)(即二分法),很重要,值得好好理解
![445033655f7a840c9dea6c6e5e1ba4f7.png](https://i-blog.csdnimg.cn/blog_migrate/67f0f49f1ff9ae4c0fe4c79aaeec434d.jpeg)
1、Assumptions:(假设)
2、Algorithm(算法)
(八)Bisection Algorithm Flowchart(等分算法流程图)
![67cd268960a96e29123da494aab131cb.png](https://i-blog.csdnimg.cn/blog_migrate/9e8ccefa866e1ed0d9c72a8b178b0f2b.jpeg)
(九)Newton-Raphson Method(Open)(牛顿拉普森法)
![fc6b099e8b21daa4ace20070702a5f72.png](https://i-blog.csdnimg.cn/blog_migrate/c95596a438d32c0663492424e5b45924.jpeg)
1、Assumptions:(假设)
2、Algorithm(算法)
(十)Newton-Raphson Algorithm Flowchart(牛顿法流程图)
![e95b167b2e50ba6f324d2f1f49dd4d21.png](https://i-blog.csdnimg.cn/blog_migrate/b00da78fa3caa6e07fcd682625f826c0.png)
(十一)Bisection vs. Newton-Raphson
![8d9b24ab1147fa53b481140b5917d8db.png](https://i-blog.csdnimg.cn/blog_migrate/0df3c977aa55fba736ce501c75204d4d.png)
三、Recursive functions(利用递归求根)
1、Function that call themselves
2、Example,factorial of an integer n
n! = 1X2X3X...Xn
3、A factorial can be defined in terms of anther factorial:
![fde2e001261c3784b587502aaade8939.png](https://i-blog.csdnimg.cn/blog_migrate/0f5fa7de350853a821008a364a5cd782.png)
(一)Factorial Recursive Function(阶乘递归函数)
1、The function includes a recursive case and a base case(该函数包括递归情况和基情况)
2、The function stop when it reaches the base case(函数到达基本情况时停止)
![e223ebfb4898275381ad5c45b1523ccc.png](https://i-blog.csdnimg.cn/blog_migrate/81c2a57e8cb5474530ca055aeb1b0f94.png)
第十一节结束