Why is DSolve different when the equation is built programmatically?












4












$begingroup$


Here is a simple differential equation.



mwe=y[x]/.First@DSolve [{y'[x]==x-2*x*y[x],y[0]==0},y[x],x]


I get the answer $frac{1}{2}-frac{e^{-x^2}}{2}$



If I build up the same equation in a simple program, I get a different answer:



lsolve[r_,q_,a_,eta_]=y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x];
nwe=lsolve[x,2*x,0,0]


After simplifying, I get the answer $frac{1}{2}-frac{e^{-2x^2}}{2}$. Where did the extra factor of $2$ in the exponent come from? And how can I make it go away?










share|improve this question











$endgroup$












  • $begingroup$
    Here are the docs on this issue, in case it helps someone else. reference.wolfram.com/language/tutorial/…
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:42


















4












$begingroup$


Here is a simple differential equation.



mwe=y[x]/.First@DSolve [{y'[x]==x-2*x*y[x],y[0]==0},y[x],x]


I get the answer $frac{1}{2}-frac{e^{-x^2}}{2}$



If I build up the same equation in a simple program, I get a different answer:



lsolve[r_,q_,a_,eta_]=y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x];
nwe=lsolve[x,2*x,0,0]


After simplifying, I get the answer $frac{1}{2}-frac{e^{-2x^2}}{2}$. Where did the extra factor of $2$ in the exponent come from? And how can I make it go away?










share|improve this question











$endgroup$












  • $begingroup$
    Here are the docs on this issue, in case it helps someone else. reference.wolfram.com/language/tutorial/…
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:42
















4












4








4





$begingroup$


Here is a simple differential equation.



mwe=y[x]/.First@DSolve [{y'[x]==x-2*x*y[x],y[0]==0},y[x],x]


I get the answer $frac{1}{2}-frac{e^{-x^2}}{2}$



If I build up the same equation in a simple program, I get a different answer:



lsolve[r_,q_,a_,eta_]=y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x];
nwe=lsolve[x,2*x,0,0]


After simplifying, I get the answer $frac{1}{2}-frac{e^{-2x^2}}{2}$. Where did the extra factor of $2$ in the exponent come from? And how can I make it go away?










share|improve this question











$endgroup$




Here is a simple differential equation.



mwe=y[x]/.First@DSolve [{y'[x]==x-2*x*y[x],y[0]==0},y[x],x]


I get the answer $frac{1}{2}-frac{e^{-x^2}}{2}$



If I build up the same equation in a simple program, I get a different answer:



lsolve[r_,q_,a_,eta_]=y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x];
nwe=lsolve[x,2*x,0,0]


After simplifying, I get the answer $frac{1}{2}-frac{e^{-2x^2}}{2}$. Where did the extra factor of $2$ in the exponent come from? And how can I make it go away?







differential-equations variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 20:32









Peter Mortensen

33627




33627










asked Feb 12 at 15:25









Joe CorneliJoe Corneli

1335




1335












  • $begingroup$
    Here are the docs on this issue, in case it helps someone else. reference.wolfram.com/language/tutorial/…
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:42




















  • $begingroup$
    Here are the docs on this issue, in case it helps someone else. reference.wolfram.com/language/tutorial/…
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:42


















$begingroup$
Here are the docs on this issue, in case it helps someone else. reference.wolfram.com/language/tutorial/…
$endgroup$
– Joe Corneli
Feb 12 at 15:42






$begingroup$
Here are the docs on this issue, in case it helps someone else. reference.wolfram.com/language/tutorial/…
$endgroup$
– Joe Corneli
Feb 12 at 15:42












1 Answer
1






active

oldest

votes


















9












$begingroup$

You attempt to solve the ODE at the time of definition of lsolve. At this point, q does not depend on x. You really want to use SetDelayed here:



lsolve[r_, q_, a_, eta_] := 
y[x] /. First@DSolve[{y'[x] == r - q*y[x], y[a] == eta}, y[x], x];
nwe = lsolve[x, 2*x, 0, 0] // Simplify



1/2 - E^-x^2/2







share|improve this answer











$endgroup$













  • $begingroup$
    Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:41










  • $begingroup$
    How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
    $endgroup$
    – Henrik Schumacher
    Feb 12 at 17:17










  • $begingroup$
    As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
    $endgroup$
    – Joe Corneli
    Feb 13 at 13:18












  • $begingroup$
    When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:24










  • $begingroup$
    I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:28














Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191391%2fwhy-is-dsolve-different-when-the-equation-is-built-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









9












$begingroup$

You attempt to solve the ODE at the time of definition of lsolve. At this point, q does not depend on x. You really want to use SetDelayed here:



lsolve[r_, q_, a_, eta_] := 
y[x] /. First@DSolve[{y'[x] == r - q*y[x], y[a] == eta}, y[x], x];
nwe = lsolve[x, 2*x, 0, 0] // Simplify



1/2 - E^-x^2/2







share|improve this answer











$endgroup$













  • $begingroup$
    Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:41










  • $begingroup$
    How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
    $endgroup$
    – Henrik Schumacher
    Feb 12 at 17:17










  • $begingroup$
    As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
    $endgroup$
    – Joe Corneli
    Feb 13 at 13:18












  • $begingroup$
    When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:24










  • $begingroup$
    I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:28


















9












$begingroup$

You attempt to solve the ODE at the time of definition of lsolve. At this point, q does not depend on x. You really want to use SetDelayed here:



lsolve[r_, q_, a_, eta_] := 
y[x] /. First@DSolve[{y'[x] == r - q*y[x], y[a] == eta}, y[x], x];
nwe = lsolve[x, 2*x, 0, 0] // Simplify



1/2 - E^-x^2/2







share|improve this answer











$endgroup$













  • $begingroup$
    Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:41










  • $begingroup$
    How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
    $endgroup$
    – Henrik Schumacher
    Feb 12 at 17:17










  • $begingroup$
    As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
    $endgroup$
    – Joe Corneli
    Feb 13 at 13:18












  • $begingroup$
    When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:24










  • $begingroup$
    I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:28
















9












9








9





$begingroup$

You attempt to solve the ODE at the time of definition of lsolve. At this point, q does not depend on x. You really want to use SetDelayed here:



lsolve[r_, q_, a_, eta_] := 
y[x] /. First@DSolve[{y'[x] == r - q*y[x], y[a] == eta}, y[x], x];
nwe = lsolve[x, 2*x, 0, 0] // Simplify



1/2 - E^-x^2/2







share|improve this answer











$endgroup$



You attempt to solve the ODE at the time of definition of lsolve. At this point, q does not depend on x. You really want to use SetDelayed here:



lsolve[r_, q_, a_, eta_] := 
y[x] /. First@DSolve[{y'[x] == r - q*y[x], y[a] == eta}, y[x], x];
nwe = lsolve[x, 2*x, 0, 0] // Simplify



1/2 - E^-x^2/2








share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 12 at 20:49

























answered Feb 12 at 15:31









Henrik SchumacherHenrik Schumacher

60.3k583169




60.3k583169












  • $begingroup$
    Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:41










  • $begingroup$
    How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
    $endgroup$
    – Henrik Schumacher
    Feb 12 at 17:17










  • $begingroup$
    As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
    $endgroup$
    – Joe Corneli
    Feb 13 at 13:18












  • $begingroup$
    When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:24










  • $begingroup$
    I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:28




















  • $begingroup$
    Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
    $endgroup$
    – Joe Corneli
    Feb 12 at 15:41










  • $begingroup$
    How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
    $endgroup$
    – Henrik Schumacher
    Feb 12 at 17:17










  • $begingroup$
    As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
    $endgroup$
    – Joe Corneli
    Feb 13 at 13:18












  • $begingroup$
    When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:24










  • $begingroup$
    I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
    $endgroup$
    – Henrik Schumacher
    Feb 13 at 13:28


















$begingroup$
Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
$endgroup$
– Joe Corneli
Feb 12 at 15:41




$begingroup$
Thanks, I'm glad I asked. Even though my version was syntactically correct, it would have been nice if Mathematica produced a warning!
$endgroup$
– Joe Corneli
Feb 12 at 15:41












$begingroup$
How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
$endgroup$
– Henrik Schumacher
Feb 12 at 17:17




$begingroup$
How should Mathematica be able to detect that this does not align with your expectations? Both syntaxes f[x_] = ... and f[x_] := ... have the applications, e.g., the former can be considered as a shorthand for f[x_] := Evaluate[...]. This is usefull whenever a symbolic compuation can already performed at the time of definition so that it need not be redone every time f is called.
$endgroup$
– Henrik Schumacher
Feb 12 at 17:17












$begingroup$
As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
$endgroup$
– Joe Corneli
Feb 13 at 13:18






$begingroup$
As you said in the answer: "At this point, q does not depend on x." I think Mathematica should be able to tell that I was building an expression that does depend on x and then generate a warning (but certainly not an error) very much along the lines of what you told me. Otherwise everyone who doesn't know about := will end up asking a question similar to mine. How to preempt that and automate your answer? :-)
$endgroup$
– Joe Corneli
Feb 13 at 13:18














$begingroup$
When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
$endgroup$
– Henrik Schumacher
Feb 13 at 13:24




$begingroup$
When an assignment is made with =, Mathematica can only set the left hand side to the value of the right hand side at the time of assignment. Evaluate y[x]/.First@DSolve[{y'[x]==r-q*y[x],y[a]==eta},y[x],x] (just in case: in a fresh kernel) and you will see that the result is (E^(-q x) (E^(a q) eta q - E^(a q) r + E^(q x) r))/q. Because Mathematica solves the ODE at this point in time and because q is independent of x. Of course, you may tell Mathematica that q should depend on x by using q[x] instead of q, but I don't think you will like the result either.
$endgroup$
– Henrik Schumacher
Feb 13 at 13:24












$begingroup$
I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
$endgroup$
– Henrik Schumacher
Feb 13 at 13:28






$begingroup$
I don't understand what you mean by "automation". The difference between = and := lies in the time of execution of the right hand side. If you want that the ODE is solved only after a precise value of q is supplied, you have to use :=. And yes, there are a lot of questions on the subtle differences of = and := on this site. And I have to admit that was also something that was quite miraculous to me when I started to use the language. It helps a bit to recall that Mathematica, in its core, is nothing else but a term rewriting system.
$endgroup$
– Henrik Schumacher
Feb 13 at 13:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191391%2fwhy-is-dsolve-different-when-the-equation-is-built-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

張江高科駅