Ask Your Question
1

How to plot changes as a result of a parameter?

asked 2021-10-20 04:50:55 +0100

Fermion gravatar image

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.

edit retag flag offensive close merge delete

Comments

Note 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$.

rburing gravatar imagerburing ( 2021-10-20 11:47:55 +0100 )edit

Good 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.

Fermion gravatar imageFermion ( 2021-10-21 03:31:51 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-10-20 11:26:20 +0100

rburing gravatar image

updated 2021-10-20 11:28:54 +0100

The syntax f(x) = x^2 defines a callable symbolic expression 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

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.

edit flag offensive delete link more

Comments

Thank you. This was very helpful.

Fermion gravatar imageFermion ( 2021-10-21 03:32:09 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-10-20 04:50:55 +0100

Seen: 311 times

Last updated: Oct 20 '21