关于割线法的理论及相关公式如下https://baike.baidu.com/item/%E5%89%B2%E7%BA%BF%E6%B3%95/5806354

本文对理论方面不再进行讨论,仅涉及代码。代码中包含了部分解释。通过对参数(点的位置,初始值,容差)的更改可以满足大部分题目的要求。如果对您有帮助,麻烦点个赞,多谢。

%% We'll try solving 3x^3-5=0 using the secant method
tol = 10^(-4);          %% set tolerance to 10^(-4).
epsilon = 1;      %% An arbitrarily large value to begin with.
steps = 1;        %% To keep track of the number of iterations; initially = 1.  %% If we get in an infinite loop we'll break out of it
%% break_flag = 0 indicates the default state
%% break_flag = 1 indicates a break
break_flag = 0;%% set the seed points = starting x values.
%% The secant method requires two seed points.
%% Try x0 = -203 and x1 = -10.
x0 = -203;
x1 = -10;
fprintf("{0, %.8f},\n",x0);
fprintf("{1, %.8f},\n",x1);while epsilon > tolx2 =x1-f(x1)*(x1-x0)/(f(x1)-f(x0));          %% compute the new value of x = g(x0,x1)X(steps,1) = x1;    %% At each step store x1 and x2 in matrix XX(steps,2) = x2;fprintf("{%d, %.8f},\n",steps+1,x2);epsilon = abs(x2-x1); %% absolute difference between new and old xsteps = steps + 1;%% If we're in an infinite loop or the solution is diverging%% let's break out if it.if steps > 120000 || epsilon == infbreak_flag = 1;break endx0 = x1;    %% replace x0 with x1x1 = x2;    %% replace x1 with x2
end%% Now let's plot all of the data from our iterations
figure(1)
clf
plot(X(:,1),X(:,2));    %% plot all the data with lines
hold on
scatter(X(:,1),X(:,2)); %% replot all the data with dots
scatter(X(1,1),X(1,2),75,'g','d','filled','MarkerEdgeColor','b'); %% the starting point
scatter(X(end,1),X(end,2),75,'r','d','filled','MarkerEdgeColor','b'); %% the end point
if break_flag   %% a different plot heading for divergent and convergent results heading = sprintf('the secant method did not converge after %d steps',steps-1);
elseheading = sprintf('after %d steps, x = %.8f',steps-1,x1);
end
title(heading);function ans = f(x)  %%% define the function to compute the roots of.ans = 3.*x^3-5;
end    

用matlab实现割线法求根相关推荐

  1. 2021-01-07 matlab数值分析 非线性方程求根 牛顿法

    matlab数值分析 非线性方程求根 牛顿法 %牛顿法求非线性方程的根: % 输入:fun--非线性函数:dfun--非线性函数导数:x0--初始值:tol--精度: % 输出:x--非线性方程数值根 ...

  2. 使用MATLAB求解方程求根——学习笔记

    使用MATLAB求解方程求根--学习笔记 碎碎念:终于参加完了某比赛,连续大约摸了两天的鱼,就在昨天由于自己的操作失误,亲手将电脑给烧了,这就是上天在暗示我是时候加油为接下来的两场比赛和一个考试努力啦 ...

  3. matlab多项式计算(求根、由根创建、导数、积分)

    1 matlab多项式求根 举例:求解多项式3x4-10x3+15x+1000的根 Matlab代码 %% 多项式求解 p = [3 -10 0 15 1000]; r = roots(p); 运行结 ...

  4. matlab方程近似求根,第七讲MATLAB中求方程的近似根(解)教学目的学习matlab中求根命令.doc...

    第七讲MATLAB中求方程的近似根(解)教学目的学习matlab中求根命令 第七讲 MATLAB中求方程的近似根(解) 教学目的:学习matlab中求根命令,了解代数方程求根求解的四种方法,即图解法. ...

  5. matlab方程近似求根,matlab求解方程的几种方法

    k=i-1; xk=x(i);[(i-1) xk yk piancha xdpiancha] return ; end end if i>gxmax disp('请注意:迭代次数超过给定的最大值 ...

  6. matlab之方程式求根

    一.利用syms和solve() 利用syms和sym定义一个标志变量 syms x sym('x') 2.求方程根: 求x-2y=5与x+y=6的联立方程组的根: 3.对函数求微分 4.对函数求积分 ...

  7. Matlab一元二次方程求根

    因为Matlab能进行复数运算,所以可以直接求解一元二次方程. 叮~~ function [x]=equation(a,b,c) d=b*b-4*a*c; x=[(-b+sqrt(d))/(2*a), ...

  8. 二分法求方程根matlab,matlab用二分法求方程 的正根,要求误差小于0.0005

    matlab编程题:用二分法求方程x^3-3*x-1=0的根 先建立二分法的fun.m文件,代码如下:functionfun(a,b,e)%f是自定义的函数%a为隔根区间左端点,b为隔根区间右端点,e ...

  9. 通用求根算法zeroin_Modern Robotics运动学数值解法及SVD算法(C matlab)

    前言 原著之前CSDN已经注销,新CSDN Galaxy_Robot的博客_CSDN博客-机器人,C语言,我是谁?领域博主​blog.csdn.net 这半个月的业余时间研究了机器人逆运动学的解析解法 ...

最新文章

  1. 这三天低效率开发的总结,我都做了些什么啊?
  2. mysql binlog_checksum_【原创】研发应该懂的binlog知识(下)
  3. Python 程序扩展名(py, pyc, pyw, pyo, pyd) 及发布程序时的选择
  4. Java 设计模式之适配器模式
  5. 丢掉xml使用JavaConfig配置Spring
  6. 【Zigbee技术入门教程-02】一图读懂ZStack协议栈的核心思想与工作机理
  7. 第5章 Python 数字图像处理(DIP) - 图像复原与重建4 - 指数噪声
  8. Java枚举:您拥有优雅,优雅和力量,这就是我所爱!
  9. .Net/C#中Cache的用法
  10. 艾蔻智能机器人怎么连接网络_艾蔻扫地机器人z3怎么用?
  11. objective-C 的OOP(上)--类定义、继承及方法调用
  12. RFID(Radio Frequency Identification)技术,又称无线射频识别
  13. Ardunio开发实例-AM2320温湿度传感器
  14. 用户密码MD5和SHA加密
  15. 推荐系统(一):不了解推荐系统,基本在被out的队列,岌岌可危
  16. 空洞卷积(dilated convolution)理解
  17. 概率DP——BZOJ4008 [HNOI2015]亚瑟王
  18. 微信开通检测平台应如何选择?
  19. 【大学生必备神器网站】免费论文下载、免费2T网盘、各类免费课程、在线高数引擎...收藏了4年的宝藏学习网站最全の整理推荐!
  20. 微信朋友圈点赞测试点【杭州多测师】【杭州多测师_王sir】

热门文章

  1. Apache配置文件介绍
  2. Vue踩坑 npm ERR code ELIFECYCLE
  3. idea中采用git上传代码详细步骤
  4. 【SOLIDWORKS】SOLIDWORKS2020工程图中直径和半径的标注更改
  5. taskflow以任务节点启动任务
  6. [数理统计] 分布族谱图
  7. Linux的用户及权限管理
  8. Java.IO 字符流读写文件
  9. dcloud mui html5plus 5+sdk
  10. C语言编程求某人岁数