MATLAB(十)方程式求根

article/2025/10/12 19:22:39

文章目录

  • 前言
  • 符号寻根法
  • 符号根查找:solve ()
  • 解决多个方程
  • 用符号表示的方程
  • Exercise练习
  • 符号区分:diff ()
  • Exercise练习
  • 象征性的集成:int()
  • Exercise练习
  • 回顾函数句柄(@)
  • fsolve()
  • Exercise练习
  • fzero()
  • 求多项式的根:roots()
  • 二分法(或分组法、方括号法)
    • 二分算法流程图
  • 牛顿迭代法(打开)
    • 牛顿迭代算法流程图
  • 递归函数

前言

此篇文章是我在B站学习时所做的笔记,主要讲象征性的方法、解决数字根、递归函数,部分为亲自动手演示过的,方便复习用。此篇文章仅供学习参考。


提示:以下是本篇文章正文内容,下面案例可供参考

符号寻根法

  1. 用符号而不是数字进行数学运算
  2. 符号数学是用“符号变量”来完成的
  3. 使用sym或syms创建符号变量
>> syms x
>> x + x + x
ans =
3*x
>> (x + x + x)/4
ans =
(3*x)/4>> y=x^2-2*x-8
y =
x^2 - 2*x - 8
>> x=sym('x');
>> x + x + x
ans =
3*x

符号根查找:solve ()

函数求解可以为方程求根
在这里插入图片描述
方法一

syms x
y = x*sin(x)-x;
solve(y, x)
ans =0pi/2

方法二

syms x
solve(x*sin(x)-x, x)
ans =0pi/2

在这里插入图片描述

>> syms x
>> y=(cos(x))^2-(sin(x))^2;
>> solve(y,x)
ans =
pi/4>> syms x
y=(cos(x))^2+(sin(x))^2;
solve(y,x)
ans =
Empty sym: 0-by-1

解决多个方程

用符号方法解这个方程:
在这里插入图片描述

>> syms x y
eq1 = x - 2*y - 5;
eq2 = x + y - 6;
A = solve(eq1,eq2,x,y)
A = 包含以下字段的 struct:x: [1×1 sym]y: [1×1 sym]>> A.x
ans =
17/3>> A.y
ans =
1/3

用符号表示的方程

  • 如果给我们一个用符号表示的函数呢?
    在这里插入图片描述
>> syms x a b
>> solve(a*x^2-b)
ans =b^(1/2)/a^(1/2)-b^(1/2)/a^(1/2)
  • x总是第一个被解决的选项
  • 如果想用a和x表示b呢?
>> syms x a b
>> solve(a*x^2-b, b)
ans =
a*x^2

Exercise练习

  1. 用符号方法求解这个方程
    在这里插入图片描述
>> syms x a y b r
>> solve((x-a)^2+(y-b)^2-r^2)
ans =a + (b + r - y)^(1/2)*(r - b + y)^(1/2)a - (b + r - y)^(1/2)*(r - b + y)^(1/2)
  1. 用符号法求矩阵的逆
    在这里插入图片描述
>> syms a b c d 
>> A=[a b;c d]; inv(A)
ans =
[  d/(a*d - b*c), -b/(a*d - b*c)]
[ -c/(a*d - b*c),  a/(a*d - b*c)]

>> syms a b c d 
>> A=[a b;c d];
>> B=inv(A); disp(B)
[  d/(a*d - b*c), -b/(a*d - b*c)]
[ -c/(a*d - b*c),  a/(a*d - b*c)]

符号区分:diff ()

计算符号函数的导数:
在这里插入图片描述

>> syms x
y = 4*x^5; 
yprime = diff(y)
yprime =
20*x^4

与之前的差值计算区别一下:

>> y=[5 6];
>> diff(y)
ans =1

Exercise练习

在这里插入图片描述

>> syms x 
>> y=(exp(x^2))/(x^3-x+3);
>> yprime=diff(y)
yprime =
(2*x*exp(x^2))/(x^3 - x + 3) - (exp(x^2)*(3*x^2 - 1))/(x^3 - x + 3)^2
>> syms x y
>> g=((x^2)+x*y-1)/((y^3)+x+3);
>> gprime=diff(g)
gprime =
(2*x + y)/(y^3 + x + 3) - (x^2 + y*x - 1)/(y^3 + x + 3)^2

象征性的集成:int()

计算符号函数的积分:
在这里插入图片描述
在这里插入图片描述

>> syms x; y = x^2*exp(x);
z = int(y); 
>> z
z =
exp(x)*(x^2 - 2*x + 2)>> z = z-subs(z, x, 0)z =
exp(x)*(x^2 - 2*x + 2) - 2

Exercise练习

在这里插入图片描述

>> y=@(x) (x.^2-x+1)./(x+3); integral(y,0,10)
ans =29.0624

回顾函数句柄(@)

  • 句柄是指向函数的指针
  • 可以用来传递函数给其他函数
  • 例如,以下函数的输入是另一个函数:
