How to plot two functions together using odeplot?












1












$begingroup$


I'm trying to compare (graphically) approximations obtained using Euler's method and RK4-method.



ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1};    
p := dsolve(ode, y(x), numeric, method = classical[rk4], stepsize = .25);
f := dsolve(ode, y(x), numeric, method = classical[foreuler], stepsize = .25)


But can't plot them together.



plots:-odeplot([p, f], x = 0 .. 10);

Error, (in plots/odeplot) input is not a valid dsolve/numeric solution


How can I solve this problem? Besides, I want to use style=point for "euler" curve, and plot exact solution on the same graph, if it's possible. What is the best method to obtain exact solution?



Any help would be appreciated.










share|cite|improve this question











$endgroup$












  • $begingroup$
    this seems to be more of a programming issue than a math one. Obviously it's applied to math in a programming language that's very much geared towards math, but I don't know how the community feels about debugging code.
    $endgroup$
    – Tyberius
    Dec 2 '18 at 16:23
















1












$begingroup$


I'm trying to compare (graphically) approximations obtained using Euler's method and RK4-method.



ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1};    
p := dsolve(ode, y(x), numeric, method = classical[rk4], stepsize = .25);
f := dsolve(ode, y(x), numeric, method = classical[foreuler], stepsize = .25)


But can't plot them together.



plots:-odeplot([p, f], x = 0 .. 10);

Error, (in plots/odeplot) input is not a valid dsolve/numeric solution


How can I solve this problem? Besides, I want to use style=point for "euler" curve, and plot exact solution on the same graph, if it's possible. What is the best method to obtain exact solution?



Any help would be appreciated.










share|cite|improve this question











$endgroup$












  • $begingroup$
    this seems to be more of a programming issue than a math one. Obviously it's applied to math in a programming language that's very much geared towards math, but I don't know how the community feels about debugging code.
    $endgroup$
    – Tyberius
    Dec 2 '18 at 16:23














1












1








1





$begingroup$


I'm trying to compare (graphically) approximations obtained using Euler's method and RK4-method.



ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1};    
p := dsolve(ode, y(x), numeric, method = classical[rk4], stepsize = .25);
f := dsolve(ode, y(x), numeric, method = classical[foreuler], stepsize = .25)


But can't plot them together.



plots:-odeplot([p, f], x = 0 .. 10);

Error, (in plots/odeplot) input is not a valid dsolve/numeric solution


How can I solve this problem? Besides, I want to use style=point for "euler" curve, and plot exact solution on the same graph, if it's possible. What is the best method to obtain exact solution?



Any help would be appreciated.










share|cite|improve this question











$endgroup$




I'm trying to compare (graphically) approximations obtained using Euler's method and RK4-method.



ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1};    
p := dsolve(ode, y(x), numeric, method = classical[rk4], stepsize = .25);
f := dsolve(ode, y(x), numeric, method = classical[foreuler], stepsize = .25)


But can't plot them together.



plots:-odeplot([p, f], x = 0 .. 10);

Error, (in plots/odeplot) input is not a valid dsolve/numeric solution


How can I solve this problem? Besides, I want to use style=point for "euler" curve, and plot exact solution on the same graph, if it's possible. What is the best method to obtain exact solution?



Any help would be appreciated.







ordinary-differential-equations maple






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Dec 2 '18 at 17:21









LutzL

57.1k42054




57.1k42054










asked Dec 2 '18 at 16:04









Kelly ShepphardKelly Shepphard

2298




2298












  • $begingroup$
    this seems to be more of a programming issue than a math one. Obviously it's applied to math in a programming language that's very much geared towards math, but I don't know how the community feels about debugging code.
    $endgroup$
    – Tyberius
    Dec 2 '18 at 16:23


















  • $begingroup$
    this seems to be more of a programming issue than a math one. Obviously it's applied to math in a programming language that's very much geared towards math, but I don't know how the community feels about debugging code.
    $endgroup$
    – Tyberius
    Dec 2 '18 at 16:23
















$begingroup$
this seems to be more of a programming issue than a math one. Obviously it's applied to math in a programming language that's very much geared towards math, but I don't know how the community feels about debugging code.
$endgroup$
– Tyberius
Dec 2 '18 at 16:23




$begingroup$
this seems to be more of a programming issue than a math one. Obviously it's applied to math in a programming language that's very much geared towards math, but I don't know how the community feels about debugging code.
$endgroup$
– Tyberius
Dec 2 '18 at 16:23










1 Answer
1






active

oldest

votes


















1












$begingroup$

The key to answering you question is that you can produce the plots separately (using odeplot or plot), and then combine them together using the plots:-display command.



For fun, let's plot the two numeric methods with style=point, at the x-values that match the fixed step-size of the forward-Euler method.



I find it more convenient to use plot instead of plots:-odeplot here, after using the output=listprocedure option of dsolve (numeric).



restart;

ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1}:

stpsz := .25;

stpsz := 0.25

a,b := 0, 10;

a, b := 0, 10

numpts := floor((b-a)/stpsz + 1);

numpts := 41

p := dsolve(ode, y(x), numeric, method = classical[rk4],
stepsize = stpsz, output=listprocedure):

f := dsolve(ode, y(x), numeric, method = classical[foreuler],
stepsize = stpsz, output=listprocedure):

e := dsolve(ode, y(x)):

Pe := plot(eval(y(x),e), x = a..b, style=line, legend="Exact"):

Pp := plot(eval(y(x),p), a..b, color=blue,
style=point, symbol=circle, symbolsize=10,
adaptive=false, numpoints=numpts, legend="RK4"):

Pf := plot(eval(y(x),f), a..b, color=green,
style=point, symbol=diagonalcross, symbolsize=10,
adaptive=false, numpoints=numpts, legend="For.Euler"):

plots:-display(Pe, Pp, Pf);


enter image description here



You can issue the commands eval(y(x),e) and eval(y(x),f) separately, to see that they are a syntax for picking off the RHS expression or procedure from the dsolve solution.






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
    $endgroup$
    – Kelly Shepphard
    Dec 2 '18 at 17:42











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3022810%2fhow-to-plot-two-functions-together-using-odeplot%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









1












$begingroup$

The key to answering you question is that you can produce the plots separately (using odeplot or plot), and then combine them together using the plots:-display command.



For fun, let's plot the two numeric methods with style=point, at the x-values that match the fixed step-size of the forward-Euler method.



I find it more convenient to use plot instead of plots:-odeplot here, after using the output=listprocedure option of dsolve (numeric).



restart;

ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1}:

stpsz := .25;

stpsz := 0.25

a,b := 0, 10;

a, b := 0, 10

numpts := floor((b-a)/stpsz + 1);

numpts := 41

p := dsolve(ode, y(x), numeric, method = classical[rk4],
stepsize = stpsz, output=listprocedure):

f := dsolve(ode, y(x), numeric, method = classical[foreuler],
stepsize = stpsz, output=listprocedure):

e := dsolve(ode, y(x)):

Pe := plot(eval(y(x),e), x = a..b, style=line, legend="Exact"):

Pp := plot(eval(y(x),p), a..b, color=blue,
style=point, symbol=circle, symbolsize=10,
adaptive=false, numpoints=numpts, legend="RK4"):

Pf := plot(eval(y(x),f), a..b, color=green,
style=point, symbol=diagonalcross, symbolsize=10,
adaptive=false, numpoints=numpts, legend="For.Euler"):

plots:-display(Pe, Pp, Pf);


enter image description here



You can issue the commands eval(y(x),e) and eval(y(x),f) separately, to see that they are a syntax for picking off the RHS expression or procedure from the dsolve solution.






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
    $endgroup$
    – Kelly Shepphard
    Dec 2 '18 at 17:42
















1












$begingroup$

The key to answering you question is that you can produce the plots separately (using odeplot or plot), and then combine them together using the plots:-display command.



For fun, let's plot the two numeric methods with style=point, at the x-values that match the fixed step-size of the forward-Euler method.



I find it more convenient to use plot instead of plots:-odeplot here, after using the output=listprocedure option of dsolve (numeric).



restart;

ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1}:

stpsz := .25;

stpsz := 0.25

a,b := 0, 10;

a, b := 0, 10

numpts := floor((b-a)/stpsz + 1);

numpts := 41

p := dsolve(ode, y(x), numeric, method = classical[rk4],
stepsize = stpsz, output=listprocedure):

f := dsolve(ode, y(x), numeric, method = classical[foreuler],
stepsize = stpsz, output=listprocedure):

e := dsolve(ode, y(x)):

Pe := plot(eval(y(x),e), x = a..b, style=line, legend="Exact"):

Pp := plot(eval(y(x),p), a..b, color=blue,
style=point, symbol=circle, symbolsize=10,
adaptive=false, numpoints=numpts, legend="RK4"):

