Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5215
#Getting help #with specific command: type the command name, append the question mark. E like solve? #Example solve? #Help for specific topic #Example search_doc("numerical approximation")
reset
<function reset at 0x7f91e82fe050>
# SageMath as a calculator 15*0.7^3
5.14500000000000
32^15
37778931862957161709568
sqrt(8);sqrt(8.0)
2*sqrt(2) 2.82842712474619
38/46
19/23
pi; N(pi);pi.n(digits=20)
pi 3.14159265358979 3.1415926535897932385
#Exercise 1 (ws) Evaluate the number e with 15 decimal digits e; N(e);e.n(digits=15)
e 2.71828182845905 2.71828182845905
#Demo2:Functions and expressions. Derivative #Pre-defined functions N(log(100));log(100,10);val=log(100.,2)
4.60517018598809 2
val;2^val
6.64385618977472 100.000000000000
#Exercise 2 (ws) (a)Evaluate three built-in functions of your choice for inputs of your choice. # (b) Explain why SageMath refuses to give the result for exp(3) and force it to evaluate the function exp(3.);
20.0855369231877
#Simple user defined functions f(x)=x^2-3*x+4; f(5)
14
exp=sin(x)+x^2;exp(1.1); #You will see error message;Get used to this. Learn by practicing.
2.10120736006144
p=plot(f(x),-2,2,figsize=[3,3],color="blue",thickness=3)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> NameError: name 'f' is not defined
plot(x^2-3*x+4,-2,2,figsize=[4,3])
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> TypeError: 'tuple' object is not callable
#Demo3: Basic plotting plot(sin(x),-2,2,figsize=[4,3])
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> TypeError: 'tuple' object is not callable
#Saving a plot in a file: #give a plot a name (say, p ) and type the command #p.save('[file name].png')
#Exercise 3 (ws) #Remove the pound sign before the command "parametric_plot", and create a parametric plot using the #command (choose your favorite color and specify the size of the plot). #Save the plot in a file with name 'my_first_plot.png' parametric_plot((cos(x),sin(x)^3),(x,0,2*pi),color="lightblue",figsize=[5 ,5 ])
#Declearing variables var('a b c')
(a, b, c)
#Solving equations exactly solve(x^2+b*x-5==0,x)
[x == -1/2*b - 1/2*sqrt(b^2 + 20), x == -1/2*b + 1/2*sqrt(b^2 + 20)]
#find_root?
find_root?
File: /ext/sage/sage-8.8_1804/local/lib/python2.7/site-packages/sage/misc/lazy_import.pyx Signature : find_root(f, a, b, xtol=1e-12, rtol=8.881784197001252e-16, maxiter=100, full_output=False) Docstring : Numerically find a root of "f" on the closed interval [a,b] (or [b,a]) if possible, where "f" is a function in the one variable. Note: this function only works in fixed (machine) precision, it is not possible to get arbitrary precision approximations with it. INPUT: * "f" -- a function of one variable or symbolic equality * "a", "b" -- endpoints of the interval * "xtol", "rtol" -- the routine converges when a root is known to lie within "xtol" of the value return. Should be >= 0. The routine modifies this to take into account the relative precision of doubles. By default, rtol is "4*numpy.finfo(float).eps", the minimum allowed value for "scipy.optimize.brentq", which is what this method uses underneath. This value is equal to "2.0**-50" for IEEE-754 double precision floats as used by Python. * "maxiter" -- integer; if convergence is not achieved in "maxiter" iterations, an error is raised. Must be >= 0. * "full_output" -- bool (default: "False"), if "True", also return object that contains information about convergence. EXAMPLES: An example involving an algebraic polynomial function: sage: R.<x> = QQ[] sage: f = (x+17)*(x-3)*(x-1/8)^3 sage: find_root(f, 0,4) 2.999999999999995 sage: find_root(f, 0,1) # abs tol 1e-6 (note -- precision of answer isn't very good on some machines) 0.124999 sage: find_root(f, -20,-10) -17.0 In Pomerance's book on primes he asserts that the famous Riemann Hypothesis is equivalent to the statement that the function f(x) defined below is positive for all x >= 2.01: sage: def f(x): ....: return sqrt(x) * log(x) - abs(Li(x) - prime_pi(x)) We find where f equals, i.e., what value that is slightly smaller than 2.01 that could have been used in the formulation of the Riemann Hypothesis: sage: find_root(f, 2, 4, rtol=0.0001) 2.0082... This agrees with the plot: sage: plot(f,2,2.01) Graphics object consisting of 1 graphics primitive The following example was added due to https://trac.sagemath.org/4942 and demonstrates that the function need not be defined at the endpoints: sage: find_root(x^2*log(x,2)-1,0, 2) # abs tol 1e-6 1.41421356237 The following is an example, again from https://trac.sagemath.org/4942 where Brent's method fails. Currently no other method is implemented, but at least we acknowledge the fact that the algorithm fails: sage: find_root(1/(x-1)+1,0, 2) 0.0 sage: find_root(1/(x-1)+1,0.00001, 2) Traceback (most recent call last): ... NotImplementedError: Brent's method failed to find a zero for f on the interval An example of a function which evaluates to NaN on the entire interval: sage: f(x) = 0.0 / max(0, x) sage: find_root(f, -1, 0) Traceback (most recent call last): ... RuntimeError: f appears to have no zero on the interval
#Finding roots approximately find_root(cos(t)-t,0,3)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> NameError: name 't' is not defined
# Syntax of user defined function syntax def char_fcn01(x): if x<=0 or x>=1: return 0 else: return 1
char_fcn01(0.5);char_fcn(1.5)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> NameError: name 'char_fcn01' is not defined
#Exercise 4. Define a function that finds the minimal element of the list. Test your function on example of your choice.
def minimal_el(L): m=L[0] for j in range(1,len(L)): if L[j]<m: m=L[j] return m c=range(1,10) minimal_el(c) a=[i^2 for i in range (2,4)] minimal_el(a)
1 4
#Exercise 5. Experiment with commands in the file sage-quickref-BasicMath.pdf. Include two examples using commands of your choice. Your commands should be different from arithmetic operations and the commands used in this worksheedef def func_f(x) if x<=0 or x>=10 return -1 else return 11 func_f(x^3)
Error in lines 1-5 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "<string>", line 1 def func_f(x) ^ SyntaxError: invalid syntax