#hw2.txt #27 Jan 2014 #Sowmya Srinivasan #Some examples from pages 30-60 #sequences x:= 1,2,3; y:= 4,5,6; seq(i^2, i=1..5); f:= 'f' : seq(f(i), i=1..7); L:=a+b+2c+4d; op(%); nops(L); op(1,L); #sets a:={1,2,3,4}; b:={2,4,6,8}; a union b; a minus b; a intersect b; member(3,a); member(5,a); #tables T:=table([a,b]); S:=table([(1)=A,(3)=B+C,(5)=A*B*C]); S[3]; S; op(S); #arrays A:=array(1..2,1..3,[]); op(A); B:=array(1..2,1..2,1..2,[]); op(B); c:=array(1..2,1..2): C[1,1]:=1 : c[1,2]:=0 : C[2,1]:=0 : C[2,2]:= 1 : op(C); F:=array(1..2,1..3,[[1,2,4],[2,4,3]]); #data conversions A:={1,2,3}: s:=1,2,3: L:=[1,2,3]: type(L,list); type(A,set); convert(s,list); #functions f:=x -> x^2-3*x+5; f(2); g:=(x,y)-> x*y/(1+x^2+y^2); g(sin(t),cos(t)); simplify(%); f:= x-> x^2; g:= x-> sqrt(1-x); (f@g)(x); (g@f)(x); (f@@2)(x); #summation and product Sum(i^2, i=1..10); value(%); sum(i^2, i=1..10); Product(1-q^i, i=1..5); value(%); i:='i'; product(1-q^i, i=1..5); #Limits f:=(x^2-4)/(x-2); Limit(f,x=2); value(%); Limit(f,x=3,right);value(%); Limit(f,x=infinity); value(%); #Differentiation f:=sqrt(1-x^2); diff(f,x); diff(tan(x),x$5); z:=exp(x*y)(1+sqrt(x^2+3y^2-x)); diff(z,x); diff(z,x,y)-diff(z,y,x); #Extrema maximize(sin(x)+cos(x)); minimize(x^2-5*x+1,x=0..3); readlib(extrema); f:=2*x^2+y+y^2; g:=x^2+y^2-1; extrema(f,{g},{x,y},'s'); s; #Integration Int(x^2/sqrt(1-x^3),x); value(%); Int(r*exp(-r^2),r=0..infinity); value(%); Int(Int(y*sin(2*x+3*y^2),x),y); %Series y:=1/sqrt(1-x); taylor(y,x=0,5); #Householder's method Hd(f,d,x,x0,N) inputs a polynomial f in x, #a positive integer d which is the order of the Householder's method, #an initial guess x0, #and outputs an approximation of root of f. Hd1:=proc(f,d,x,x0) #x0->x0+d*normal(subs(x=x0,(diff(1/f,x$(d-1)))/subs(x=x0,diff(1/f,x$d))) x0+d*normal(subs(x=x0,(diff(1/f,x$(d-1)))/subs(x=x0,diff(1/f,x$d))): end: #Hd(f,d,x,x0,N): performs N iterations in Householder's of order d Hd:=proc(f,d,x,x0,N) local x1,i: x1:=x0: for i from 1 to N do x1:=Hd1(f,d,x,x1): od: x1: end: #TestHouseholder(f,d,x,x0,eps,N): inputs an expression #f in the variable x, an intial guess x0, a small eps #and a pos. integer N , and outputs the successive errors #abs(xn-alpha) for n from 1 to N where alpha is the #appx. to the root of f(x)=0 found by the bisection method TestHouseholder:=proc(f,d,x,x0,eps,N) local i,L,alpha,x1: #Here (B,f,x,a,b,eps) refers to the bisection method, from C2.txt alpha:=B(f,x,x0-2,x0+2,eps): if alpha=FAIL then RETURN(FAIL): fi: L:=[]: x1:=x0: for i from 1 to N do x1:=Hd1(f,d,x,x1): L:=[ op(L), abs(x1-alpha[1])]: od: L: end: