常/偏微分方程求解

常微分方程

1、四階經典步長Runge-Kutta法求解高階常微分方程
y''=2y^3
y(1)=y'(1)=-1
1<x≤1.5

clear; 
clc;
% 四階經典定步長Runge-Kutta法求解高階常微分方程組
% function
f = @(x,y,z)z;
g = @(x,y,z)2*(y^3);
% 條件
h = 0.01;
len = 0.5/h; 
x(1) = 1; 
y(1) = -1; 
z(1) = -1; 
i = 1; 
k = 0;
while k < len
    K1 = f(x(i),y(i),z(i));
    L1 = g(x(i),y(i),z(i));
    K2 = f(x(i)+h/2,y(i)+h*K1/2,z(i)+h*L1/2);
    L2 = g(x(i)+h/2,y(i)+h*K1/2,z(i)+h*L1/2);
    K3 = f(x(i)+h/2,y(i)+h*K2/2, z(i)+h*L2/2);
    L3 = g(x(i)+h/2,y(i)+h*K2/2, z(i)+h*L2/2);
    K4 = f(x(i)+h,y(i)+h*K3,z(i)+h*L3);
    L4 = g(x(i)+h,y(i)+h*K3,z(i)+h*L3);
    y(i+1) = y(i)+(K1+2*K2+2*K3+K4)*h/6;
    z(i+1) = z(i)+(L1+2*L2+2*L3+L4)*h/6;
    x(i+1) = x(i) + h;
    i = i + 1;
    k = k + 1;
end
plot(x, y, '-o');
title('第一題圖');
y = y';
z = z';

2、用ode15s函數、ode23s函數和ode23tb函數求剛性方程并畫圖比較:
y''-1000(1-y^2)y'+y=0
y(0)=2,y'(0)=0
https://ww2.mathworks.cn/help/matlab/ref/ode15s.html

function dydt = vdp1000(t,y)
%VDP1000  Evaluate the van der Pol ODEs for mu = 1000.
%
%   See also ODE15S, ODE23S, ODE23T, ODE23TB.

%   Jacek Kierzenka and Lawrence F. Shampine
%   Copyright 1984-2014 The MathWorks, Inc.

dydt = [y(2); 1000*(1-y(1)^2)*y(2)-y(1)];

使用 ode15s 求解器對剛性方程組求解,然后繪制解 y 的第一列對時間點 t 的圖。ode15s 求解器通過剛性區域所需的步數遠遠少于 ode45。

%% ode15s
[t1,y1] = ode15s(@vdp1000,[0 3000],[2 0]);
figure(1)
plot(t1,y1(:,1),'-o')
title('ode15s作圖')
%% ode23s
[t2,y2] = ode23s(@vdp1000,[0 3000],[2 0]);
figure(2)
plot(t2,y2(:,1),'-*')
title('ode23s作圖')
%% ode23tb
[t3,y3] = ode23tb(@vdp1000,[0 3000],[2 0]);
figure(3)
plot(t3,y3(:,1),'-.')
title('ode23tb作圖')
ode15s.jpg
ode23s.jpg
ode23tb.jpg

3、求解微分方程組


第三題圖
y0 = [0, 0, 0.68, 1, -0.5];
[t, y] = ode45(@fun8_3, [-10, 10], y0);
% plot(y(:,4),y(:,1));
plot (t,y);
title('第三題');
% 微分方程組
function f = fun8_3(t,y)
f = [y(2);
    y(3);
    -3*y(1)*y(3)+2*(y(2))^2 - y(4);
    y(5);
    -2.1*y(1)*y(5);];
 end
  1. 已知Appolo衛星的運動軌跡(x,y)滿足下面的方程:


    方程組.jpg

    其中
    條件1.jpg

    試在初值條件為
    初值條件.jpg

    的情況下求解,并繪制衛星軌跡圖。

解:
函數appollo,初始化方程組,設定參數

function dx = appollo(t,x)
% mui = μ
% mustar = λ
mui = 1/82;
mustar = 1 - mui;
% 條件r1,r2 
% x(1) = x; 
% x(3) = y;
r1 = sqrt((x(1) + mui)^2 + x(3)^2);
r2 = sqrt((x(1) - mustar)^2 + x(3)^2);
% 方程組
% dy/dt = x(4)
% d^2y/dt^2 = x(2)
dx = [x(2)
2*x(4) + x(1) - mustar*(x(1)+mui)/r1^3 - mui*(x(1) - mustar)/r2^3
x(4)
-2*x(2) + x(3) - mustar*x(3)/r1^3 - mui*x(3)/r2^3];

主函數

clear;
clc;
% test 4
% x0(i)對應與xi的初值
% x(0) = 1.2;
% x'(0) = 0;
% y(0) = 0;
% y'(0) = -1.04935371
x0=[1.2;0;0;-1.04935371];
options=odeset('reltol',1e-8);
% 該命令的另一種寫法是options=odeset;options.reltol=1e-8;
% 是解微分方程時的選項設置,'RelTol',1e-4,是相對誤差設置
% 'AbsTol',[1e-4 1e-4 1e-5]是絕對誤差設置
tic
[t,y]=ode45(@appollo,[0,20],x0,options);
%t是時間點,y的第i列對應xi的值,t和y的行數相同
toc
plot(y(:,1),y(:,3))
%繪制x1和x3,也就是x和y的圖形
title('Appollo衛星運動軌跡')
xlabel('X')
ylabel('Y')

運行結果:

>> test4
時間已過 0.039310 秒。
appollo.jpg

偏微分方程求解工具的學習

圖片1.jpg
圖片2.jpg
圖片3.jpg
圖片4.jpg
圖片5.jpg
圖片6.jpg
圖片7.png
圖片8.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容