function [y] = xy_plot(input,x)
% xy_plot receives the handle of a function and plots that 
% function of x
y = input(x); plot(x,y,'r--');
xlabel('x'); ylabel('function(x)');
end

在这里插入图片描述

fsolve()

在这里插入图片描述

Exercise练习

在这里插入图片描述

>> f=@(x) ([2*x(1)-x(2)-exp(-x(1));-x(1)+2*x(2)-exp(-x(2))]);
>> fsolve(f,[-5 -5])Equation solved.fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.<stopping criteria details>ans =0.5671    0.5671

fzero()

在这里插入图片描述

当且仅当函数穿过x轴时,求零

>> f=@(x)x.^2
fzero(f,0.1)f =包含以下值的 function_handle:@(x)x.^2正在退出 fzero: 将终止搜索包含符号变化的区间因为在搜索期间遇到 NaN 或 Inf 函数值。
(-1.37296e+154 处的函数值为 Inf。)
请检查函数或使用其他起始值重试。ans =NaN>> fsolve(f,0)Equation solved at initial point.fsolve completed because the vector of function values at the initial point
is near zero as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.<stopping criteria details>ans =0

在这里插入图片描述

求多项式的根:roots()

求这个多项式的根:
在这里插入图片描述

>> roots([1 -3.5 2.75 2.125 -3.875 1.25])ans =2.0000 + 0.0000i-1.0000 + 0.0000i1.0000 + 0.5000i1.0000 - 0.5000i0.5000 + 0.0000i

二分法(或分组法、方括号法)

在这里插入图片描述

二分算法流程图

在这里插入图片描述

牛顿迭代法(打开)

在这里插入图片描述
在这里插入图片描述

牛顿迭代算法流程图

在这里插入图片描述

递归函数

  • 调用自身的函数
  • 例如,整数n的阶乘
    在这里插入图片描述
  • 一个阶乘可以用另一个阶乘来定义:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
>> fact(3)ans =6

如若侵权,请及时与我联系。


http://chatgpt.dhexx.cn/article/BCAGf6vJ.shtml

相关文章

matlab根号二,matlab根号二怎么打_常见问题解析,matlab,根号二,根号

matlab级数求和的方法_常见问题解析 matlab级数求和使用symsum函数&#xff0c;有4种方式&#xff0c;分别为symsum(expr)、symsum(expr,v)、symsum(expr,a,b)和symsum(f,v,a,b)。 matlab根号二怎么打 matlab中根号使用^来表示&#xff0c;根号二的表示方法为&#xff1a;2^0.5…

matlab 坐标轴根号,Matlab中根号表示怎么操作?根号表示教程分享

最近很多网友表示自己不清楚Matlab中根号表示的操作&#xff0c;而本篇文章就来给大家讲解Matlab中根号表示的教程介绍&#xff0c;大家都来一起学习吧。 Matlab中根号表示的教程介绍 常数根号的表示方法 1、打开matlab&#xff0c;在命令行窗口中输入“9^0.5”&#xff0c;按回…

matlab中根号的表示方法,Matlab中根号表示方法分享

现在许多朋友都已经安装使用Matlab这款软件&#xff0c;但不少伙伴使用过程里还不会根号表示方法&#xff0c;那么究竟是如何操作的呢&#xff1f;今日就来下文看看Matlab中根号表示教程吧。 常数根号的表示方法 1、打开matlab&#xff0c;在命令行窗口中输入9^0.5&#xff0c;…

记Git报错-refusing to merge unrelated histories

环境 系统&#xff1a;win10 git version 2.9.0.windows.1 创建一个新的分支&#xff0c;很久没用了。要重新启用&#xff0c;拉取最新分支的代码&#xff0c;结果报错&#xff1a;refusing to merge unrelated histories fatal: refusing to merge unrelated histories 解…

git: refusing to delete the current branch

最近&#xff0c;不知怎么操作&#xff0c;导致远程test分支无法删除。 原因是&#xff0c;远程仓库里的当前分支是test。进入远程仓库目录&#xff0c;git branch。由于已经解决&#xff0c;所以没有图。在网上找相关资料&#xff0c;都是github里面的操作。但是这个是自己搭建…

fatal: refusing to merge unrelated histories解决

背景&#xff1a;由于公司进行了仓库迁移&#xff0c;同事直接把代码init推进了新的仓库地址&#xff0c;我想在本地代码上切换远程仓库地址进行拉取远端代码时出现报错“fatal: refusing to merge unrelated histories”&#xff0c;大概意思是&#xff1a;“拒绝合并不相关的…

解决Git报错: fatal: refusing to merge unrelated histories

如果合并了两个不同的开始提交的仓库&#xff0c;在新的 git 会发现这两个仓库可能不是同一个&#xff0c;为了防止开发者上传错误&#xff0c;于是就给下面的提示 git无法pull仓库refusing to merge unrelated histories &#xff08;拒绝合并不相关仓库&#xff09; 遇到上问…

