From: Magbenji on
Hello, I'm having a problem with plotting and I'm not sure what the
cause of it is. What is going on???

Here is what I'm doing:

(*A simple system of ODEs*)

dS[t_]:= -b/P*S[t]*Inf[t]
dInf[t_]:=b/P*S[t]*Inf[t]-g*Inf[t]
dR[t_]:=g*Inf[t]

(*A function to solve the system and return the value of R[100000]*)

ar[r_,s_,pop_]:=Module[{parms,solution},
parms={P->pop,b->r,g->s};
solution=
NDSolve[{S'[t]==dS[t],Inf'[t]==dInf[t],R'[t]==dR[t],S[0]==P-1,Inf[0]==1,R[0]==0}/.parms,
{S,Inf,R},{t,0,100000}];
(R[x]/pop /.solution /. x->100000)[[1]]
]

(*Use ListPlot to look at values; works correctly*)

ListPlot[Table[{i,ar[0.1,i,10000]},{i,0,0.1,0.001}],PlotLabel-
>"CORRECT"]

(*Now try it using Plot; WRONG PLOT*)

Plot[ar[0.1,x,10000],{x,0,0.1}, PlotLabel->"INCORRECT"]

From: Bob Hanlon on

You used x inside the definition of ar[ ] without localizing it to the Module. This conflicts with the x used in the Plot.


Bob Hanlon

---- Magbenji <ben.ridenhour(a)gmail.com> wrote:

=============
Hello, I'm having a problem with plotting and I'm not sure what the
cause of it is. What is going on???

Here is what I'm doing:

(*A simple system of ODEs*)

dS[t_]:= -b/P*S[t]*Inf[t]
dInf[t_]:=b/P*S[t]*Inf[t]-g*Inf[t]
dR[t_]:=g*Inf[t]

(*A function to solve the system and return the value of R[100000]*)

ar[r_,s_,pop_]:=Module[{parms,solution},
parms={P->pop,b->r,g->s};
solution=
NDSolve[{S'[t]==dS[t],Inf'[t]==dInf[t],R'[t]==dR[t],S[0]==P-1,Inf[0]==1,R[0]==0}/.parms,
{S,Inf,R},{t,0,100000}];
(R[x]/pop /.solution /. x->100000)[[1]]
]

(*Use ListPlot to look at values; works correctly*)

ListPlot[Table[{i,ar[0.1,i,10000]},{i,0,0.1,0.001}],PlotLabel-
>"CORRECT"]

(*Now try it using Plot; WRONG PLOT*)

Plot[ar[0.1,x,10000],{x,0,0.1}, PlotLabel->"INCORRECT"]


--

Bob Hanlon


From: dh on
Hi,
this has nothing to do with Plot. It is a matter of localization.
In the ar modul you are using R[x] without localizing x. Therefore, the
global x is used that already has a value different from what you want.
Daniel


On 13.03.2010 13:56, Magbenji wrote:
> dS[t_]:= -b/P*S[t]*Inf[t]
> dInf[t_]:=b/P*S[t]*Inf[t]-g*Inf[t]
> dR[t_]:=g*Inf[t]
>
> (*A function to solve the system and return the value of R[100000]*)
>
> ar[r_,s_,pop_]:=Module[{parms,solution},
> parms={P->pop,b->r,g->s};
> solution=
> NDSolve[{S'[t]==dS[t],Inf'[t]==dInf[t],R'[t]==dR[t],S[0]==P-1,Inf[0]==1,R[0]==0}/.parms,
> {S,Inf,R},{t,0,100000}];
> (R[x]/pop /.solution /. x->100000)[[1]]
> ]
>
> (*Use ListPlot to look at values; works correctly*)
>
> ListPlot[Table[{i,ar[0.1,i,10000]},{i,0,0.1,0.001}],PlotLabel-
>> >"CORRECT"]
> (*Now try it using Plot; WRONG PLOT*)
>
> Plot[ar[0.1,x,10000],{x,0,0.1}, PlotLabel->"INCORRECT"]
>


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh(a)metrohm.com>
Internet:<http://www.metrohm.com>