本文为作者学习matlab求解方程笔记
欢迎交流讨论,喜欢的话点个赞吧
欢迎去看我的主页
: NicholasYe’s Homepage.
符号 | 含义 |
---|---|
diff | 差分和近似导数 |
gradient | 数值梯度 |
del2 | 离散拉普拉斯算子 |
diff
函数diff(X, n, 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')
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)
del2
函数L = del2(U,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')
integral/quadgk
)q = integral(fun,xmin,xmax,Name,Value)
& q = quadgk(fun,a,b)
fun = @(z) 1./(2*z-1);
q = integral(fun,0,0,'Waypoints',[1+1i,1-1i])
integral2/quad2d/integral3
)q = integral2(fun,xmin,xmax,ymin,ymax)
& q = quad2d(fun,a,b,c,d)
% 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)
dsolve
函数dsolve(eqns)
syms y(t) z(t);
eqns = [diff(y,t)==z, diff(z,t)==-y];
[ySol(t),zSol(t)] = dsolve(eqns);
ODE
数值求解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
请在转载文章过程中明确标注文章出处!尊重原创,尊重知识产权,谢谢!