MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Summation Problem w/ 5.0

  • To: mathgroup at
  • Subject: [mg43840] Re: Summation Problem w/ 5.0
  • From: Bill Rowe <browe51 at>
  • Date: Wed, 8 Oct 2003 04:47:58 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

On 10/7/03 at 2:40 AM, andrea.knorr at (Andrea) wrote:

> I posted code Saturday morning, but it has not been added, so I will
> try again.  In my program I have created three lists, "virus",
> "uninfected", and "infected" which are concentrations at a particular
> time.  "vtime", "xtime", and "ytime" are the time points that
> correspond to the given concentration.  The function "titer" solves
> three differential equations that model a viral system.  minfcn is a
> part of nonlinear regression.  The problem I am having occurs when I
> evaluate minfcn, using the parameters published in a journal article,
> at values above a particular element of "virus", "uninfected", and
> "infected".  Here's the code:

> titer[lambda_, beta_, d_, k_, a_, u_] :=
>     Module[{soln, x, y, v, valueslist, xeval, yeval, veval}, 
>       soln = NDSolve[{
>             x'[t] == lambda - d x[t] - beta x[t] v[t],
>             y'[t] == beta x[t] v[t] - a y[t],
>             v'[t] == k y[t] - u v[t], 
>             x[0] == 10^9, y[0] == 0, v[0] == 10^9},
>           {x, y, v}, {t, 0, 2500}];
>       xtiter = Evaluate[x[xtime] /. soln];
>       ytiter = Evaluate[y[ytime] /. soln];
>       vtiter = Evaluate[v[vtime] /. soln];

This block here doesn't make sense. NDSolve returns an interpolating fuction not a rule. So, soln is a list of three interpolating fuctions. If you want to evaluate say the first of these three functions at some point the syntax would be soln[[1]][xtime] where xtime is a real number between 0 and 2500.
>       values
>       values = Table[Flatten[{xtiter, ytiter, vtiter}, 1]];

You don't need to save the results to a local variable like you've done here. Replacing these two lines of code with

Table[Flatten[{xtiter, ytiter, vtiter}, 1]]

will result in exactly the same output. Note there is no ";" at the end of this line.

>       ];              

> calcs = titer[10^7, 5*10^-10, 0.1, 500, 0.5, 5];

> minfcn[lambda_, beta_, d_, k_, a_, u_] :=
>   Module[{i, j},

>     diffsum = 
>       Sum[10000*(uninfected[[i, 2]] - calcs[[1, i]])^2 + 
>             10000*(infected[[i, 2]] - calcs[[2, i]])^2, {i, 
>             Length[uninfected]}] + 
>         Sum[(virus[[j, 2]] - calcs[[3, j]])^2, {j, Length[virus]}];
>     diffsum
>     ]

The code for this function is not written well. None of the arguements you are passing to it are used in the function. The variables that are used are not defined in the function. If this works at all for you, it is because you've defined calcs, infected, unifected and virus elsewhere in your notebook as global variables. It is definitely not a good idea to make your functions dependent on the values of global variables.

If unifected, calcs, infected and virus were lists with the appropriate dimensions then

Tr[10000 (uninfected[[All,{2}]] - calcs[[1]])^2 + 10000 (infected[[all,{2}]]+ calcs[[2]])^2] +

would give you the sum you appear to want.

To sum lists, it isn't necessary to have an iterator to select each element of the list. If the list is 1 dimensionsal, either 
Plus@@list or Tr@list will sum all of the elements of the list. My experience indicates Tr@list is faster than Plus@@list. And I would expect both to be faster than Sum[list[[n]], {n, Length@list}]

> minfcn[10^7, 5*10^-10, 0.1, 500, 0.5, 5]

> Here is the output:
> (2.518473817923033*10^24 + 
>     10000*(-1.80000521715242*10^7 + 20000)^2 + 
>     10000*(-1.8*10^7 + 20000)^2 + 
>     10000*(-1.*10^7 + 980000)^2 + 
>     10000*(-9.99926182857273*10^6 + 980000)^2)

I cannot duplicate this output nor will anyone else reading the code you posted be able to duplicate this output. The output of minfcn depends on the values of virus, infected and uninfected which are not defined by the code you posted.
To reply via email subtract one hundred and nine

  • Prev by Date: Re: Newbie: plot data from a text file?
  • Next by Date: Re: poisson equation
  • Previous by thread: Re: Re: Summation Problem w/ 5.0
  • Next by thread: Re: Summation Problem w/ 5.0