2
$\begingroup$

$$\frac{\partial u(t,x)}{\partial t}+\frac{\partial u(t,x)}{\partial x}=0$$

A problem is stated as follows:

subject to initial condition u(0,x)=Exp[-x] Sin(x)^2 and boundary condition u(t,0)=0. Solve this PDE analytically, then plot the solution for the following range of x and t : x from 0 to 3, and t from 0 to 3. Comment on the nature of the solution.

Three questions:

(1) How can I verify solution with short form of ReplaceAll function? Goal is to return True or False. (2) How do I fix module? Or should I use nested modules or some other construct perhaps Block? (3) Am I using the If function correctly because it doesn't return True or False.

However I was successful in finding solution (but not verify it) in global scope...

pde = D[u[t, x], t] + D[u[t, x], x] == 0
ic = u[0, x] == Exp[-x] Sin[x]^2
bc = u[t, 0] == 0
sol = DSolve[{pde, ic, bc}, u[t, x], {t, x}] // FullSimplify
If[u[t, x] == pde /. sol[[1]] // FullSimplify, Print["True"], Print["False"]](* did not work *)
Plot3D[Evaluate[u[t, x] /. sol[[1]]], {t, 0, 3}, {x, 0, 3}]

This is the module that I need to repair...

Module[{pde, ic, bc, sol}; pde = D[u[t, x], t] + D[u[t, x], x] == 0; 
 ic = u[0, x] == Exp[-x] Sin[x]^2; bc = u[t, 0] == 0; 
 sol = u[t, x] /. DSolve[{pde, ic, bc}, u[t, x], {t, x}];
 If[FullSimplify[pde /. sol], Print["Verified True"], 
  Print["Verified False"]]; 
 Plot3D[{u[t, x] /. sol[[1]]}, {t, 0, 3}, {x, 0, 3}], Print[sol[[1]]]]
$\endgroup$
6
  • $\begingroup$ what is Sin2(x) ? $\endgroup$
    – Nasser
    Apr 22, 2020 at 6:58
  • $\begingroup$ "How do I fix module?" Err. What's wrong with module? "Am I using the If function correctly because it doesn't return True or False." We cannot tell because you did not share your code. After all, this question is very incomplete. I suspect that all issues are caused by typos or syntax errors. $\endgroup$ Apr 22, 2020 at 7:09
  • $\begingroup$ @HenrikSchumacher sorry about that. a keyboard combination accidentally submitted question before it was completed. $\endgroup$ Apr 22, 2020 at 7:21
  • $\begingroup$ what do you define a module with no API to call it? What is the point? Just for practice? Normally one defines a called module (i.e. function) that takes input as arguments and returns some result. You also need to replace ; by , after the local module symbols section. $\endgroup$
    – Nasser
    Apr 22, 2020 at 7:24
  • $\begingroup$ @Nasser nice catch. it was copied out of a Word document. i fixed it but it want causing the errors. please have another look at this. $\endgroup$ Apr 22, 2020 at 7:27

2 Answers 2

2
$\begingroup$

A couple of , and ; had to be replaced and the Plot3D has to be returned with Print. Moreover, you used

sol = u[t, x] /. DSolve[{pde, ic, bc}, u[t, x], {t, x}];

where you actually wanted the solution Rule, so I changed the line to

sol = DSolve[{pde, ic, bc}, u[t, x], {t, x}];

Here is the corrected code

sol = Module[{pde, ic, bc, sol},
  pde = D[u[t, x], t] + D[u[t, x], x] == 0;
  ic = u[0, x] == Exp[-x] Sin[x]^2;
  bc = u[t, 0] == 0;
  sol = DSolve[{pde, ic, bc}, u[t, x], {t, x}];
  If[
   FullSimplify[pde /. sol],
   Print["Verified True"],
   Print["Verified False"]
   ];
  Print[Plot3D[{u[t, x] /. sol[[1]]}, {t, 0, 3}, {x, 0, 3}]];
  sol[[1]]
]
$\endgroup$
4
  • $\begingroup$ Wonderful it worked but the If is still returning null. how can i fix it? here is what i have: If[pde /. sol[[1]], Print[Style["Verified True", Bold, Green]], Print[Style["Verified False", Bold, Red]], Print[Style["Verified Unresolved", Bold, Blue]]]; $\endgroup$ Apr 22, 2020 at 7:56
  • $\begingroup$ The If returns Null because Print returns Null. After all, you should avoid print and store the results in separate variables or in a List. $\endgroup$ Apr 22, 2020 at 8:06
  • $\begingroup$ tried your suggestion still returns null. could you see if i am applying the condition correctly? here is what i have: If[pde /. sol[[1]] // FullSimplify, ver = Style["True", Green], ver = Style["False", Red], ver = Style["Null", Blue]]; $\endgroup$ Apr 22, 2020 at 8:59
  • $\begingroup$ What returns "null"? Really, it is important here to have complete code. "could you see if i am applying the condition correctly?" I cannot tell because I do not know what you actually want to do there... Sorry. $\endgroup$ Apr 22, 2020 at 11:48
3
$\begingroup$

This works

Module[{pde, ic, bc, sol, u, x, t},
 pde = D[u[t, x], t] + D[u[t, x], x] == 0;
 ic = u[0, x] == Exp[-x] Sin[x]^2;
 bc = u[t, 0] == 0;
 sol = DSolve[{pde, ic, bc}, u[t, x], {t, x}];
 Plot3D[Evaluate[u[t, x] /. sol[[1]]], {t, 0, 3}, {x, 0, 3}]
 ]

Mathematica graphics

Or this

Module[{pde, ic, bc, sol, u, x, t},
 pde = D[u[t, x], t] + D[u[t, x], x] == 0;
 ic = u[0, x] == Exp[-x] Sin[x]^2;
 bc = u[t, 0] == 0;
 sol = First@DSolve[{pde, ic, bc}, u[t, x], {t, x}];
 Plot3D[Evaluate[u[t, x] /. sol], {t, 0, 3}, {x, 0, 3}]
 ]

It is better to define all symbols used as local to the module, so not to use global one by mistake.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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