Download Lecture 11

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Cubic function wikipedia , lookup

Quadratic equation wikipedia , lookup

Newton's method wikipedia , lookup

Mathematical optimization wikipedia , lookup

Quartic function wikipedia , lookup

Interval finite element wikipedia , lookup

Weber problem wikipedia , lookup

False position method wikipedia , lookup

System of polynomial equations wikipedia , lookup

Calculus of variations wikipedia , lookup

System of linear equations wikipedia , lookup

Transcript
Lecture 11
Computer Application
.Solution of ordinary differential equation
An ordinary differential equation (ODE) is an equation that contains an
independent variable, a dependent variable, and derivatives of the dependent
variable. The equations that are considered here are of first order with the form:
dy
 f  x, y 
dx
Where x and y are the independent and dependent variables, respectively. A
solution is a function y = f(x) that satisfies the equation.
Numerical solution of O.D.E
MATLAB has a large library of tools that can be used for solving differential
equations. To fully utilize the power of MATLAB, however, requires that the user
have knowledge of differential equations and the various numerical methods that
can be used for solving them.
Steps for solving a single first order ODE:
Step1 Write the equation in the form:
Step2Create a function file that calculates ( dy /dx) for given values of t and y.
Step3 Select the numerical method that you would like MATLAB to use in the
solution.
The following table lists seven ODE solver commands, which are MATLAB builtin functions that can be used for solving a first order ODE. A short description of
each solver is included in the table.
Page 1 of 8
Lecture 11
Computer Application
Step 4: Solve the ODE.
The form of the command that is used to solve an initial value ODE problem is the
same for all the solvers and all the equations that are solved. The form is:
[ t, y]
solver_name
'ODEfun'
is the output,
which is the
solution of the
ODE. t and y are
column vectors.
The first and the
last points are
the beginning
and end points of
the interval.
is the name of
the solver
(numerical
method) that is
used (e.g. ode45
or ode23s)
is the name,
entered as a
string, of the'
user-defined
function(function file)
that calculates dy
for given values
of t and y.
tspan
is a vector that
specifies the
interval of the
solution. The
vector must
have at least
two elements,
but can have
more.
y0
is the initial
value of y (the
value of y at the
first point of
the interval.)
Page 2 of 8
Lecture 11
Computer Application
Example:
Find the solution of the following differential equation:
Solution:
Step1 Write the equation in the form :
Step2Create a function file that calculates ( dy /dx) for given values oft andy.
Step3 Select the numerical method that you would like MATLAB to use in the
solution.
Page 3 of 8
Lecture 11
Computer Application
To show the solution, the problem is solved again below using tspan with smaller
spacing, and the solution is plotted with the plot command.
The plot that is created is:
Page 4 of 8
Lecture 11
Computer Application
H.w.
Page 5 of 8
Lecture 11
Computer Application
Exact solution of O.D.E
a) First Order Differential Equations
MATLAB can solve linear ordinary differential equations with or without
initial/boundary conditions. Do not expect MATLAB can solve nonlinear ordinary
differential equations which typically have no analytical solutions. Higher
derivatives can be handled as well. The command for finding the symbolic solution
of differential equations is dsolve. For that command, the derivative of the function
y is represented by Dy. For example, suppose that we want to find the solution of
the equation x y' - y = 1. We will have:
>> dsolve('x*Dy-y=1', 'x')
ans =
-1+x*C1
This means that the solution is any function of the form y = -1+ cx, where c is
any constant. The letter “D” has a special meaning and cannot be used otherwise
inside dsolve. It means “first derivative of”. The C1 is a constant of integration.
If we have the initial condition y(1) = 5, we can get the particular solution on
the following way:
>> dsolve('x*Dy-y=1', 'y(1)=5', 'x')
ans =
-1+6*x
>>dsolve('Dy+y=cos(t)')
ans =
1/2*cos(t)+1/2*sin(t)+exp(-t)*C1
Page 6 of 8
Lecture 11
Computer Application
>> dsolve('Dy = x+y', 'y(0)=c', 'x')
ans=
-x-1+exp(x)*(1+c)
>> dsolve('Dy+2*y=12*t^2')
ans =
6*t^2-6*t+3+exp(-2*t)*C1
b) Second Order Equations
The second order linear equations can be solved similarly as the first order
differential equations by using dsolve. For the command dsolve, the second
derivative of y is represented with D2y. The letters “D2” mean second derivative.
For example, the command for solving y''-3y'+2y = sin x.
>> dsolve('D2y-3*Dy+2*y=sin(x)', 'x')
ans =
3/10*cos(x)+1/10*sin(x)+C1*exp(x)+C2*exp(2*x)
If we have the initial conditions y(0) = 1., y'(0)=-1., we would have:
>> dsolve('D2y-3*Dy+2*y=sin(x)', 'y(0)=1', 'Dy(0)=-1', 'x')
ans =
3/10*cos(x)+1/10*sin(x)+5/2*exp(x)-9/5*exp(2*x)
>>dsolve('D2y+y=1','y(0)=0','y(1)=1')
ans =
Page 7 of 8
Lecture 11
Computer Application
1+cos(1)/sin(1)*sin(t)-cos(t)
Example: d2y/dx2 -2dy/dx -3y=x2
>> dsolve('D2y - 2*Dy - 3*y=x^2', 'x')
ans =
-14/27+4/9*x-1/3*x^2+C1*exp(3*x)+C2*exp(-x)
Example: d2y/dx2 -2dy/dx -3y=x2, with y(0)=0, and dy/dx =1 at x=1
>> dsolve('D2y - 2*Dy - 3*y=x^2','y(0)=0, Dy(1)=1','x')
ans =
-1/3*x^2+4/9*x-14/27+1/9*(-11+14*exp(3))/(3*exp(3)+exp(-1))*exp(-x)
+1/27*(33+1 4*exp(-1))/(3*exp(3)+exp(-1))*exp(3*x)
Example: d2y/dt2+y=4cos(t), y(pi/2)=2pi, dy/dt=-3 at t=pi/2
>> y=dsolve('D2y+y=4*cos(t)' , 'y(pi/2)=2*pi, Dy(pi/2)=-3')
y=
-2*sin(t)^2*cos(t)+(2*sin(t)*cos(t)+2*t)*sin(t)+5*cos(t)+pi*sin(t)
c) Higher Order Differential Equations
Similarity you can use the same way to solve the higher order differential
equations.
Page 8 of 8