001 x1=-4;x2=4;y1=-4;y2=4
002
003
004 function f(x){-(x^2)+3}
005 function g(x){x^2-2*x-1}
006
007 x=x1;N=40
008 step=(x2-x1)/N
009 gstdc(0,10,159,100)
010 gaxis(x1,y1,x2,y2,1,1)
011 gtitl('Calculate roots and centroid')
012 gmove(x,f(x))
013 gmove2(x,g(x))
014 while(x<=x2)
015 {
016 glin(x,f(x))
017 glin2(x,g(x))
018 x=x+step
019 }
020
021 // Find the roots.
022 function s(x){f(x)-g(x)}
023 x1=root('s',x1)
024 x2=root('s',x2)
025
026
027 fmt(0,2,1,0)// Set display format
028 gprt(2,122,'P1=('+x1+','+f(x1)+')')
029 gcprt(90,122,'P2=('+x2+','+f(x2)+')')
030
031 // Draw area.
032 x=x1
033 step=(x2-x1)/N
034 gline3(x,g(x),x,f(x))
035 while(x<=x2)
036 {
037 gline3(x,g(x),x,f(x))
038 x=x+step
039 }
040
041 // Calculate area
042 A=int('s',x1,x2)
043 gcprt(140,122,'A='+A)
044
045 function my(x){x*s(x)}
046 py=int('my',x1,x2)
047
048 function mx(x){f(x)^2-g(x)^2}
049 px=int('mx',x1,x2)
050
051 xCen=(0.5*px)/A
052 yCen=py/A
053 gpnt(1,xCen,yCen,2)
054
055 gcprt(80,135,'Centroid= ('+xCen+','+yCen+')')
|