Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dsolve vs. ode45

669 views
Skip to first unread message

Michael Serban

unread,
Feb 28, 1999, 3:00:00 AM2/28/99
to
Hello everybody,
I have this system: dx1/dt = -8*x1-5*x2
dx2/dt = -5*x1-8*x2
which I solved using dsolve and I plotted the functions.
{This is the solution :
x1 = -1/2*exp(-3*t)+1/2*exp(-13*t)
x2 = 1/2*exp(-13*t)+1/2*exp(-3*t)
t = [0 2]}
Next, I solved the same system with ode45 and plotted the graphs.
Next I wanted to plot the difference, dsolve-ode45, and here the
problem put me down.
I got a message telling that convertion to double from ssym is not
possible. I forgot to tell you that I used
a tspan=[0 2] in ode45, and I tried to use the same same pitch in order
to get the value for the functions calculated analytically.
I am interested in graphing the difference x1(obtained with dsolve)
-x1(obtained with ode45) and the same for x2. I am looking for the
error between an analytically solved and numerically solved functions.
I tried to create an array with all the values of t used by the dsolve,
however I cannot create the array over 58 elements(I'm using Matlab5
student version) and all in all I have 169 values for t.
Is there a way to do it?
TIA,
Michael

Cleve Moler

unread,
Mar 1, 1999, 3:00:00 AM3/1/99
to

Hi.

Try this. (This is the first time I've used "inline" with a
vector-valued function.)

F = inline('-[8 5; 5 8]*x','t','x')
[t,x] = ode45(F,[0 2],[0; 1]);
[x1,x2] = dsolve('Dx1 = -8*x1-5*x2', 'Dx2 = -5*x1-8*x2', 'x1(0)=0', 'x2(0)=1')
exact = [subs(x1,t) subs(x2,t)];
plot(t,x-exact)

The idea is to have ode45 determine the values of t it likes and then
substitute those values into the analytic solution obtained with dsolve.
The graph shows that, with ode45's default tolerances, the maximum error
is about 3e-5.

Nice example. Thanks.

-- Cleve Moler
mo...@mathworks.com

Michael Serban

unread,
Mar 2, 1999, 3:00:00 AM3/2/99
to
Thank you for your time and suggestions.
I tried. For a tspan =[0 2] I 'm surpassing the limits of the student version.
Restricting t=[0 .5], I'm getting the messages: "Error using ==>plot. Ambiguous line
property: 's' " and " plot (t,x-exact).
I'll try some more, thank you anyway.
Regards, Michael
0 new messages