radaar

202 Reputation

8 Badges

5 years, 284 days

MaplePrimes Activity


These are Posts that have been published by radaar

Newton raphson method is used for optimization of functions and is based on taylor series expansion. Here is the code for a three level newton raphson method.
 

restart; with(Student[MultivariateCalculus]); ff := proc (xx) xx^3-2*xx+2 end proc; ii := 0; XX[ii] := 2; while ii < 25 do GR := Gradient(ff(xx), [xx] = [XX[ii]]); GR1 := evalf(GR[1]); HESS := Student[VectorCalculus]:-Hessian(ff(xx), [xx] = [XX[ii]]); HESS1 := evalf(HESS[1]); YY[ii] := XX[ii]-GR1[1]/HESS1[1]; GR := Gradient(ff(xx), [xx] = [YY[ii]]); GR1 := evalf(GR[1]); HESS := Student[VectorCalculus]:-Hessian(ff(xx), [xx] = [YY[ii]]); HESS1 := evalf(HESS[1]); ZZ[ii] := YY[ii]-GR1[1]/HESS1[1]; GR := Gradient(ff(xx), [xx] = [ZZ[ii]]); GR1 := evalf(GR[1]); HESS := Student[VectorCalculus]:-Hessian(ff(xx), [xx] = [ZZ[ii]]); HESS1 := evalf(HESS[1]); ii := ii+1; XX[ii] := ZZ[ii-1]-GR1[1]/HESS1[1]; printf("%a\n", XX[ii]) end do

.8180854533
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808
.8164965808

 

ff(.8164965808)

.911337892

(1)

with(Optimization); Minimize(xx^3-2*xx+2, xx = -2 .. 2)

[HFloat(0.9113378920963653), [xx = HFloat(0.8164965785244629)]]

(2)

NULL


 

Download THREE_LEVEL_NEWTON_RAPHSON_METHOD1.mw

Monte Carlo integration uses random sampling unlike classical techniques like the trapezoidal or Simpson's rule in evaluating the integration numerically.

restart; ff := proc (rho, phi) return exp(rho*cos(phi))*rho end proc; aa := 0; bb := 1; cc := 0; dd := 2*Pi; alfa := 5; nrun := 15000; sum1 := 0; sum2 := 0; X := Statistics:-RandomVariable(Uniform(0, 1)); SX := Statistics:-Sample(X); for ii to nrun do u1 := SX(1)[1]; u2 := SX(1)[1]; xx1 := aa+(bb-aa)*u1; xx2 := cc+(dd-cc)*u2; xx3 := (bb-aa)*(1-u1); xx4 := (dd-cc)*(1-u2); sum1 := sum1+evalf(ff(xx1, xx2)); sum2 := sum2+evalf(ff(xx1, xx2))+evalf(ff(xx1, xx4))+evalf(ff(xx3, xx2))+evalf(ff(xx3, xx4)) end do; area1 := (bb-aa)*(dd-cc)*sum1/nrun; area2 := (bb-aa)*(dd-cc)*sum2/(4*nrun); area2

HFloat(3.5442090450428574)

(1)

evalf(Int(exp(rho*cos(phi))*rho, rho = 0 .. 1, phi = 0 .. 2*Pi))

3.550999378

(2)

NULL


 

Download MONTE_CARLO_INTEGRATION1.mw

 

 

The binary search algorithm is used to obtain the index of a given number by dividing the search bound in half over iteration. If the value entered in the array a message pop up telling that ''value is not present in the array". Please see the code. 
 

restart; with(ArrayTools); AA := Array(1 .. 10, [20, 2, 30, 4, 50, 7, 60, 8, 90, 100]); AA := sort(AA); KEYVALUE := 200; DUP_KEYVALUE := infinity; low := 1; high := NumElems(AA); while DUP_KEYVALUE <> KEYVALUE do mid := floor((low+high)*(1/2)); if AA[mid] = KEYVALUE then DUP_KEYVALUE := KEYVALUE; printf("%s\n %a\n", "the index is ", mid) elif AA[mid] < KEYVALUE then low := mid+1 elif AA[mid] > KEYVALUE then high := mid-1 end if; if low > high then printf("%s\n", "value not present in the array"); break end if end do


 

Download BINARY_SEARCH1.mw

 

Page 1 of 1