Assign 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Kernel: SageMath 10.

Shri Ramdeobaba College of Engineering and Management Nagpur

Department of Mathematics

Computational Mathematics Lab (MAP 151) (2020-21)

Expriment Number 1

Aim: To Use SageMath as Advanced Calculator

Name:Piyush Patel

Roll Number: 39

Section:C

Date: 26/09/2024

Basic numerical Computation

In [1]: 564545+73636 #

Out[1]: 638181

In [2]:
6454-8363535

Out[2]: -8357081

In [3]: 565*709

Out[3]: 400585

In [4]: 759^11

Out[4]: 48157012271803961449924277680359

In [5]:
759**11 # Returns 11th power of 759

Out[5]: 48157012271803961449924277680359

In [6]:
81^(1/4)

Out[6]: 3

In [7]:
(79/12)

Out[7]: 79/12

In [9]:
(79/12.0)

Out[9]: 6.58333333333333

In [10]:
(79/12).n()

Out[10]: 6.58333333333333

In [11]: (79/12).n()

Out[11]: 6.58333333333333

In [12]: 79//12 # Returns quotient

Out[12]: 6

In [13]: 79 % 12 # Returns remainder

Out[13]: 7

In [14]:
17*(68+72)

Out[14]: 2380

In [15]: (sin(81)).n(digits=20)

Out[15]: -0.62988799427445387857

In [16]: sin(81.0)

Out[16]: -0.629887994274454

In [17]: cos(-5.3)

Out[17]: 0.554374336179161

In [18]: tan(1/2.0)

Out[18]: 0.546302489843790

In [19]:
asin(-0.2)

Out[19]: -0.201357920790331

In [20]: exp(1.0)

Out[20]: 2.71828182845905

In [21]:
pi.n(digits=50)

Out[21]: 3.1415926535897932384626433832795028841971693993751

In [22]: F = factorial(575)

In [23]: print(F)

Out[23]: 739044177711914144340163003120913688696220649621834947725038382067263003829165002719927604137960497761382230035613758248367109475446243386206042021342172687553887264311319034417173352830563722961607927
247491455377926232706894408228906373929389914766429633441130417890962892264047803648004312389788980821791450595169682444396373566601199730056372616248829184760330188815674476093815979031329183226235508
112862108311290423305168936214231975260326069408598967329585653717484240200348138286770572945205714711999695761816962916034492974117436630500215341753045903688819594481866023699076493279365981468000818
855698232700003654518792463763404339738107299048799906048841964705471890915918292973484895118626793346555224135808997264431491277987559661335668764150242293545429007510476811236475825803556784159881592
248340578877453492644622428159485520688049139049036099733563005129343517587944213020542348768471260895734606790511189558065199075432432972197291804384765433362484245649135716108532122894777454234895185
238412755220249179457138242922062525378682086557968284244292121989379136729504832762707050058213710066049412773563483064907375850286271364616477909255924092061802563238092546271537438503469056000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

In [24]:
F.ndigits()

Out[24]: 1339

In [27]: w=F.digits()
w

Expand

In [28]:
w[0] ## gives digit presant at unit place of number 575
w[1] ## gives digit presant at tenth place of number 575

Out[28]: 0

Defining Variables

In [29]: a = 7563
b = 87431

In [30]: print(a,b)

Out[30]: 7563 87431

In [31]: first_number = 73537221


second_number = 70305037221
first_number* second_number

Out[31]: 5170037059533902841

In [32]: b.is_prime()

Out[32]: False

In [34]: b.next_prime()

Out[34]: 87433

In [35]: gcd(a,b)

Out[35]: 1

In [36]: lcm(a,b)

Out[36]: 661240653

In [37]: nth_prime(100)

Out[37]: 541

In [38]: list(primes(50,100))

