Download mar 12

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

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

Document related concepts
no text concepts found
Transcript
MATLAB second order differential equation & system examples
Engineering 240
March 12, 2013
1. Solve the boundary value problem:
d2 y
dx2
−
dy
dx
− 2y = 0 and y(0) = 1 and y 0 (3) = 2
First transform the equation to a system:
(
dy1
dx
dy2
dx
=
y2
= 2y1 + y2
)
with y1 (0) = 1 and y2 (3) = 2
Create a function file for the derivative:
function ydot=yprime(t,y)
ydot=[ y(2) ; 2*y(1) + y(2)];
end
Create a function file for the boundary value condition errors:
function res = bc(ya,yb)
res=[ ya(1)−1 ; yb(2)−2 ]
end
Initialize the x-values with step size 1/10 and provide a guess for the initial y and y 0 values:
solg = bvpinit( linspace(0,3,31) , [ 1 1 ] );
% this is a built-in function which can be called from the
command line
Generate a solution to the boundary value problem:
sol = bvp4c(@yprime, @bc, solg);
Graph the approximate solution y and the approximate solution’s derivative y 0 at the evaluation points with step-size h = 1/10:
xx=sol.x ; yy = sol.y ; yder=sol.yp ;
plot( xx, yy , ‘k’ , xx, yder , ‘g’ )
Experiment with the same differential equation but with different boundary conditions:
• y(0) = 5 and y(3) = −1
• y 0 (0) = 0 and y 0 (3) = 1
2. Find the approximate solution to Lorenz’ equations:
(first used to model atmospheric convection!)
 dx

 dt
dy
dt

 dz
dt
=
σ(y − x)
= x(ρ − z) − y
=
xy − βz





Create a function file:
function ydot=lz(t,y);
sigma=10; rho=28; beta=8/3;
ydot=[ sigma*( y(2)−y(1) ) ; y(1)*(rho−y(3)) − y(2) ; y(1)*y(2)−beta*y(3)];
end
Generate an approximate solution:
[t y] = ode45(‘lz ’ , [0 40], [ 1 ; 0 ; 0 ] ) ;
Plot the parametric solution:
figure(3)
plot3(y(:,1),y(:,2),y(:,3),’k’)
Experiment with the shape of the solution curve by varying the initial conditions and by
varying the values of the parameters sigma, rho, and beta.
3. A few numerical derivatives
For the function y = f (x) = sin(x), with derivative f 0 (x) = cos(x), compare the exact first
derivative yp with the approximate “centered difference quotient” ypa:
x = 0 : .01 : 3 ;
h = .01 ;
y = sin (x) ; yp = cos(x) ;
ypa = 1/(2*h)*(diff(x(1:end-1)) + diff(x(2:end)) ) ;
plot ( x(2:end-1) , ypa , ‘g’, x(2:end-1) , yp(2:end-1) , ‘k’)
second derivatives:
ypp = -sin(x); % exact
yppa = diff(x,2)/h∧2 ; % approximate
plot ( x(2:end-1) , yppa , ‘g’, x(2:end-1) , ypp(2:end-1) , ‘k’)
Related documents