解决Git refusing to merge unrelated histories

背景&#xff1a;在本地初始化了一个Git代码仓库&#xff0c;关联到Github上新建的仓库&#xff0c;第一次执行git pull origin master 拉取远程分支时&#xff0c;出现标题上的问题&#xff08;Git 2.9之后的版本才会出现此问题&#xff09;。 refusing to merge unrelated hi…

git pull 无法下载合并 报错:refusing to merge unrelated histories

场景 我本地的代码和线上的代码&#xff0c;需要合并一下&#xff0c;因为线上的代码有修改的地方&#xff0c;我本地的代码也有修改的地方&#xff0c;直接覆盖会有问题&#xff0c;于是想到了用git(平时不用&#xff0c;直接ftp拉上去直接覆盖的)&#xff0c;把修改过的文件…

git提交 出现 : fatal: refusing to merge unrelated histories

项目场景&#xff1a; 场景&#xff1a;本地文件在变更之后 无法提交到远程仓库并报错 fatal: refusing to merge unrelated histories 我最近因为同一个本地文件做了不同的修改&#xff0c;要提交到不同的仓库会经常提示此问题 原因分析&#xff1a; 问题的分析&#xff1a;…

git pull 提示错误 fatal: refusing to merge unrelated histories

从远程拉项目到本地的时候提示错误 造成 fatal: refusing to merge unrelated histories错误的原因有以下几点&#xff1a; 有一个包含一些提交的新 Git 存储库。然后&#xff0c;您尝试从现有的远程仓库中提取。合并变得不兼容&#xff0c;因为分支和远程拉取的历史不同。当…

git初始化错误fatal: refusing to merge unrelated histories

发生原因: git服务上创建了git远程仓库,并且创建README.md文件 本地初始化项目 关联远程仓库:git remote add origin “远程仓库地址” 可以:git remote -v 查看是否关联 再 git init git add README.md git commit -m “first commit” 现在将本地仓库push远程,但是远程有re…

Git报错关于 refusing to merge unrelated histories

refusing to merge unrelated histories 把远程仓库的地址都配置好了之后&#xff0c;执行git pull操作&#xff0c;往往会出现如上图所示 报错信息&#xff0c;这个问题出现的根本原因就是本地的库和远程的库本质上是两个库&#xff0c;如果是直接从git上面clone过来的项目则不…

解决TortoiseGit报错fatal: refusing to merge unrelated histories

如果是用命令行的&#xff0c;就是直接pull后面加--allow-unrelated-histories&#xff1a; git pull origin master --allow-unrelated-histories 如果是用tortoisegit的就是在推送的时候勾选强制覆盖所有&#xff0c;作用就是跟上面命令行加后缀差不多&#xff0c;无视无关历…

Refusing to delete

Refusing to delete 一、场景二、报错三、解决方案 一、场景 npm升级依赖时一直提示不能删除某某&#xff0c;删除某某后重试 二、报错 Refusing to deleteXXX,Move it away, and try again 三、解决方案 改为cnpm 下载

idea使用git提交代码报异常refusing to merge unrelated histories和unknown option `allow-unrelated-histories‘

目录 一、异常refusing to merge unrelated histories 二、异常unknown option allow-unrelated-histories 一、异常refusing to merge unrelated histories 使用git提交代码的时候报异常&#xff1a;refusing to merge unrelated histories 解决方法&#xff1a; 这个错误通…

Livy

本来这篇是要分享到我的“大数据与人工智能”专栏的&#xff0c;关注我的人虽然不多&#xff0c;但是我怕只关注devops的童鞋可能也想瞄一眼&#xff0c;所以在此分享。之后&#xff0c;只要不是属于devops工作范围的&#xff0c;我就一律不在此分享了&#xff0c;想了解其他的…

Cloudera Manager中安装部署Livy服务

制作Livy的Parcel包和csd文件 将Parcel包和manifest.json文件部署到httpd服务中 [rootnode01 ~]# mkdir -p /var/www/html/livy [rootnode01 ~]# cd /var/www/html/livy [rootnode01 livy]# cp /root/github/cloudera/cm-livy-scripts/LIVY-1.0-xenial.parcel ./ [rootnode01 …

livy在交互式查询中的深度定制

随着SparkSql在大规模数据分析中的运用越来越广&#xff0c;在大数据分析平台中集成SparkSql提供用户交互式sql查询的功能已经成为了很多开发者的选择&#xff0c;而将SparkSql作为rest服务有两种方式: jobserver和livy&#xff1b;其中livy作为Apache的孵化项目&#xff0c;其…

livy安装文档

1、下载Livy(可以自己下载源代码进行编译) http://livy.incubator.apache.org/download/ 源代码在git 上能够找到apache 版本和cloudera 两个版本 https://github.com/cloudera/livy 有比较详细的说明文档。 2、将livy的压缩包放到Linux 主机上&#xff0c;并解压 3、配…