Ask Your Question
0

Error when trying to integrate

asked 2020-08-14 23:36:56 +0200

Alex89 gravatar image

updated 2020-08-15 11:21:14 +0200

This is what I want to do in Sage. It can be done very easily in Desmos. https://www.desmos.com/calculator/7oc...

I am just trying to do this integral in Sage. When I do it the answer I get is

f(x) = (1/3.33sqrt(2pi))exp((-1/2)((x-9.486)/3.33)^2)

N(definite_integral(f(x), x, 6, 23))

5.35572539176154

edit retag flag offensive close merge delete

Comments

Don't use "N": just use 2*pi and exp(...).

John Palmieri gravatar imageJohn Palmieri ( 2020-08-15 02:40:30 +0200 )edit

I did the changes, now I get a wrong answer.

Alex89 gravatar imageAlex89 ( 2020-08-15 11:17:28 +0200 )edit

I think the problem is parentheses, not Sage. 1/3.33 * sqrt(2*pi) is equal to sqrt(2*pi)/3.33, whereas I think you meant to have all of that in the denominator. So you need parentheses: 1/(3.33 * sqrt(2*pi)), as in @eric_g's answer.

John Palmieri gravatar imageJohn Palmieri ( 2020-08-15 18:02:24 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-08-15 11:37:14 +0200

eric_g gravatar image

You should use numerical_integral:

sage: f(x) = 1/(3.33*sqrt(2*pi))*exp(-1/2*((x-9.486)/3.33)^2)                   
sage: numerical_integral(f, (6,23))                                             
(0.8523901699416254, 9.463431926333425e-15)

The second number in the output is an estimate of the numerical error (type numerical_intergal? for more details, in particular for options to control the numerical error).

edit flag offensive delete link more

Comments

This is perfect. Thank you!

Alex89 gravatar imageAlex89 ( 2020-08-15 12:22:34 +0200 )edit
0

answered 2020-08-15 02:10:56 +0200

Emmanuel Charpentier gravatar image

updated 2020-08-15 02:12:12 +0200

This smells (sloppily reported) homework at ten yards, so a few hints.

  • Sage grammar needs explicit operators:(a)(b) will not be interpreted as a*b...

  • Do not use numerical approximations when you have exact values :

    • N(pi)and N(e) should advantageously replaced by pi and e

    • Your 3.33 reeeks of 3/10

    • Re-read your homework statement and look for a more formal definition of your 9.486 constant.

  • Be more precise "I get a big error" does not help the poor sod, still running H sapiens sapiens L. without mind-reading interface or divoinatory coprocessor, who's trying to divine what error you got, and what hint you may have extracted from it (if only you took the time to read it...).

HTH,

edit flag offensive delete link more

Comments

I don't understand why did you bother to respond if you have no idea why Sage does not compute a definite integral of a normal distribution. And no this is not a homework. I solved it in Desmos in 1 minute. I just wanted to learn how to do it in Sage since I wanted to learn how to use it better. I had no idea that you have to be an expert to even ask questions around here.

Alex89 gravatar imageAlex89 ( 2020-08-15 11:13:34 +0200 )edit

Your original post (before edition by John Palmieri) contained syntactic errors in your expression (= meaningless expression for Sage) ; The error message should have hinted at this.

Not giving the error message is requiring the (volunteer, unpaid, unobligated) person answering you to guess not only the solution to your problem but also what the problem is in the first place, thus doubling the (uncompensated) workload. That's usually considered rude...

FWIW, this forum regularly receives "questions" that aref high-school or university homework. A wide consensus is to give hints rather than a solution, in order to keep the point...

FYI, after guessing what you wanted to type, Sage is perfectly capable to give you a correct answer, both analytically (= symbolically) and numerically

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-08-15 22:16:15 +0200 )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

Stats

Asked: 2020-08-14 23:36:56 +0200

Seen: 262 times

Last updated: Aug 15 '20