15.11.2014 Views

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Initial Value Problems for ODEs and DAEs<br />

In this example, the first call to ode45 uses the defaults for the solver<br />

parameters:<br />

ode = @(t,y) abs(y);<br />

[t0,y0] = ode45(ode,[0, 40], 1);<br />

The second uses options to impose nonnegativity conditions:<br />

options = odeset('NonNegative',1);<br />

[t1,y1] = ode45(ode,[0, 40], 1, options);<br />

This plot compares the numerical solution to the exact solution.<br />

Here is a more complete view <strong>of</strong> the code used to obtain this plot:<br />

ode = @(t,y) abs(y);<br />

options = odeset('Refine',1);<br />

[t0,y0] = ode45(ode,[0, 40], 1,options);<br />

options = odeset(options,'NonNegative',1);<br />

[t1,y1] = ode45(ode,[0, 40], 1, options);<br />

t = linspace(0,40,1000);<br />

y = exp(-t);<br />

plot(t,y,'b-',t0,y0,'ro',t1,y1,'b*');<br />

legend('Exact solution','No constraints','Nonnegativity', ...<br />

'Location','SouthWest')<br />

5-39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!