牛顿法和弦截法java实现

Java实现牛顿法和弦截法

写个数值分析作业用计算器算起来太麻烦咯,还是写些代码自动化一下

package test;

public class mathHomework
{

	public static void main(String[] args)
	{
		double x0 = 2;
		double x1 = 0.6;
		newtonInteration(x0, 10);
		newtonXiashanInteration(x0, 3, 10);
//		xianjieInteration(0.5,0.6,10);
		myInteration(x0, 10);
	}

	/**
	 * 自己写的递推关系式
	 * 
	 * @param x
	 * @param repeat
	 * @return
	 */

	public static double interationFunc(double x)
	{
		return x -  (Math.pow(x, 3) - 5) / (3* x * x);
	}

	/**
	 * 函数表达式
	 * 
	 * @param x
	 * @return
	 */
	public static double func(double x)
	{

		// return x - Math.pow(Math.E, -x);
		return Math.pow((Math.pow(x, 3) - 5), 2);
	}

	/**
	 * 导数表达式
	 * 
	 * @param x
	 * @return
	 */
	public static double diffFunc(double x)
	{
		// return 1 + Math.pow(Math.E, -x);
		return 2 * (Math.pow(x, 3) - 5) * 3 * x * x;
	}

	/**
	 * 牛顿迭代法
	 * 
	 * @param x0
	 * @param times
	 * @param flag:使用自己的迭代函数
	 */

	public static void newtonInteration(double x0, int times)
	{
		double xi = x0;
		double xi1;
		System.out.println("---------------------牛顿迭代法------------------------");
		for (int i = 0; i < times; i++)
		{
			xi1 = xi - func(xi) / diffFunc(xi);
			System.out.println("x" + (i + 1) + "=" + xi1);
			xi = xi1;
		}

	}

	/**
	 * 牛顿下山法
	 * 
	 * @param x0
	 * @param times
	 */
	public static void newtonXiashanInteration(double x0, int repeat, int times)
	{
		double xi = x0;
		double xi1;
		System.out.println("---------------------牛顿下山法------------------------");
		for (int i = 0; i < times; i++)
		{
			xi1 = xi - repeat * func(xi) / diffFunc(xi);
			System.out.println("x" + (i + 1) + "=" + xi1);
			xi = xi1;
		}
	}

	/**
	 * 弦截法
	 * 
	 * @param x0
	 * @param x1
	 * @param times
	 */
	public static void xianjieInteration(double x0, double x1, int times)
	{
		double xi = x0;
		double xi1 = x1;
		double xi2;

		System.out.println("---------------------弦截法------------------------");

		for (int i = 0; i < times; i++)
		{
			xi2 = (xi1 - (xi1 - xi) * func(xi1) / (func(xi1) - func(xi)));
			System.out.println("x" + (i + 2) + "=" + xi2);
			xi = xi1;
			xi1 = xi2;
		}
	}

	public static void myInteration(double x0, int times)
	{
		double xi = x0;
		double xi1;
		System.out.println("---------------------自定义迭代法------------------------");

		for (int i = 0; i < times; i++)
		{
			xi1 = interationFunc(xi);
			System.out.println("x" + (i + 1) + "=" + xi1);
			xi = xi1;
		}
	}

}


输出结果:

---------------------牛顿迭代法------------------------
x1=1.875
x2=1.799537037037037
x3=1.7569482000561527
x4=1.7340843825074606
x5=1.7221969740024148
x6=1.7161297191047062
x7=1.7130638529350126
x8=1.7115226812070843
x9=1.710750012637576
x10=1.7103631547521934
---------------------牛顿下山法------------------------
x1=1.625
x2=1.7592455621301775
x3=1.6873915918533813
x4=1.7217235565732205
x5=1.7042221021520378
x6=1.7128820411801964
x7=1.708530291010196
x8=1.7107006098678912
x9=1.7096140754736906
x10=1.7101569971813315
---------------------自定义迭代法------------------------
x1=1.75
x2=1.7108843537414966
x3=1.7099764289169748
x4=1.709975946676833
x5=1.709975946676697
x6=1.709975946676697
x7=1.709975946676697
x8=1.709975946676697
x9=1.709975946676697
x10=1.709975946676697

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牛顿和弦都是数值计算中的迭代方,用于求解非线性方程的根。 牛顿: 牛顿基于泰勒展开的思想,通过迭代逼近方程的根。假设需要求解的方程为f(x)=0,给定初始值x0,通过以下公式进行迭代: x_n+1 = x_n - f(x_n)/f'(x_n), 其中f'(x_n)是函数f(x)在x_n点的导数。通过不断迭代,可以逐步逼近方程的根。以下是使用Matlab实现的牛顿程序示例: function xn = newton_method(f, f_prime, x0, tol, max_iter) xn = x0; for i = 1:max_iter xn_next = xn - f(xn)/f_prime(xn); if abs(f(xn_next)) < tol xn = xn_next; break; end xn = xn_next; end end 其中,f是需要求解的方程函数,f_prime是f的导数函数,x0是初始值,tol是容差,max_iter是最大迭代次数。 弦: 弦也是一种迭代方,与牛顿类似,只是在迭代公式中用差商代替了导数。给定初始的两个点x0和x1,通过以下公式进行迭代: x_n+1 = x_n - f(x_n)*(x_n-x_n-1) / (f(x_n)-f(x_n-1)), 通过不断迭代,可以逐步逼近方程的根。以下是使用Matlab实现的弦程序示例: function xn = secant_method(f, x0, x1, tol, max_iter) xn_minus_1 = x0; xn = x1; for i = 1:max_iter xn_next = xn - f(xn)*(xn-xn_minus_1)/(f(xn)-f(xn_minus_1)); if abs(f(xn_next)) < tol xn = xn_next; break; end xn_minus_1 = xn; xn = xn_next; end end 其中,f是需要求解的方程函数,x0和x1是初始值,tol是容差,max_iter是最大迭代次数。 以上是牛顿和弦的Matlab程序示例和简要介绍。希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值