MATLAB 数值微积分


前言

b站课程《MATLAB教程_台大郭彦甫(14课)》学习记录


一、Differentiation 微分

在这里插入图片描述

二、Polynomial Differentiation 多项式微分

在这里插入图片描述

三、Representing Polynomials in MATLAB

Polynomials were represented as row vectors 多项式用行向量表示
For example, consider the equation
在这里插入图片描述
To enter this polynomial into MATLAB, use
p = [1 0 -2 -5];

四、Values of Polynomials: polyval()

在这里插入图片描述

a = [9,-5,3,7]; x = -2:0.01:5;
f = polyval(a,x); 
plot(x,f,'LineWidth', 2);
xlabel('x'); ylabel('f(x)');
set(gca, 'FontSize', 14)

在这里插入图片描述

五、Polynomial Differentiation 多项式微分: polyder()

在这里插入图片描述

>> p=[5 0 -2 0 1];
polyder(p)
ans =
    20     0    -4     0
polyval(polyder(p),7)
ans =
        6832

六、Exercise

在这里插入图片描述
conv(): 多项式相乘

a = [20 -7 5 10];
b = [4 12 -3];
t = conv(a,b);
hold on;
x = -2:0.01:1;
plot(x,polyval(t,x),'--b');
plot(x,polyval(polyder(t),x),'-r');
hold off;
xlabel('x');
legend('f(x)','f`(x)');
set(gca,'XLim',[-2,1]);
set(gca,'YLim',[-800,800]);

七、Polynomial Integration 多项式积分

在这里插入图片描述

八、Polynomial Integration: polyint()

在这里插入图片描述
我们要给MATLAB提供一个常量3,作为C的值

>> p=[5 0 -2 0 1];
polyint(p, 3)
ans =
    1.0000         0   -0.6667         0       1.0000    3.0000
polyval(polyint(p, 3),7)
ans =
   1.6588e+04

九、Numerical Differentiation 数值微分

在这里插入图片描述

十、Differences: diff()

diff()计算vector中相邻元素之间的差值

>> x = [1 2 5 2 1];
diff(x)
ans =
     1     3    -3    -1

Exercise

obtain the slope(斜率) of a line between 2 points (1,5) and (2,7)
在这里插入图片描述

>> x = [1 2]; y = [5 7];
slope = diff(y)./diff(x)
slope =
     2

十一、Numerical Differentiation Using diff()

在这里插入图片描述

>> x0 = pi/2; h = 0.1;
x = [x0 x0+h];
y = [sin(x0) sin(x0+h)]; 
m = diff(y)./diff(x)
m =
   -0.0500

How does ℎ affect accuracy?
h越小越准确

Exercise

在这里插入图片描述

x0 = pi/2; h = 0.1;
while h>0.0000001
    x = [x0 x0+h];
    y = [sin(x0) sin(x0+h)]; 
    m = diff(y)./diff(x);
    h = h/10;
    m
end
m =
     0
m =
   -0.0500
m =
   -0.0050
m =
  -5.0000e-04
m =
  -5.0000e-05
m =
  -5.0000e-06
m =
  -5.0004e-07
m =
  -4.9960e-08

十二、How to Find the 𝑓′ over An Interval [0,2𝜋]?

在这里插入图片描述

十三、Find sin′(𝑥) over 𝑥 = [0, 2𝜋]

h = 0.5; x = 0:h:2*pi;
y = sin(x); m = diff(y)./diff(x);

在这里插入图片描述

十四、Various Step Size

The derivatives of 𝑓(𝑥) = sin(𝑥) calculated using various ℎ values

g = colormap(lines); hold on;
for i=1:4
x = 0:power(10, -i):pi;
y = sin(x); m = diff(y)./diff(x);
plot(x(1:end-1), m, 'Color', g(i,:)); 
end
hold off;
set(gca, 'XLim', [0, pi/2]); set(gca, 'YLim', [0, 1.2]);
set(gca, 'FontSize', 18); 
%set(gca, 'FontName', 'symbol');
set(gca, 'XTick', 0: pi/4: pi/2);
set(gca, 'XTickLabel', {'0', '\pi/4', '\pi/2'});
h = legend('h=0.1','h=0.01','h=0.001','h=0.0001');
set(h,'FontName', 'Times New Roman'); box on;

