Matlab求解微分、积分方程

本文为作者学习matlab求解方程笔记
欢迎交流讨论,喜欢的话点个赞吧

欢迎去看我的主页: NicholasYe’s Homepage.


1. 数值微分

符号 含义
diff 差分和近似导数
gradient 数值梯度
del2 离散拉普拉斯算子
  1. diff函数
  • 使用方法:diff(X, n, dim)
    1. X:数列
    2. n:差分次数(阶数)
    3. dim:差分维度
h = 0.001;       % step size
X = -pi:h:pi;    % domain
f = sin(X);      % range
Y = diff(f)/h;   % first derivative
Z = diff(Y)/h;   % second derivative
plot(X(:,1:length(Y)),Y,'r',X,f,'b', X(:,1:length(Z)),Z,'k')
  1. gradient函数
  • 使用方法:[FX,FY,FZ] = gradient(F)
% Calculate the 2-D gradient of xe^(-x*x-y*y) on a grid.
x = -2:.2:2; y = (-1:.15:1).';
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
% Plot the contour lines and vectors in the same figure.
contour(x,y,z), hold on
quiver(x,y,px,py)
  1. del2函数
  • 使用方法:L = del2(U,h)
    1. U:数列
    2. h:标量间距
% This produces 100 evenly spaced points in the range −2π≤x≤2π.
% Create a vector of cosine values in this domain.
x = linspace(-2*pi,2*pi);
% Calculate the Laplacian of U using del2. Use the domain vector x to define the 1-D coordinate of each point in U.
U = cos(x);
% Analytically, the Laplacian of this function is equal to ΔU=−cos(x).
L = 4*del2(U,x);
% Plot the results.
plot(x,U,x,L)
legend('U(x)','L(x)','Location','Best')

2. 数值积分

  1. 一重积分(integral/quadgk)
  • 使用方法:q = integral(fun,xmin,xmax,Name,Value) & q = quadgk(fun,a,b)
    1. fun:函数
    2. xmin/xmax:x最大最小值
    3. Name/Value:指定绝对误差容限、相对误差容限、数组值函数标志、积分路点等
fun = @(z) 1./(2*z-1);
q = integral(fun,0,0,'Waypoints',[1+1i,1-1i])
  1. 二重三重积分(integral2/quad2d/integral3)
  • 使用方法:q = integral2(fun,xmin,xmax,ymin,ymax) & q = quad2d(fun,a,b,c,d)
    1. fun:函数
    2. xmin/xmax:x最大最小值
    3. ymin/ymax:y最大最小值
% Define the function
fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 );
polarfun = @(theta,r) fun(r.*cos(theta),r.*sin(theta)).*r;
% Define a function for the upper limit of r.
rmax = @(theta) 1./(sin(theta) + cos(theta));
% Integrate over the region bounded by 0≤θ≤π/2 and 0≤r≤rmax
q = integral2(polarfun,0,pi/2,0,rmax) 

3. 微分方程解析解求解

  1. dsolve函数
  • 使用方法:dsolve(eqns)
    1. eqns:函数
syms y(t) z(t);
eqns = [diff(y,t)==z, diff(z,t)==-y];
[ySol(t),zSol(t)] = dsolve(eqns);
  1. ODE数值求解
  • The Lotka-Volterra equations
clc, clear all, close all

t0 = 0;
tfinal = 15;
p0 = [50; 50];

[t,p] = ode45(@evolutionODE,[t0 tfinal],p0);
plot(t,p)
title('Predator/Prey Populations Over Time')
xlabel('t')
ylabel('Population')
legend('Prey','Predators')

function dpdt = evolutionODE(t,p)
a = 1;
b = 1;
c = 0.01;
d = 0.02;
dpdt = [p(1) .* (a - c*p(2));
        p(2) .* (-b + d*p(1))];
end

Matlab求解微分、积分方程_第1张图片


请在转载文章过程中明确标注文章出处!尊重原创,尊重知识产权,谢谢!

你可能感兴趣的:(matlab)