230 likes | 336 Views
Learn how to find solutions to differential equations, from first to nonlinear order, using Matlab commands like dsolve and ODE45 for numerical solutions. Verify results and handle initial conditions.
E N D
Math Review with Matlab: Differential Equations Finding Solutions to Differential Equations S. Awad, Ph.D. M. Corless, M.S.E.E. D. Cinpinski E.C.E. Department University of Michigan-Dearborn
Finding Solutions to Differential Equations • Solving a First Order Differential Equation • Solving a Second Order Differential Equation • Solving Simultaneous Differential Equations • Solving Nonlinear Differential Equations • Numerical Solution of a Differential Equation • Using the ODE45 Solver
Solving a 1st Order DE • The Matlab command used to solve differential equations is dsolve • Consider the differential equation: • The general solution is given by: • Verify the solution using dsolve command
Solving a DifferentialEquation in Matlab » syms y t » ys=dsolve('Dy+2*y=12') ys = 6+exp(-2*t)*C1 • C1 is a constant which is specified by way of the initial condition • Dy means dy/dt and D2y means d2y/dt2 etc
Verify Results • Verify results given y(0) = 9 » ys=dsolve('Dy+2*y=12','y(0)=9') ys = 6+3*exp(-2*t)
Solving a 2nd Order DE » syms c y » ys=dsolve('D2y=-c^2*y') ys = C1*sin(c*t)+C2*cos(c*t) • Find the general solution of:
Solving SimultaneousDifferential Equations Example • Solve the following set of differential equations: • Syntax for solving simultaneous differential equations is: dsolve('equ1', 'equ2',…)
General Solution • Given the equations: • The general solution is given by:
Matlab Verification • Given the equations: • General solution is: » syms x y t » [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y') x = exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2) y = -exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
Initial Conditions • Solve the previous system with the initial conditions: » [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y', 'y(0)=1','x(0)=0') x = exp(3*t)*sin(4*t) y = exp(3*t)*cos(4*t)
Non-Linear Differential Equation Example • Solve the differential equation: • Subject to initial condition: » syms y t » y=dsolve('Dy=4-y^2','y(0)=1') » y=simplify(y) y = 2*(3*exp(4*t)-1)/(1+3*exp(4*t))
Specifying the Independent Parameter of a Differential Equation • If another independent variable, other than t, is used, it must be introduced in the dsolve command • Solve the differential equation: » y=dsolve('Dy+2*y=12','x') y = 6+exp(-2*x)*C1
Numerical Solution Example • Not all non-linear differential equations have a closed form solution, but a numerical solution can be found • Solve the differential equation: • Subject to initial conditions: • No closed form solution exists • Use the ode45 command to get a numerical solution
Rewrite Differential Equation • Rewrite in the following form
Create a New Function • Create a Matlab function evalxdot to evaluate and numerically in terms of x1 and x2. function xdot=evalxdot(t,x) %x=[x1, x2] %xdot=[dx1/dt, dx2/dt]; xdot=[x(2); -9*sin(x(1))];
ODE45 • ODE45 is used to solve non-stiff differential equations • [T,Y] = ODE45('F',TSPAN,Y0) T = Time vector Y = Output corresponding to time vector F = Function name TSPAN = Simulation duration Y0 = Initial conditions • If the left hand side [T,Y]of the output arguments is omitted,Matlab solves it and plots it
Returning t, y and dy/dt • Run the solver with the input and output arguments specified » [t,y]=ode45('evalxdot',10,[1 0]); » plot(t,y) » xlabel('Time (sec)'); » ylabel('Amplitude'); » title('Numerical Solution'); » legend('Y','dY/dt')
Omit Output Arguments • We can run the solver again without output arguments • Omitting the output arguments causes Matlab to plot the results » ode45('evalxdot',10,[1 0]); » xlabel('Time (sec)'); » ylabel('Amplitude'); » title('Numerical Solution'); » legend('Y','dY/dt')
Summary • The symbolic toolbox can be used to find the closed form solutions for differential equations where they exist • The symbolic toolbox can be simultaneously solve a system of differential equations • Other Matlab commands can be used to numerically solve systems of differential equations if closed forms do not exist