Message Boards Message Boards

GROUPS:

Avoid issue when differentiating Green's function

Posted 1 month ago
403 Views
|
4 Replies
|
4 Total Likes
|

So I'm trying to plot my Green's function derivative using the following code:

n = 3;    
G[x_, xx_] := -1/(2 n) If[x < xx, x^n xx^-n, xx^n x^-n];
DG[x_, xx_] := D[G[x, xx], x];
Plot[DG[x, 0.3], {x, 0, 1}]

While Plot[G] works, trying to plot its derivative gives me some weird errors: enter image description here

What's wrong with my code?

POSTED BY: Omer G
Answer
4 Replies
Posted 1 month ago

What is n?

Posted 1 month ago

n can be any real number greater than 2, forgot to initialize it.

POSTED BY: Omer G
Answer
Posted 1 month ago

The problem is caused by using SetDelayed in the definition of DG. The derivative is not evaluated until it is used, and when it is, it is a derivative with respect to a constant x. So, either use Set in the definition of DG or force evaluation prior to use.

Plot[Evaluate@DG[x, 0.3], {x, 0, 1}, Exclusions -> None]

enter image description here

If[] is a programming construct. It is not designed to play well with symbolic mathematics. Use Piecewise instead.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract