ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 21 Oct 2021 03:32:09 +0200How to plot changes as a result of a parameter?https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/ Hi,
I am new to Sage, with a Mathematica background, and am finding my way through things but am stumped on two problems and would be gladly pointed in the right direction.
What I want to do is plot how changing a parameter, in this case k (the domain of the graph), affects the accuracy of an approximation. My definition of accuracy in this case is simply the integral of the difference of the functions, which works for the purpose I have.
a = point2d((k, N(integrate(abs(nsin(x)-sin(x)),x,-k,k))) for k in range(-5, 5))
a
I have defined nsin(x) as
nsin(x)=x-x^3/factorial(3)+x^5/factorial(5)
I would also like to be able to define an error function so that I could instead write:
error(function1, function2, xmin,xmax) = N(integrate(abs(function1-function2),x,-xmin,xmax)))
but I haven't figured out how to pass a function as an argument within a function.
Any help appreciated.
For context, I am a high school mathematics teacher and I am working with a group of enrichment students to compare the accuracy of some approximations of the Gaussian function to the analytical form.
Wed, 20 Oct 2021 04:50:55 +0200https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/Comment by Fermion for <p>Hi,</p>
<p>I am new to Sage, with a Mathematica background, and am finding my way through things but am stumped on two problems and would be gladly pointed in the right direction.</p>
<p>What I want to do is plot how changing a parameter, in this case k (the domain of the graph), affects the accuracy of an approximation. My definition of accuracy in this case is simply the integral of the difference of the functions, which works for the purpose I have.</p>
<pre><code>a = point2d((k, N(integrate(abs(nsin(x)-sin(x)),x,-k,k))) for k in range(-5, 5))
a
</code></pre>
<p>I have defined nsin(x) as</p>
<pre><code>nsin(x)=x-x^3/factorial(3)+x^5/factorial(5)
</code></pre>
<p>I would also like to be able to define an error function so that I could instead write:</p>
<pre><code> error(function1, function2, xmin,xmax) = N(integrate(abs(function1-function2),x,-xmin,xmax)))
</code></pre>
<p>but I haven't figured out how to pass a function as an argument within a function.</p>
<p>Any help appreciated.</p>
<p>For context, I am a high school mathematics teacher and I am working with a group of enrichment students to compare the accuracy of some approximations of the Gaussian function to the analytical form.</p>
https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?comment=59422#post-id-59422Good point. We are moving towards looking at the skew normal and approximations of the skew normal, neither of which are symmetric. I can just add an abs function as needed.Thu, 21 Oct 2021 03:31:51 +0200https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?comment=59422#post-id-59422Comment by rburing for <p>Hi,</p>
<p>I am new to Sage, with a Mathematica background, and am finding my way through things but am stumped on two problems and would be gladly pointed in the right direction.</p>
<p>What I want to do is plot how changing a parameter, in this case k (the domain of the graph), affects the accuracy of an approximation. My definition of accuracy in this case is simply the integral of the difference of the functions, which works for the purpose I have.</p>
<pre><code>a = point2d((k, N(integrate(abs(nsin(x)-sin(x)),x,-k,k))) for k in range(-5, 5))
a
</code></pre>
<p>I have defined nsin(x) as</p>
<pre><code>nsin(x)=x-x^3/factorial(3)+x^5/factorial(5)
</code></pre>
<p>I would also like to be able to define an error function so that I could instead write:</p>
<pre><code> error(function1, function2, xmin,xmax) = N(integrate(abs(function1-function2),x,-xmin,xmax)))
</code></pre>
<p>but I haven't figured out how to pass a function as an argument within a function.</p>
<p>Any help appreciated.</p>
<p>For context, I am a high school mathematics teacher and I am working with a group of enrichment students to compare the accuracy of some approximations of the Gaussian function to the analytical form.</p>
https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?comment=59409#post-id-59409Note e.g. for $k=-5$ you integrate from $-(-5)=5$ to $-5$ (i.e. the interval $[-5,5]$ with the reversed orientation), hence the "error" is negative; it would be reasonable to restrict to positive $k$.Wed, 20 Oct 2021 11:47:55 +0200https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?comment=59409#post-id-59409Answer by rburing for <p>Hi,</p>
<p>I am new to Sage, with a Mathematica background, and am finding my way through things but am stumped on two problems and would be gladly pointed in the right direction.</p>
<p>What I want to do is plot how changing a parameter, in this case k (the domain of the graph), affects the accuracy of an approximation. My definition of accuracy in this case is simply the integral of the difference of the functions, which works for the purpose I have.</p>
<pre><code>a = point2d((k, N(integrate(abs(nsin(x)-sin(x)),x,-k,k))) for k in range(-5, 5))
a
</code></pre>
<p>I have defined nsin(x) as</p>
<pre><code>nsin(x)=x-x^3/factorial(3)+x^5/factorial(5)
</code></pre>
<p>I would also like to be able to define an error function so that I could instead write:</p>
<pre><code> error(function1, function2, xmin,xmax) = N(integrate(abs(function1-function2),x,-xmin,xmax)))
</code></pre>
<p>but I haven't figured out how to pass a function as an argument within a function.</p>
<p>Any help appreciated.</p>
<p>For context, I am a high school mathematics teacher and I am working with a group of enrichment students to compare the accuracy of some approximations of the Gaussian function to the analytical form.</p>
https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?answer=59408#post-id-59408The syntax `f(x) = x^2` defines a [callable symbolic expression](https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/callable.html) `f`. Callable symbolic expressions are limited in the type of arguments they can take; in particular they cannot take functions as arguments.
But you can define an ordinary Python function instead:
def error(function1, function2, xmin, xmax):
x = var('x')
return N(integrate(abs(function1(x)-function2(x)),x,xmin,xmax))
and then:
sage: nsin(x)=x-x^3/factorial(3)+x^5/factorial(5)
sage: point2d((k, error(nsin,sin,-k,k)) for k in range(-5, 5))
![error graph](/upfiles/16347213877758381.png)
In the `error` function I defined the local variable `x` to be the symbolic variable named `x`, to avoid relying on the assumption that the variable `x` has already been defined that way elsewhere. It is defined that way by default in a fresh SageMath session, but it's not uncommon to re-define `x`, and we should allow that without breaking our function. To solve the issue in a different way, you could also add `x` as a (third) parameter in the `error` function instead.Wed, 20 Oct 2021 11:26:20 +0200https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?answer=59408#post-id-59408Comment by Fermion for <p>The syntax <code>f(x) = x^2</code> defines a <a href="https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/callable.html">callable symbolic expression</a> <code>f</code>. Callable symbolic expressions are limited in the type of arguments they can take; in particular they cannot take functions as arguments.</p>
<p>But you can define an ordinary Python function instead:</p>
<pre><code>def error(function1, function2, xmin, xmax):
x = var('x')
return N(integrate(abs(function1(x)-function2(x)),x,xmin,xmax))
</code></pre>
<p>and then:</p>
<pre><code>sage: nsin(x)=x-x^3/factorial(3)+x^5/factorial(5)
sage: point2d((k, error(nsin,sin,-k,k)) for k in range(-5, 5))
</code></pre>
<p><img src="/upfiles/16347213877758381.png" alt="error graph"></p>
<p>In the <code>error</code> function I defined the local variable <code>x</code> to be the symbolic variable named <code>x</code>, to avoid relying on the assumption that the variable <code>x</code> has already been defined that way elsewhere. It is defined that way by default in a fresh SageMath session, but it's not uncommon to re-define <code>x</code>, and we should allow that without breaking our function. To solve the issue in a different way, you could also add <code>x</code> as a (third) parameter in the <code>error</code> function instead.</p>
https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?comment=59423#post-id-59423Thank you. This was very helpful.Thu, 21 Oct 2021 03:32:09 +0200https://ask.sagemath.org/question/59404/how-to-plot-changes-as-a-result-of-a-parameter/?comment=59423#post-id-59423