Out[38]: [53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Defining Functions
f(x) = sin(x^2)e^(− x) + 3x + 1

In [60]: f(x) = sin(x^2)*exp(-x)+3*x+1

In [61]: f(2.1)

Out[61]: 7.18309969277139

In [62]: f.diff()

Out[62]: x |--> 2*x*cos(x^2)*e^(-x) - e^(-x)*sin(x^2) + 3

In [64]: f.diff(2)

Out[64]: x |--> -4*x^2*e^(-x)*sin(x^2) - 4*x*cos(x^2)*e^(-x) + 2*cos(x^2)*e^(-x) + e^(-x)*sin(x^2)

In [65]: f.diff(3)

Out[65]: x |--> -8*x^3*cos(x^2)*e^(-x) + 12*x^2*e^(-x)*sin(x^2) + 6*x*cos(x^2)*e^(-x) - 12*x*e^(-x)*sin(x^2) - 6*cos(x^2)*e^(-x) - e^(-x)*sin(x^2)

In [66]: f.integral(x)

Out[66]: x |--> 3/2*x^2 - 1/16*sqrt(pi)*((-(I + 1)*sqrt(2)*cos(1/4) + (I - 1)*sqrt(2)*sin(1/4))*erf(-1/2*(-1)^(3/4)*(2*I*x + 1)) + (-(I + 1)*sqrt(2)*cos(1/4) + (I - 1)*sqrt(2)*sin(1/4))*erf(-(1/4*I -
1/4)*sqrt(2)*(2*I*x + 1)) + ((I - 1)*sqrt(2)*cos(1/4) - (I + 1)*sqrt(2)*sin(1/4))*erf(-(1/4*I + 1/4)*sqrt(2)*(2*I*x - 1)) + (-(I - 1)*sqrt(2)*cos(1/4) + (I + 1)*sqrt(2)*sin(1/4))*erf(1/2*(2*I*x -
1)/sqrt(-I))) + x

In [67]: var('x,y')
h(x,y) = sin(x^2-y^2)

In [68]: h(1.0,2.0)

Out[68]: -0.141120008059867

Taking Help on functions


Use solve? or solve?? of help(solve) to find help on solve function

In [69]: var('a,b,c')
show((solve(a*x^2+b*x+c==0,x)))

Out[69]:
[x = − ]
b+ b2 − 4 ac b− b2 − 4 ac
,x = −
​ ​

2a 2a
​ ​

In [70]: var('x1,x2')
solve([3*x1-2*x2==7,2*x1+3*x2==11],x1,x2)

Out[70]: [[x1 == (43/13), x2 == (19/13)]]

In [71]: def quad(a,b,c):


x1=(-b+sqrt(b**2-4*a*c))/(2*a)
x2=(-b-sqrt(b**2-4*a*c))/(2*a)
return(x1,x2)

In [72]: a,b,c=2,3,-2
x1,x2=quad(a,b,c)
show(f'The roots of the quadratic are {x1} and {x2}')

Out[72]: The roots of the quadratic are 1/2 and -2

In [73]: show((x1.n()), (x2).n())

Out[73]: 0.500000000000000 − 2.00000000000000

In [94]: def Heron(a,b,c):


s=(a+b+c)/2.0
A=sqrt(s*(s-a)*(s-b)*(s-c))
return A

In [75]:
a,b,c=3,7,9
Area=Heron(a,b,c)
print(f'The area of Triangle is {Area.n()}')

Out[75]: The area of Triangle is 8.78564169540279

Find the sum of first 50 numbers

In [76]:
k=0

In [77]:
for i in range (1,51):# (1,51) will take all numbers from 1 to 50 only.
k=k+i
k

Out[77]: 1275

Exercise Problem

1.Find the roots of x3 − 2x^2 − 5*x + 6 = 0.

2.Solve the system of non linear equations x^2 + y^2=4 and y=x^2-2 for x and y

3.Find the number digits in 1050! and how many zeros are there in 1050!

4. Find the factors of sum of digits of 1275!.

5.Evaluate ∫cos( x )/(√sin ( x ) + 1).dx

6.Find the fifth order derivative of f(x) = ln(x) + 3x**3 + cos(2x)

7.Suppose an investment is made to a bank by an indivisual. The bank gives an annual interest at the rate 4%. Return is

calculated by using compound interest.

Solution1

In [97]:
var('x1,x2,x3')
solve(x**3-2*x**2-5*x+6 ==0,x)

Out[97]: [x == 3, x == -2, x == 1]

Solution2

In [98]: var('x,y')

Out[98]: (x, y)

In [99]: show(solve([x**2+y**2==4,y==x**2-2],x,y))

[[x = − 3, y = 1] , [x = 3, y = 1] , [x = 0, y = (−2)]]
Out[99]:
​ ​

Solution3

In [100]: a=factorial(1050)
a.ndigits()
print('Number of digits of 1050! is ',{a.ndigits()})
k=0

Out[100]: Number of digits of 1050! is {2719}

In [101]: for i in range(0,a.ndigits()):


if a.digits()[i]==0:
k=k+1
print('Number of zero in 1050! is',k)

Out[101]: Number of zero in 1050! is 472

Solution4

In [119]: b=factorial(1275)

Out[119]: ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[119], line 1
----> 1 b.factorial(Integer(1275))
TypeError: factorial() takes exactly 0 positional arguments (1 given)

Solution5

In [112]: f(x)=(cos(x))/(sin(x)+1)
f.integral(x)

Out[112]: x |--> log(sin(x) + 1)

Solution6

In [113]: f= ln(x) + 3*x*3 + cos(2*x)


show(f.diff(5))

Out[113]: 24
− 32 sin (2 x)
x5

Solution7

In [108]: def interest(amt,year):


ret=amt*((1+(4/100))^year)
return ret
ret = interest(1000,10.0)
print("The return amount on investment years is", ret)

Out[108]: The return amount on investment years is 1480.24428491834

what you learn


from sagenmath's exp.1 of to use SageMath as Advanced Calculator.and i learn to solve huge mathematical problems or equaton by few commands in just few seconds it was really helpful and fun learning for me.

In [0]:

You might also like