Runge-Kutta method to solve differential equations

Continuous problem, differential equations or partial differential equations must be able to express. Such as the spread of disease, and other news media.

Discrete problem, you can use differential equations or algorithms similar difference.

equation

$ Y '= cos t $

Code

1
2
3
4
5
6
7
8
9
clear,clc;

@ = F (t, Y) COS (t);
tspan = [ 0 , 2 * PI]; % range of time t
yO = 2 ; initial value for the% y, C integrator for processing obtained
[t, y] = ode23 (F, tspan, yO); % Note call format
Plot (T, Y);
the xlabel ( 'T' );
ylabel ( 'Y' );

@He represents the handle, when the function as a parameter to another function, this time must be used handle. Here is the function fto the function ode23.

result

.png first order differential equations

Solving higher order differential equations

equation

$begin{equation}
left{
begin{array}{r1}
y’’=-sin y+sin 5t\
y(0)=1\
y’(0)=0\
end{array}
right.
end{equation}$

可以将该高阶微分方程转化为两个一阶的微分方程:$begin{equation}
left{
begin{array}{r1}
y_1=y\
y_2=y’\
y’_1=y_2\
y’_2=-sin y_1+sin 5t\
y_1(0)=1\
y_2(0)=0\
end{array}
right.
end{equation}$

代码

1
2
3
4
5
6
7
8
9
10
clear,clc;

@ = F (T, Y) [Y ( 2 ); -sin (Y ( . 1 )) + SiN ( . 5 * T)]; % are the two parameters y1 and y2 of the derivative
tspan = [ 0 , 20 is ]; % range of time t
yO = [ . 1 ; 0 ]; % initial value, corresponding to y1, y2 initial value
[t, Y] = ode23 (F, tspan, yO); % Note call format
plot (t, y);
the xlabel ( 'T' );
ylabel ( 'Y' );
Legend ( 'Y1' , 'Y2' );

result

.png higher order differential equations

Author: @ smelly salted fish

This article original author, please indicate the source: https://chouxianyu.github.io

Welcome to discuss and share!

Original: Big Box  Runge-Kutta method to solve differential equations


Guess you like

Origin www.cnblogs.com/petewell/p/11612032.html