在这里插入图片描述

Exercise

Given 𝑓(𝑥) = 𝑒 −𝑥 sin(𝑥 2/2), plot the approximate derivatives 𝑓′ of ℎ = 0.1, 0.01, and 0.001

g = colormap(lines); hold on;
for i=1:3
    x = 0:power(10, -i):2*pi;
    y = exp(-x).*sin((x.^2)/2); m = diff(y)./diff(x);
    plot(x(1:end-1), m, 'Color', g(i,:));
end
hold off;
set(gca, 'XLim', [0, 2*pi]); set(gca, 'YLim', [-0.3, 0.3]);
set(gca, 'FontSize', 18);
set(gca, 'XTick', 0: pi/2: 2*pi);
set(gca, 'XTickLabel', {'0', '\pi/2', '\pi', '3\pi/2', '2\pi'});
h = legend('h=0.1','h=0.01','h=0.001');
set(h,'FontName', 'Times New Roman'); box on;

在这里插入图片描述

十五、Second and Third Derivatives

在这里插入图片描述

x = -2:0.005:2; y = x.^3;
m = diff(y)./diff(x);
m2 = diff(m)./diff(x(1:end-1));
plot(x,y,x(1:end-1),m,x(1:end-2),m2);
xlabel('x', 'FontSize', 18); 
ylabel('y', 'FontSize', 18);
legend({'f(x) = x^3','f`(x)','f``(x)'});
set(gca, 'FontSize', 18);

在这里插入图片描述

十六、Numerical Integration 数值积分

在这里插入图片描述

十七、Numerical Quadrature Rules 数值求积规则

在这里插入图片描述

十八、Midpoint Rule

在这里插入图片描述

十九、Midpoint Rule Using sum()

在这里插入图片描述
x(1:end-1) = [x1 x2 x3 x4 … xend-1]
x(2:end) = [x2 x3 x4 x5 … xend]

>> h = 0.05; x = 0:h:2;
midpoint = (x(1:end-1)+x(2:end))./2;
y = 4*midpoint.^3;
s = sum(h*y)
s =
   15.9950

二十、Trapezoid Rule

在这里插入图片描述

二十一、Trapezoid Rule Using trapz()

在这里插入图片描述

>> h = 0.05; x = 0:h:2;
midpoint = (x(1:end-1)+x(2:end))./2;
y = 4*midpoint.^3;
s = sum(h*y)
s =
   15.9950

Alternative:

>> h = 0.05; x = 0:h:2; y = 4*x.^3;
trapezoid = (y(1:end-1)+y(2:end))/2;
s = h*sum(trapezoid)
s =
   16.0100

二十二、Second-order Rule: 1/3 Simpson’s

在这里插入图片描述

二十三、Simpson’s Rule

在这里插入图片描述

>> h = 0.05; x = 0:h:2; y = 4*x.^3;
s = h/3*(y(1)+2*sum(y(3:2:end-2))+...
4*sum(y(2:2:end))+y(end))
s =
    16

二十四、Comparison

在这里插入图片描述

二十五、Review of Function Handles (@)

相当于嵌套函数
A handle is “a pointer to a function”
Can be used to pass functions to other functions
Example:
Pass a function f(x)=sin(x)
to a user-defined function: g(f,…)
f=sin(x)
g(@f,…)

二十六、Function Handles (@) Example

The input of the following function xy_plot is a math function:

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

Try:

xy_plot(@sin,0:0.01:2*pi);

在这里插入图片描述

xy_plot(@cos,0:0.01:2*pi);

在这里插入图片描述

xy_plot(@exp,0:0.01:2*pi);

在这里插入图片描述

二十七、Numerical Integration: integral()

Numerical integration on a function from using global adaptive quadrature and default error tolerances
利用全局自适应正交和默认容错对函数进行数值积分
在这里插入图片描述

