Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: diff eq
Replies: 8   Last Post: Apr 11, 2013 2:27 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bart

Posts: 3
Registered: 4/10/13
Re: diff eq
Posted: Apr 10, 2013 11:55 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Torsten" wrote in message <kk3s9b$5b5$1@newscl01ah.mathworks.com>...
> "richard " <richard7893@embarqmail.com> wrote in message <kk3r1m$1c2$1@newscl01ah.mathworks.com>...
> > Hi again
> >
> > So I used the link you posted along with some other info on the site and my code ran this time, and I think its right. This is what my code looks like so far:
> >
> > function question1_a
> > [t,a] = ode45(@eq1, [0:.1:10], [1;0;0;0])
> > end
> > function da = eq1(t,a)
> > da = zeros(4,1);
> > da(1) = -2.4712 * a(1) + 0.005 * a(2) + 0.56 * a(3) + 0.1 * a(4);
> > da(2) = 0.385 * a(1) - 0.1002 * a(2);
> > da(3) = 0.002 * a(2) - 0.6462 * a(3);
> > da(4) = 0.007 * a(2) - 0.2042 * a(4);
> > end
> >
> > What comes out is
> >
> > 1) column vector for t from 0 -->10 days in steps of 0.1 days
> >
> > 2) and what comes out for 'a' is a 4 column matrix with the activities ('a') corresponding to the times provided, ie a(1) at t=0, a(2) at t=0, a(3) at t=0, and a(4) at t=0 and so on for the times up to 10 days. There are many rows in this matrix but only 4 columns
> >
> > Now I am trying to plot the results of the "a's" up to the 10 days (a vs t) and this is what I wrote out
> >
> > plot(t,a(:,1),'-',t,a(:,2),'-.',t,a(:,3),'.',t,a(:,4),'o')
> >
> > and this error message comes up:
> > ??? Undefined function or variable "a".
> >
> > Error in ==> question1_a>eq1 at 6
> > da(1) = -2.4712 * a(1) + 0.005 * a(2) + 0.56 * a(3) + 0.1 * a(4);
> >
> > Error in ==> odearguments at 110
> > f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
> >
> > Error in ==> ode45 at 173
> > [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
> >
> > Error in ==> question1_a at 2
> > [t,A] = ode45(@eq1, [0:.1:10], [1;0;0;0])
> >
> > The line I write for the plot is basically following the example 1 from the link you provided. However when I delete the plot line from my code, the program runs. Can you see anything wrong with what I wrote for the plot line?
> >
> > Also what is the purpose of the 'da = zeros(4,1)' line on my code? What does it do because if I leave it out I get error messages.
> >
> > Thanks again
> > "Torsten" wrote in message <kk3eo9$msg$1@newscl01ah.mathworks.com>...

> > > "richard " <richard7893@embarqmail.com> wrote in message <kk3e55$lbv$1@newscl01ah.mathworks.com>...
> > > > Hello
> > > >
> > > > I am doing as you posted and calling ode45 from a different mfile. In my code I am really using a's instead of x's, does that cause a problem?
> > > >
> > > > So when I run the first file which is this:
> > > > function da = system_ex(t,a)
> > > > da(1) = -2.4712*a(1) + 0.005*a(2) + 0.56*a(3)+ 0.1*a(4);
> > > > da(2) = 0.385*a(1) - 0.1002*a(2)
> > > > da(3) = 0.002*a(2) - 0.6462*a(3)
> > > > da(4) = 0.007*a(2) - 0.2042*a(4)
> > > > It is still saying input argument 'a' is undefined.
> > > >
> > > > but the a(t) is what I am trying to find. In a way I think I'm defining 'a' with this file, what's wrong with this part?
> > > >
> > > > and for my second mfile, when I am calling ode45 it wont run probably due to 'a' function being undefined
> > > > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kjtnkm$ro7$1@newscl01ah.mathworks.com>...

> > > > > "richard " <richard7893@embarqmail.com> wrote in message <kjrsih$mgb$1@newscl01ah.mathworks.com>...
> > > > >

> > > > > >
> > > > > > this is what I am typing into MATLAB:
> > > > > > function dx = system_ex(t,x)
> > > > > > dx = zeros(4,1);
> > > > > > dx(1) = -2.4712*x(1) + 0.005*x(2) + 0.56*x(3)+ 0.1*x(4);
> > > > > > dx(2) = 0.385*x(1) - 0.1002*x(2)
> > > > > > dx(3) = 0.002*x(2) - 0.6462*x(3)
> > > > > > dx(4) = 0.007*a(2) - 0.2042*x(4)
> > > > > > [T,X] = ode45(@systems_ex,[0 10],[1 0 0 0])
> > > > > > plot(t,x(:,1))
> > > > > >

> > > > >
> > > > > I hope you save system_ex in an mfile and call ode45 on another mfile. You seem to put everything in the same mfile.
> > > > >
> > > > > Bruno

> > >
> > > Start from example 1 under
> > > http://www.mathworks.de/de/help/matlab/ref/ode23.html
> > > and see whether it works.
> > > Then replace the relevant parts step by step.
> > >
> > > Best wishes
> > > Torsten.

>
> According to the error message, you use
> [t,A] = ode45(@eq1, [0:.1:10], [1;0;0;0])
> as the call to ode45, but you want to plot with
> plot(t,a(:,1),'-',t,a(:,2),'-.',t,a(:,3),'.',t,a(:,4),'o')
> Because MATLAB differs between small and capital letters, the error message
> ??? Undefined function or variable "a"
> makes sense.
>
> By the way:
> You should call ODE45 with a row vector of inital values, i.e.
> [t,a] = ode45(@eq1, [0:.1:10], [1 0 0 0])
>
> Best wishes
> Torsten.


Are you sure that the initial conditions need to be a row vector?
This seems to work for row and column vectors:

A = [-2.4712 0.005 0.56 0.1; 0.385 -0.1002 0 0; 0 0.002 -0.6462 0; 0 0.007 0 -0.2042]
tstDV = @(t,x) A*x;
x0 = [1;2;3;4]
tspan = [0 10]
[t,y] = ode45(tstDV, tspan,x0);
plot(t,y)

Gives the same as
[t2,y2] = ode45(tstDV, tspan,x0');
plot(t2,y2)


Date Subject Author
4/7/13
Read diff eq
Guest
4/8/13
Read Re: diff eq
Bruno Luong
4/10/13
Read Re: diff eq
Guest
4/10/13
Read Re: diff eq
Torsten
4/10/13
Read Re: diff eq
Guest
4/10/13
Read Re: diff eq
Torsten
4/10/13
Read Re: diff eq
Bart
4/11/13
Read Re: diff eq
Torsten
4/10/13
Read Re: diff eq
richard

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.