数值计算方法——不动点迭代和牛顿迭代法
不动点迭代和牛顿迭代法
- MATLAB基础
- feval函数
- format long
- syms x
- 简单迭代法【不动点迭代】
- Newton 迭代法
- 作业
MATLAB基础
feval函数
用于求函数值
基本使用格式:y=feval(fhandle, x)
%fhandle——函数表达式,x——变量值[y1, y2, ...] = feval(fhandle, x1,..., xn)
format long
设置输出格式,表示高精度输出
syms x
定义符号变量,用于占位符占位!
简单迭代法【不动点迭代】
大致讲解:
不动点可看成 y=φ(x)与y=xy=φ(x)与y=xy=φ(x)与y=x 的交点
MATLAB实现:
function [x,count]=Simple_iterative_method(x0,tol)
format long %High precision output
error=1; %The value of the initialization error
count=0; %Initialization counter
while error>tol %Shutdown criteriax=f(x0); %Function to be solvederror=abs(x-x0); %Update the error of each stepx0=x; %Update iteration pointcount=count+1;
end
end%%Definition of function to be solved
function f=f(x)
f=(10/(4+x))^(1/2);
end
测试文件:
clear; %Empty variable
clc; %Clear screen
tol=1e-9; %Set the required precision
x0=1.5; %Exact solution of the point near
[x,count]=Simple_iterative_method(x0,tol); %Use simple iterative algorithm
disp('The exact solution is as follows:')
disp(x);
disp('Number of iterations required:')
disp(count);
结果展示:
Newton 迭代法
matlab算法实现
function [x,count]=Newton_it(x0,tol)
format long
error=1;
count=0;
while error>tolx=x0-f(x0)/df(x0); %Newton iteration schemeerror=abs(x-x0); x0=x;count=count+1;
end
end%%Enter the function test below
function f=f(x0)
syms x; %Defining symbolic variables
m(x)=x^2-115;
f=vpa(m(x0)); %Calculate the value of the symbolic variable
endfunction df=df(x0) %Calculating the first derivative of a function
syms x; %Defining symbolic variables
m(x)=x^2-115;
df(x)=diff(m(x));
df=vpa(df(x0));
end
测试文件
clear; %Empty variable
clc; %Clear screen
x0=10;
tol=1e-6;
[x,count]=Newton_it(x0,tol);
disp('The exact solution is as follows:')
disp(x);
disp('Number of iterations required:')
disp(count);
结果展示:
作业
- 用两种迭代方式求解一个多项式的根,多项式满足下面两个条件:
(1)7次多项式
(2)系数在(0,7] 之间
要求:写出收敛阶、收敛速度、初值 - 求出下面方程的根并作图x2+(54y−∣x∣)2−4=0x^{2}+\left ( \frac{5}{4}y-\sqrt{|x|} \right )^{2}-4=0x2+(45y−∣x∣)2−4=0
初值(这个方程的初值不好找!)
尝试解答:
1.如何生成一个随机整数向量(满足第二个条件叭)
round(rand(1,5)*6+1)
2.构造随机多项式
function f=ex6_1(x0,p0) %Constructing polynomials
syms x; %Defining symbolic variables
y(x)=poly2sym(p0);
disp(y(x));
f=vpa(y(x0)); %Calculate the value of the symbolic variable
end
p0=round(rand(1,8)*6+1);
%If you put it in a function, every time you call the function, a polynomial will be generated randomly
x0=2;
f=ex6_1(x0,p0);
disp(f);
x0=0;
f=ex6_1(x0,p0);
disp(f);
输出结果:
3.尝试使用简单迭代法
function [x,count]=Simple_iterative_method(x0,tol)
format long %High precision output
error=1; %The value of the initialization error
count=0; %Initialization counter
while error>tol %Shutdown criteriax=f(x0); %Function to be solvederror=abs(x-x0); %Update the error of each stepx0=x; %Update iteration pointcount=count+1;
end
end%%Definition of function to be solved
function f=f(x0)
syms x; %Defining symbolic variables
p0=round(rand(1,8)*6+1); %Each time the function is called, a random polynomial is constructed
y(x)=poly2sym(p0);
f=vpa(y(x0)); %Calculate the value of the symbolic variable
end
clear; %Empty variable
clc; %Clear screen
tol=1e-9; %Set the required precision
x0=0; %Exact solution of the point near
[x,count]=Simple_iterative_method(x0,tol); %Use simple iterative algorithm
disp('The exact solution is as follows:')
disp(x);
disp('Number of iterations required:')
disp(count);
输出结果(这是什么东西…):
构造多项式并改写成 x=ϕ(x)x=\phi(x)x=ϕ(x) 的形式
function [f,df]=ex6_1(x0,p0)
syms x; %Defining symbolic variablesS
y(x)=poly2sym(p0);
% disp('This is a randomly generated equation of degree 7')
% disp(y(x));
q(x)=((y(x)-p0(6)*x^2)/(-p0(6)))^(1/2);
% disp('This is phi(x)');
% disp(q(x));
f=vpa(q(x0)); %Calculate the value of the symbolic variable
df(x)=diff(q(x));
df=vpa(df(x0));
end
function [x,count]=Newton_it(x0,tol)
format long
error=1;
count=0;
p0=round(rand(1,8)*6+1);
while error>tol && count <50[f,df]=ex6_1(x0,p0);x=x0-f/df; %Newton iteration schemeerror=abs(x-x0);disp('error:')disp(error);x0=x;count=count+1;
end
end
clear; %Empty variable
clc; %Clear screen
x0=10;
tol=1e-3;
[x,count]=Newton_it(x0,tol);
disp('The exact solution is as follows:')
disp(x);
disp('Number of iterations required:')
disp(count);
function [x,count]=Simple_iterative_method(x0,tol)
format long %High precision output
error=1; %The value of the initialization error
count=0; %Initialization counter
p0=round(rand(1,8)*6+1);
disp(p0);
while error>tol && count<20 %Shutdown criteriax=ex6_1(x0,p0); %Function to be solveddisp(x0);count=count+1;error=abs(x-x0);disp('it is error');disp(error);x0=x;
end
end
clear; %Empty variable
clc; %Clear screen
tol=1e-5; %Set the required precision
x0=0; %Exact solution of the point near
[x,count]=Simple_iterative_method(x0,tol); %Use simple iterative algorithm
disp('The exact solution is as follows:')
disp(x);
disp('Number of iterations required:')
disp(count);
close all;
clear all;
clc;
ezplot('x^2+((5/4)*y-sqrt(abs(x)))^2-4=0',[-3,3])
grid on
数值计算方法——不动点迭代和牛顿迭代法相关推荐
- 不动点迭代法和牛顿迭代法
不定点迭代法 方程的根 不动迭代法的概念 代码实现 import numpy import numpy as np from sympy import * import math import mat ...
- 数值计算方法——Jacobi迭代法和G-S迭代法
Jacobi迭代法和G-S迭代法 没完成的代码,仅做储存 矩阵常用操作 Jacobi迭代实现 G-S迭代实现 没完成的代码,仅做储存 function [L,D,U]=LU(A) n=length(A ...
- 数值分析-----不动点迭代和牛顿迭代(Python)
目录 1 概述 1.1 迭代法 1.2 Newton迭代法 2 不动点迭代 2.1 基本思想 2.2 案例及实现 3 牛顿迭代 3.1 基本思想 3.2 案例及实现 ...
- 基于Python实现Aitken迭代法和牛顿迭代法
目录 简单迭代法 简单迭代法的Aitken加速算法 基于Pyhton实现的Aitken加速算法 牛顿迭代法 基于Pyhton实现的牛顿迭代法 对于非线性方程,我们可以使用迭代的方式求出近似解.下面介绍 ...
- C语言编写Picard迭代和牛顿迭代法
为了完成学校计算方法的作业,花了两个多星期将所有实验报告编写出来,看到同桌找代码的辛苦,CSDN也没有这类代码,我只是一时兴起,想分享我写的代码与大家共飨,有错误还请多多指正,代码都不难,也是为了让C ...
- 非线性方程求解 :二分迭代法和牛顿迭代法
在机器人算法开发中,经常会遇到求解非线性方程.非线性方程的求解十分困难,这里介绍两种方法: 1. 二分法 2.牛顿迭代法 定义: 非线性方程,就是因变量与自变量之间的关系不是线性的关系,这类方程很多, ...
- 迭代法解方程:牛顿迭代法、Jacobi迭代法
牛顿迭代公式 设已知方程f(x)=0的近似根x0 ,则在x0附近f(x)可用一阶泰勒多项式 近似代替.因此, 方程f(x)=0可近似地表示为p(x)=0.用x1表示p(x)=0的根,它与f(x)=0的 ...
- 数值分析】迭代法解方程:牛顿迭代法、Jacobi迭代法
转自:http://blog.csdn.net/xiaowei_cqu 博主写的很好 ps:最近在学数值分析,本来打算自己写这博文,呀呀呀,看到博主这篇博文这么棒,就转载了.方程组高斯消去法和 ...
- 牛顿迭代法求解平方根
一个实例 迭代简介 牛顿迭代法 牛顿迭代法简介 简单推导 泰勒公式推导 延伸与应用 一个实例 //java实现的sqrt类和方法 public class sqrt {public static do ...
最新文章
- 改进量子计算机的三项创新
- 神经拟态芯片拉近AI与人脑距离
- C#的ARRAYLIST 和HASHTABLE示例代码
- Shell第三篇:基本语法
- 15-CoreData删除所有数据之NSBatchDeleteRequest
- DynamipsGUI2.8 交换模块试用(Etherchannel)
- 《Hadoop大明白》【1】Hadoop的核心组件
- 近期GitHub上最热门的开源项目(附链接)
- python mpi开销_GitHub - hustpython/MPIK-Means
- 零基础转行自学前端,怎么学习更系统?
- 删除购物车ajax js,在购物车中使用ajax在woocommerce中移除产品
- 敏捷开发绩效管理之七:敏捷开发生产率(下)(简化功能点分析,NESMA,两级简化)...
- 首批 5G 手机到位;来电显示暗藏黑色利益链;印度下架抖音国际版 | 极客头条...
- Apache常见配置及问题
- jms.jar 2.0_JMS API概述:JMS 1.x和JMS 2.x
- ImageLoader的简单分析
- 无共享模式的数据架构
- php require失败,关于php:致命错误:require_once()[function.require]:要求打开失败
- AM、FM、PM调制技术
- hadoop--环境搭建--域名解析失败