>> y = @(x) 1./(x.^3-2*x-5);
integral(y,0,2)
ans =
   -0.4605

二十八、Double and Triple Integrals

在这里插入图片描述

>> f = @(x,y) y.*sin(x)+x.*cos(y);
integral2(f,pi,2*pi,0,pi)
ans =
   -9.8696

在这里插入图片描述

>> f = @(x,y,z) y.*sin(x)+z.*cos(y);
integral3(f,0,pi,0,1,-1,1)
ans =
    2.0000

总结

继续加油吧。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Matlab数值微积分与方程数值求解。数值微积分数值计算方法的一种,是对函数在某一区间内的近似积分。方程数值求解是利用数值计算方法来解决方程,包括线性方程组,非线性方程组和微分方程等。在Matlab中,可以使用一些特定的函数和工具箱来进行数值微积分和方程数值求解。 ### 回答2: MATLAB是一个广泛使用的数学软件,其内置了许多数值微积分和方程数值求解的工具。数值微积分是数学中的一个分支,主要研究如何利用计算机对连续函数进行计算。方程数值求解是数学中的另一个分支,主要研究如何使用计算机求解方程。 对于数值微积分而言,MATLAB提供了许多工具,如积分函数、微分函数、定积分函数等。利用MATLAB的这些工具,可以很方便地计算一些复杂的函数的积分和导数,为科研工作者和工程师提供了非常实用的工具。 对于方程数值求解而言,MATLAB同样提供了许多工具,如求根函数、ODE求解器等。利用MATLAB的这些工具,可以求解一些困难的方程组,同时也可以对一些常微分方程进行数值求解。这些工具非常实用,为科学研究和工程应用提供了非常快捷的解法。 除了上述的工具,MATLAB还提供了一些非常实用的工具箱,如优化工具箱、信号处理工具箱等。这些工具箱可以帮助用户进行更高级的数值计算和分析,同时也可以应用到各种不同的领域,如机器学习、图像处理等。 综上所述,MATLAB是一款非常强大的数学软件,其数值微积分和方程数值求解工具在科研和工程实践中有着广泛的应用。同时,MATLAB还提供了许多工具箱和应用,在许多领域都有着广泛的应用。 ### 回答3: Matlab是一款强大的数学软件工具,其中包括了数值微积分和方程数值求解的功能。数值微积分是指对函数进行数值积分、微分及求导的过程,而方程数值求解则是指求解一般的数学方程,包括线性方程组、非线性方程、微分方程等。在Matlab中,数值微积分与方程数值求解可以方便地通过内置函数或辅助工具箱实现。 首先,数值微积分是我们计算与建模中常常需要用到的过程。Matlab中可以使用诸如quad、dblquad、triplequad、quad2d、quad3d等函数进行积分计算。这些函数使用的是数值积分法,将其所需积分区间分成若干子区间,并对每个子区间进行数值积分,从而最后得到整个区间的积分估算值。此外,Matlab中还有比如diff、gradient等函数可以用于求解函数的微分和梯度,方便地为我们提供了数学分析的工具。 对于方程数值求解,Matlab提供了非常丰富的内置函数和工具箱,包括ODE工具箱、PDE工具箱等。ODE工具箱可用于求解各种常微分方程的初值问题和边值问题,PDE工具箱可用于求解偏微分方程的初值问题和边值问题。同时,Matlab中还有其他非线性方程、线性方程组等常见问题的求解函数,如roots、fsolve、linsolve等等。这些函数除了可以求解方程本身之外,还可以提供方程求解的收敛性、解的稳定性、数值误差等信息,有时还能给出方程的解析解。 总的来说,数值微积分与方程数值求解是Matlab中非常重要的部分,也是工程师和科学家常用的分析方法。Matlab提供了丰富的函数和工具箱,方便我们进行求解,而且也带有较高的准确性和精度。因此,学习Matlab中的数值微积分和方程数值求解不仅是科学和工程计算的必要部分,也是日常分析中必不可少的工具。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值