2
$\begingroup$

Can anyone tell me why do I have errors when I try the code below ?

cd[t] = t*e^(t/2);
s0 = 10; u = 0.4; w = 0.9; v = 0.01; k = 0.1; p = 5; L = 4;
pde1 = w*D[c[x, t], t] + u*D[c[x, t], x] - v*D[c[x, t], {x, 2}] == -k*c[x, t]*s[x, t];
pde2 = D[s[x, t], t] == -p*k*c[x, t]*s[x, t];
sol = 
  NDSolve[
    {pde1, pde2, c[0, t] == cd[t], c[L, t] == 0, c[x, 0] == 0, s[x, 0] == s0}, 
    {c, s}, {x, 0, 4}, {t, 0, 10}]
Plot3D[{c[x, t], s[x, t]} /. sol,{t, 0, 4}, {x, 0, 10}]
$\endgroup$
  • 1
    $\begingroup$ What are the error messages? $\endgroup$ – Michael E2 Jun 8 at 13:11
  • $\begingroup$ InterpolatingFunction::dmval NDSolve::nlnum those are the name of errors i got sorry if i didnt use the right style of this forum i am new here $\endgroup$ – ilyes abidi Jun 8 at 13:20
6
$\begingroup$

You have number of basic syntax errors. You can try this

ClearAll[t,s,x,t,c]
cd[t_] :=t*E^(t/2);
s0=10;u=0.4;w=0.9;v=0.01;k=0.1;p=5;L=4;(*I choose these randomly*)
pde1 = w*D[c[x,t],t]+u*D[c[x,t],x]-v*D[c[x,t],{x,2}]==-k*c[x,t]*s[x,t];
pde2 = D[s[x,t],t]==-p*k*c[x,t]*s[x,t];
sol  = NDSolve[{pde1,pde2,c[0,t]==cd[t],c[L,t]==0,c[x,0]==0,s[x,0]==s0},
          {c,s},{x,0,4},{t,0,10}]

Mathematica graphics

Plot3D[{Evaluate[c[x, t] /. sol], Evaluate[s[x, t] /. sol]}, 
      {t, 0, 4}, {x, 0, 10}, PlotStyle -> {Red, Yellow}]

Mathematica graphics

To see full range, you can add PlotRange->All

Plot3D[{Evaluate[c[x, t] /. sol], Evaluate[s[x, t] /. sol]}, 
  {t, 0, 4}, {x, 0, 10}, PlotStyle -> {Red, Yellow}, PlotRange -> All]

Mathematica graphics

Update

To answer comment

You can use specific time instance:

Plot[{Evaluate[c[x, 1] /. sol], Evaluate[s[x, 1] /. sol]},
 {x, 0, 10},
 PlotStyle -> {Red, Blue},
 PlotRange -> {Automatic, {-10, 20}},
 ImageSize -> 400,
 PlotLegends -> {"c", "s"}]

Mathematica graphics

Or you can make a small manipulate

Manipulate[
 Grid[{
   {Row[{"time = ", time}]},
   {
    Plot[{Evaluate[c[x, time] /. sol], Evaluate[s[x, time] /. sol]},
     {x, 0, 10},
     PlotStyle -> {Red, Blue},
     PlotRange -> {Automatic, {-20, 60}},
     ImageSize -> 400,
     PlotLegends -> {"c", "s"}]
    }}],
 {{time, 0, "time"}, 0, 10, .1}
 ]

enter image description here

$\endgroup$
  • $\begingroup$ thnk you so much for the help can you please tell me how to add different colors to see the difference between s and c $\endgroup$ – ilyes abidi Jun 8 at 20:53
  • $\begingroup$ @ilyesabidi Added colors. You need to make the two functions as list, and then use PlotStyle. $\endgroup$ – Nasser Jun 8 at 21:13
  • $\begingroup$ @ilyesabidi added for single time instance example $\endgroup$ – Nasser Jun 8 at 21:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.