Matlab 版 (精华区)
发信人: candle ( 马 走 日), 信区: Matlab
标 题: How do I solve a time dependent differential equa
发信站: 紫 丁 香 (Fri Dec 24 17:45:02 1999), 转信
1505
How Do I Solve a Time Dependent
Differential Equation?
Revison: 1.0
Last Date Modified: 17-August-1999
This is an example of an ordinary differential equation that has a time
dependent term using ode23/ode45. The time dependent term can be
defined by either a data set with known sample times or as a simple
function. If the time dependent term is defined by a data set, the data set
and its sample times are passed into the function called by ode23/ode45
as global variables. If the time dependent term is defined by a function,
then that function is called in the derivative function as needed.
The differential equation used in this example is the Damped Wave
Equation with a sinusoidal driving term.
y''(t) - beta * y'(t) + omega^2 * y(t) = A * sin(w0 * t - theta)
MATLAB requires the differential equation be expressed as a first order
differential equation using the following form:
y'(t) = A * y(t)
where y is a column vector of states and A is a matrix. The MATLAB
definition of this differential equation is:
xdot(2) = beta * x(2) - omega^2 * x(1) + ...
A * sin(w) * t - theta)
xdot(1) = x(2)
where
xdot = dx/dt
x(1) = y
x(2) = dy/dt.
In this example beta, omega, A, w0, and theta need to be defined. They
are declared global and defined before ode23/ode45 is called. These
variables are passed to the derivative function by declaring them global
in the derivative function also. In this example the time dependent term is
a sine wave so it can be called from within the function.
Example 1: Time dependent term is a function
Create the following derivative function:
% FUN1.M: Time dependent ODE example.
function xdot = fun1(t,x)
global beta omega A w0 theta
% The time dependent term is A * sin(w0 * t - theta)
xdot(2)= -beta*x(2) + omega^2 * x(1) + A * sin(w0 * t - theta);
xdot(1) = x(2);
xdot=xdot(:); % To make xdot a column
% End of FUN1.M
To call this function in MATLAB use:
global beta omega A w0 theta
beta = .1;
omega = 2;
A = .1;
w0 = 1.3;
theta = pi/4;
X0 = [0 1]';
t0 = 0;
tf = 20;
[t,y]=ode23('fun1',[t0,tf],X0);
plot(t,y)
Example 2: The time dependent term is defined by a data set.
This requires more global variables and the interp1 command.
Create the following derivative function:
% FUN2.M Time dependent ODE example with data set.
function xdot = fun2(t,x)
global beta omega
global P T % Additional global variables
pt=interp1(T,P,t);
xdot(2) = beta*x(2)-omega^2*x(1)+pt;
xdot(1) = x(2);
xdot=xdot(:); % To make xdot a column
% End of FUN2.M
To call this function in MATLAB use:
global beta omega T P
beta = .1;
omega = 2;
A = .1;
w0 = 1.3;
theta = pi/4;
X0 = [0 1]';
t0 = 0;
tf = 20;
T=t0-eps:.1:tf+theta+eps;
P=A.*sin(w0.*T-theta);
[t,y]=ode23('fun2',[t0,tf],X0);
plot(t,y)
The calls to interp1 may cause the second example to be slower than
the first.
--
---------------------------------------------------
I BELIEVE I CAN FLY. I BELIEVE I CAN TOUCH THE SKY.
------------------- CANDLE --------------------
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: 150.59.34.186]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:5.741毫秒