当前位置:网站首页>使用Matlab实现:弦截法、二分法、CG法,求零点、解方程
使用Matlab实现:弦截法、二分法、CG法,求零点、解方程
2022-07-02 06:25:00 【霏霏小雨】
弦截法
例题
求 x e x − 1 = 0 xe^x - 1 = 0 xex−1=0 的根,取初值 x 0 = 0.5 , x 1 = 0.6 。 x_0 = 0.5, x_1 = 0.6。 x0=0.5,x1=0.6。
实现
其数学迭代公式为:
x k + 1 = x k − f ( x k ) f ( x k ) − f ( x k − 1 ) ⋅ ( x k − x k − 1 ) x_{k + 1} = x_k - \frac{f(x_k)} {f(x_k) - f(x_{k - 1})} \cdot (x_k - x_{k - 1}) xk+1=xk−f(xk)−f(xk−1)f(xk)⋅(xk−xk−1)
使用Matlab实现:
f = inline('x * e^x - 1');
x = 0;
xx = 0.5;
xxx = 0.6;
i=0;
while abs(xx-xxx) >= 1e-5
x = xx;
xx = xxx;
xxx = xx - f(xx) / (f(xx) - f(x)) * (xx - x);
i = i + 1;
end
经过迭代4次,得到 x 5 = 0.56714 x_5 = 0.56714 x5=0.56714。
如果需要看到更多小数位数,可以使用:
format long g
二分法
例题
用二分法求方程 2 e − x − s i n x = 0 2e^{-x} - sinx = 0 2e−x−sinx=0 在区间 [ 0 , 1 ] [0, 1] [0,1] 内的根,要求 ∣ x k − x ∗ ∣ < 1 2 × 1 0 − 5 | x_k - x^* | < \frac12 \times 10^{-5} ∣xk−x∗∣<21×10−5 。
实现
首先通过求端点值,求导,确定左边函数在 [ 0 , 1 ] [0, 1] [0,1] 有解,且是单根区间。
取 c = 0 , d = 1 c = 0, d = 1 c=0,d=1 ,将 [ c , d ] [c, d] [c,d] 对分,设对分点为 x 0 = 1 2 ( c + d ) x_0 = \frac12 (c + d) x0=21(c+d) ,计算 f ( x 0 ) f(x_0) f(x0) ,如果 f ( x 0 ) f(x_0) f(x0) 与 f ( c ) f(c) f(c) 同号,……
d n − c n = 1 2 n ( d − c ) d_n - c_n = \frac1{2^n} (d - c) dn−cn=2n1(d−c) ,只要n足够大,有根区间长度就会足够小,当长度达到精度要求时,取 x n = 1 2 ( c n + d n ) x_n = \frac12 (c_n + d_n) xn=21(cn+dn) 作为方程的根的近似值。
使用Matlab实现:
f = inline('2 * e^(-x) - sin(x)');
c = 0;
d = 1;
x = 1/2 * (c + d);
i = 0;
while d - c >= 1e-5/2
x = 1/2 * (c + d);
i ++;
if f(x) > 0
c = x;
elseif f(x) < 0
d = x;
else
break;
end;
end;
经过18次迭代, c = x 17 c = x_{17} c=x17 时, d − c < 1 2 × 1 0 − 5 d - c < \frac12 \times 10^{-5} d−c<21×10−5 ,得到原方程的解为: x ∗ = 1 2 ( c 18 + d 18 ) = 0.92103 x^* = \frac12 (c_{18} + d_{18}) = 0.92103 x∗=21(c18+d18)=0.92103 。
CG法
例题
取 x 0 = ( 0 , 0 ) T , 用 C G 法 求 解 : x_0 = (0, 0)^T,用CG法求解: x0=(0,0)T,用CG法求解:
[ 6 3 3 2 ] ⋅ [ x 1 x 2 ] = [ 0 − 1 ] \left[\begin{array}{cc} 6 & 3\\ 3 & 2\\ \end{array}\right] \cdot \left[\begin{array}{c} x_1\\ x_2\\ \end{array}\right] = \left[\begin{array}{c} 0\\ -1\\ \end{array}\right] [6332]⋅[x1x2]=[0−1]
实现
方程组化为: A ⋅ x = b A \cdot x = b A⋅x=b 。
取 x 0 = ( 0 , 0 ) T , r 0 = b − A x 0 = ( 0 , − 1 ) T , p 0 = r 0 x_0 = (0, 0)^T,r_0 = b - Ax_0 = (0, -1)^T,p_0 = r_0 x0=(0,0)T,r0=b−Ax0=(0,−1)T,p0=r0
α 0 = ( r 0 , r 0 ) ( p 0 , A p 0 ) = 1 2 \alpha_0 = \frac{(r_0, r_0)}{(p_0, Ap_0)} = \frac12 α0=(p0,Ap0)(r0,r0)=21
x 1 = x 0 + α 0 ⋅ p 0 = ( 0 , − 1 2 ) T x_1 = x_0 + \alpha_0 \cdot p_0 = (0, -\frac12)^T x1=x0+α0⋅p0=(0,−21)T
r 1 = r 0 − α 0 ⋅ A ⋅ p 0 = ( 3 2 , 0 ) T r_1 = r_0 - \alpha_0 \cdot A \cdot p_0 = (\frac32, 0)^T r1=r0−α0⋅A⋅p0=(23,0)T
β 0 = ( r 1 , r 1 ) ( r 0 , r 0 ) = 9 4 \beta_0 = \frac{(r_1, r_1)}{(r_0, r_0)} = \frac94 β0=(r0,r0)(r1,r1)=49
p 1 = r 1 + β ⋅ p 0 = ( 3 2 , − 9 4 ) T p_1 = r_1 + \beta \cdot p_0 = (\frac32, -\frac94)^T p1=r1+β⋅p0=(23,−49)T
α 1 = ( r 1 , r 1 ) ( p 1 , A p 1 ) = 2 3 \alpha_1 = \frac{(r_1, r_1)}{(p_1, Ap_1)} = \frac23 α1=(p1,Ap1)(r1,r1)=32
x 2 = x 1 + α 1 ⋅ p 1 = ( 1 , − 2 ) T x_2 = x_1 + \alpha_1 \cdot p_1 = (1, -2)^T x2=x1+α1⋅p1=(1,−2)T
r 2 = r 1 − α 1 ⋅ A ⋅ p 1 = ( 0 , 0 ) T r_2 = r_1 - \alpha_1 \cdot A \cdot p_1 = (0, 0)^T r2=r1−α1⋅A⋅p1=(0,0)T
在 r k = 0 或 ( p k , A p k ) = 0 r_k = 0 或 (p_k, Ap_k) = 0 rk=0或(pk,Apk)=0 时,计算终止,有 x k = x ∗ x_k = x^* xk=x∗ 。
使用Matlab实现:
x = [0;0];
A = [6,3;3,2];
b = [0;-1];
r = b - A * x;
p = r;
i = 0;
while !all(r == [0;0]) && !all(dot(p, A * p) == [0;0])
alpha = dot(r, r) / dot(p, A * p);
xx = x + alpha * p;
rr = r - alpha * A * p;
beta = dot(rr, rr) / dot(r, r);
pp = rr + beta * p;
i ++;
r = rr;
x = xx;
p = pp;
end;
边栏推荐
- Ingress Controller 0.47.0的Yaml文件
- Oracle 11g uses ords+pljson to implement JSON_ Table effect
- SQLI-LABS通关(less15-less17)
- Oracle 11g sysaux table space full processing and the difference between move and shrink
- Common prototype methods of JS array
- SQL注入闭合判断
- JS to determine whether there is a value in the object in the array
- Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
- Tool grass welfare post
- How to call WebService in PHP development environment?
猜你喜欢
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
mapreduce概念和案例(尚硅谷学习笔记)
Error in running test pyspark in idea2020
图解Kubernetes中的etcd的访问
Sqli-labs customs clearance (less15-less17)
ORACLE 11G利用 ORDS+pljson来实现json_table 效果
UEditor .Net版本任意文件上传漏洞复现
Analysis of MapReduce and yarn principles
SQLI-LABS通關(less6-less14)
TCP attack
随机推荐
Oracle segment advisor, how to deal with row link row migration, reduce high water level
SSM二手交易网站
类加载器及双亲委派机制
Explanation of suffix of Oracle EBS standard table
MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
Yolov5 practice: teach object detection by hand
MySQL组合索引加不加ID
2021-07-05C#/CAD二次开发创建圆弧(4)
Differences between ts and JS
Oracle rman半自动恢复脚本-restore阶段
PXC high availability cluster summary
Oracle段顾问、怎么处理行链接行迁移、降低高水位
Sqli labs customs clearance summary-page1
pm2简单使用和守护进程
Oracle 11g uses ords+pljson to implement JSON_ Table effect
php中根据数字月份返回月份的英文缩写
JSP智能小区物业管理系统
UEditor .Net版本任意文件上传漏洞复现
2021-07-17c /cad secondary development creation circle (5)
sqli-labs通關匯總-page2