Pf := plot(eval(y(x),f), a..b, color=green,
style=point, symbol=diagonalcross, symbolsize=10,
adaptive=false, numpoints=numpts, legend="For.Euler"):

plots:-display(Pe, Pp, Pf);


enter image description here



You can issue the commands eval(y(x),e) and eval(y(x),f) separately, to see that they are a syntax for picking off the RHS expression or procedure from the dsolve solution.






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
    $endgroup$
    – Kelly Shepphard
    Dec 2 '18 at 17:42














1












1








1





$begingroup$

The key to answering you question is that you can produce the plots separately (using odeplot or plot), and then combine them together using the plots:-display command.



For fun, let's plot the two numeric methods with style=point, at the x-values that match the fixed step-size of the forward-Euler method.



I find it more convenient to use plot instead of plots:-odeplot here, after using the output=listprocedure option of dsolve (numeric).



restart;

ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1}:

stpsz := .25;

stpsz := 0.25

a,b := 0, 10;

a, b := 0, 10

numpts := floor((b-a)/stpsz + 1);

numpts := 41

p := dsolve(ode, y(x), numeric, method = classical[rk4],
stepsize = stpsz, output=listprocedure):

f := dsolve(ode, y(x), numeric, method = classical[foreuler],
stepsize = stpsz, output=listprocedure):

e := dsolve(ode, y(x)):

Pe := plot(eval(y(x),e), x = a..b, style=line, legend="Exact"):

Pp := plot(eval(y(x),p), a..b, color=blue,
style=point, symbol=circle, symbolsize=10,
adaptive=false, numpoints=numpts, legend="RK4"):

Pf := plot(eval(y(x),f), a..b, color=green,
style=point, symbol=diagonalcross, symbolsize=10,
adaptive=false, numpoints=numpts, legend="For.Euler"):

plots:-display(Pe, Pp, Pf);


enter image description here



You can issue the commands eval(y(x),e) and eval(y(x),f) separately, to see that they are a syntax for picking off the RHS expression or procedure from the dsolve solution.






share|cite|improve this answer









$endgroup$



The key to answering you question is that you can produce the plots separately (using odeplot or plot), and then combine them together using the plots:-display command.



For fun, let's plot the two numeric methods with style=point, at the x-values that match the fixed step-size of the forward-Euler method.



I find it more convenient to use plot instead of plots:-odeplot here, after using the output=listprocedure option of dsolve (numeric).



restart;

ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1}:

stpsz := .25;

stpsz := 0.25

a,b := 0, 10;

a, b := 0, 10

numpts := floor((b-a)/stpsz + 1);

numpts := 41

p := dsolve(ode, y(x), numeric, method = classical[rk4],
stepsize = stpsz, output=listprocedure):

f := dsolve(ode, y(x), numeric, method = classical[foreuler],
stepsize = stpsz, output=listprocedure):

e := dsolve(ode, y(x)):

Pe := plot(eval(y(x),e), x = a..b, style=line, legend="Exact"):

Pp := plot(eval(y(x),p), a..b, color=blue,
style=point, symbol=circle, symbolsize=10,
adaptive=false, numpoints=numpts, legend="RK4"):

Pf := plot(eval(y(x),f), a..b, color=green,
style=point, symbol=diagonalcross, symbolsize=10,
adaptive=false, numpoints=numpts, legend="For.Euler"):

plots:-display(Pe, Pp, Pf);


enter image description here



You can issue the commands eval(y(x),e) and eval(y(x),f) separately, to see that they are a syntax for picking off the RHS expression or procedure from the dsolve solution.







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered Dec 2 '18 at 17:35









aceracer

3,640199




3,640199












  • $begingroup$
    Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
    $endgroup$
    – Kelly Shepphard
    Dec 2 '18 at 17:42


















  • $begingroup$
    Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
    $endgroup$
    – Kelly Shepphard
    Dec 2 '18 at 17:42
















$begingroup$
Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
$endgroup$
– Kelly Shepphard
Dec 2 '18 at 17:42




$begingroup$
Thank you very much, it was the best possible answer. Now it's clear for me how to plot graphs of approximations. :)
$endgroup$
– Kelly Shepphard
Dec 2 '18 at 17:42


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematics 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%2fmath.stackexchange.com%2fquestions%2f3022810%2fhow-to-plot-two-functions-together-using-odeplot%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

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei