Anda di halaman 1dari 12

CCS Lab Report

Name: Vineet Rao( IIT2009171 ) Khushman Patel( IIT2009172 ) Mukesh Gawadia( IIT2009173 ) Pradeep Kumar( IIT2009174 ) Yogesh Sharma( IIT2009175 )

Find oating point error of reversible function.

#i n c l u d e <s t d i o . h> #i n c l u d e <math . h> i n t main ( ) { do ub le f , output ; i n t input ; f o r ( i n p u t =1; input <500; i n p u t++) { f = pow ( input , 1/100 ) ; output = pow ( f , 100 ) ; p r i n t f ( %f \n , ( inputoutput ) ) ; } return 0; }

Find oating interest.

#i n c l u d e <s t d i o . h> #i n c l u d e <time . h> #i n c l u d e < s t d l i b . h> #d e f i n e COUNTS 100000 i n t main ( ) { l o n g do uble amount , i n t r , r a t e , i n s t a l l m e n t ; int iter ; f r e o p e n ( 1 2 . dat , w , s t d o u t ) ; s c a n f (% Lf ,&amount ) ; s c a n f (% Lf ,& i n s t a l l m e n t ) ; srand ( time ( NULL ) ) ; iter = 0; p r i n t f (%d\ t%Lf \n , i t e r , amount ) ; do { i t e r ++; r a t e = ( l o n g double ) ( rand ( ) % 10000) / 1 0 0 0 . 0 ; i n t r = ( amount r a t e ) / ( 1 2 . 0 1 0 0 . 0 ) ;

amount = amount + i n t r ; p r i n t f (%d\ t%Lf \n , i t e r , amount ) ; amount = amount i n s t a l l m e n t ; i f ( amount < 0 ) { amount = 0 ; } p r i n t f (%d\ t%Lf \n , i t e r , amount ) ; } w h i l e ( amount > 0 && i t e r < COUNTS ) ; f c l o s e ( stdout ) ; system ( g n u p l o t 1 2 . gnu ) ; return 0; }

3
3.1

Plotting fourier transform of curves.


Fourier Series: Half/Full wave rectier

clc clear t =0:.01:10; u=1; f o r t1 =0:.01:10 i f s i n ( t 1 )<0 f ( u )=0; else f ( u)= s i n ( t 1 ) ; end u=u+1; end %f o u r i e r s e r i e s y=1/ p i ; f o r n=0:2 i f n==0 y=y ; 2

e l s e i f n==1 y=y+1/2 s i n ( t ) ; e l s e i f mod( n,2)==0 y=y+(2/ p i ) 1 / ( n21) c o s ( n t ) ; else y=y+0; end end %e r r o r f o r u=1:u1 e ( u)=( f ( u)y ( 1 , u ) ) 2 ; end figure (1); h ol d on ; z=p l o t ( t , f ) ; a x i s ( [ 1 , 1 1 , . 5 , 1 . 5 ] ) s e t ( z , Color , black , LineWidth , 2 )

z=p l o t ( t , y ) ; s e t ( z , Color , green , LineWidth , 2 ) z=p l o t ( t , e ) ; g r i d on ; s e t ( z , Color , red , LineWidth , 2 ) x l a b e l ( time >);

3.2

Fourier Series: Square Wave

clc clear t =0:.01:10; u=1; f o r t1 =0:.01:10 i f mod( t1 , 2 p i )< p i f ( u )=1; else f ( u )=0; end 3

u=u+1; end %f o u r i e r s e r i e s a0 =1/(2 p i ) p i ; y=a0 ; an =0; f o r n=0:3 i f n==0 y=y ; else y=y+an c o s ( n t )+(1/( n p i ))(1 c o s ( n p i ) ) s i n ( n t ) ; end end %e r r o r f o r u=1:u1 e ( u)=( f ( u)y ( 1 , u ) ) 2 ; end figure (1); h ol d on ; z=p l o t ( t , f ) ; a x i s ( [ 1 , 1 1 , . 5 , 1 . 5 ] ) s e t ( z , Color , black , LineWidth , 2 )

z=p l o t ( t , y ) ; s e t ( z , Color , green , LineWidth , 2 )

z=p l o t ( t , e ) ; g r i d on ; s e t ( z , Color , red , LineWidth , 2 ) x l a b e l ( time >);

3.3

Fourier Series: Triangular

clc clear t l a s t =10 p i ; 4

t =0:.01: t l a s t ; u=1; f o r t1 = 0 : . 0 1 : t l a s t i f mod( t1 , 2 p i )< p i /2 f ( u)=mod( t1 , 2 p i ) ; e l s e i f mod( t1 , 2 p i )<3 p i /2 f ( u)= pimod( t1 , 2 p i ) ; else f ( u)=mod( t1 , 2 p i )2 p i ; end u=u+1; end

%f o u r i e r s e r i e s y=0; f o r n=0:1 i f n==0 y=y ; else I 1= (4 s i n ( ( p i n ) / 4 ) 2 p i n s i n ( ( p i n ) / 2 ) ) / ( 2 n 2 ) ; I 2= (4 c o s ( ( p i n ) / 2 ) 3 + 2 p i n s i n ( ( p i n ) / 2 ) c o s ( ( p i n ) / 2 ) 2 4 c o s ( ( p i I 3= ( c o s (2 p i n ) c o s (3/2 n p i ) + ( p i n s i n ( ( 3 p i n ) / 2 ) ) / 2 ) / n 2 ; an= 1/ p i ( I 1+I 2+I 3 ) ; I 4= I 5= I 6= bn=

( s i n ( ( p i n ) / 2 ) ( p i n c o s ( ( p i n ) / 2 ) ) / 2 ) / n 2 ; (4 c o s ( ( p i n ) / 2 ) 2 s i n ( ( p i n ) / 2 ) 2 s i n ( ( p i n ) / 2 ) + n ( p i c o s ( ( p i n ) / ( s i n ( ( 3 p i n ) / 2 ) s i n (2 n p i ) + ( p i n c o s ( ( 3 p i n ) / 2 ) ) / 2 ) / n 2 ; 1/ p i ( I 4+I 5+I 6 ) ;

y=y+an c o s (2 p i n t /(2 p i ))+bn s i n (2 p i n t /(2 p i ) ) ; end end %e r r o r f o r u=1:u1 e ( u)=( f ( u)y ( 1 , u ) ) 2 ; end figure (1); h ol d on ; z=p l o t ( t , f ) ; a x i s ([ 1 , t l a s t +1,pi , p i ] ) s e t ( z , Color , black , LineWidth , 2 ) z=p l o t ( t , y ) ; s e t ( z , Color , green , LineWidth , 2 ) z=p l o t ( t , e ) ; g r i d on ; s e t ( z , Color , red , LineWidth , 2 ) x l a b e l ( time >);

3.4
clc clear

Fourier Series: Sawtooth

t= 0 : . 0 1 : 1 0 p i ; f=mod( t , 2 p i ) ; %f o u r i e r s e r i e s y= p i ; f o r n=0:4 i f n==0 y=y ; else an= ( 1 / p i ) (2 s i n ( p i n ) 2 2 p i n s i n (2 p i n ) ) / n 2 ; bn= ( 1 / p i ) ( s i n (2 p i n ) 2 p i n c o s (2 p i n ) ) / n 2 ; y= y+an c o s ( 2 p i n t /(2 p i ) ) + bn s i n ( 2 p i n t /(2 p i ) ) ; end end

%e r r o r f o r u =1:3142 e ( u)=( f ( u)y ( 1 , u ) ) 2 ; end figure (1); h ol d on ; z=p l o t ( t , f ) ; a x i s ([ 1 ,10 p i +1 , 5 ,10]); s e t ( z , Color , black , LineWidth , 2 )

z=p l o t ( t , y ) ; s e t ( z , Color , green , LineWidth , 2 )

z=p l o t ( t , e );% g r i d on ; s e t ( z , Color , red , LineWidth , 2 ) 6

x l a b e l ( time >);

3.5

Fourier Transform: Full wave/Half wave rectied pulse

clc clear f 0 =5; t=f 0 p i : . 0 0 1 : f 0 p i ; u=1; f o r t 1=f 0 p i : . 0 0 1 : f 0 p i i f t1>=0 && t1<=p i f ( u)= s i n ( t 1 ) ; ; else f ( u )=0; end u=u+1; end %f o u r i e r t r a n s f o r m w1=f 0 p i : . 0 1 : f 0 p i ; u=1; f o r w=f 0 p i : . 0 1 : f 0 p i %F( u)=(1/exp ( p i j w) 1 ) / ( j w ) ; F( u)=(1/ exp ( p i j w) + 1 ) / ( ( j w) 2 + 1 ) ; u=u+1; end figure (1); h ol d on ; z=p l o t ( t , f ) ; g r i d on s e t ( z , Color , black , LineWidth , 2 ) z=p l o t (w1 , F ) ; s e t ( z , Color , green , LineWidth , 2 )

3.6

Fourier Transform: Square Pulse

clc clear f 0 =5; t=f 0 p i : . 0 0 1 : f 0 p i ; u=1; f o r t 1=f 0 p i : . 0 0 1 : f 0 p i i f t1>=0 && t1<=p i f ( u )=1; else f ( u )=0; end u=u+1; end %f o u r i e r t r a n s f o r m

w1=f 0 p i : . 0 1 : f 0 p i ; u=1; f o r w=f 0 p i : . 0 1 : f 0 p i F( u)=(1/exp ( p i j w) 1 ) / ( j w ) ; u=u+1; end figure (1); h ol d on ; z=p l o t ( t , f ) ; g r i d on s e t ( z , Color , black , LineWidth , 2 ) z=p l o t (w1 , F ) ; s e t ( z , Color , green , LineWidth , 2 )

3.7

Fourier Transform: Triangular

clc clear f 0 =2; t=2f 0 p i : . 0 1 : 2 f 0 p i ; u=1; f o r t 1=2f 0 p i : . 0 1 : 2 f 0 p i i f t1>=0 && t1<=p i /2 f ( u)= t 1 ; e l s e i f t1>=p i /2 && t1 <=3p i /2 f ( u)= pit 1 ; e l s e i f t1 >=3p i /2 && t1 <=2p i f ( u)=t1 2 p i ; else f ( u )=0; end u=u+1; end %f o u r i e r t r a n s f o r m %syms t ; %syms jw ; %i n t ( t exp(jw t ) , 0 , 2 p i ) w1=2p i f 0 : . 0 1 : 2 p i f 0 ; u=1; f o r w=2p i f 0 : . 0 1 : 2 p i f 0 F1=1/( j w) 2 ( ( p i j w)/ 2 + 1 ) / ( ( j w)2 exp ( ( p i j w ) / 2 ) ) ; F2=( p i j w exp ( ( p i j w) / 2 ) 2 exp ( ( p i j w) / 2 ) + 2 ) / ( 2 ( j w)2 exp ( p i j w ) ) ; F3=( p i j w 2 exp ( ( p i j w) / 2 ) + 2 ) / ( 2 ( j w)2 exp ( ( 3 p i j w ) / 2 ) ) ; F4=(p i j w exp ( ( p i j w) / 2 ) 2 exp ( ( p i j w) / 2 ) + 2 ) / ( 2 ( j w)2 exp (2 p i j w ) ) ; F( u)=F1+F2+F3+F4 ; u=u+1; end 9

figure (1); hol d on ; z=p l o t ( t , f ) ; g r i d on s e t ( z , Color , black , LineWidth , 2 ) z=p l o t (w1 , F ) ; s e t ( z , Color , green , LineWidth , 2 )

3.8
clc clear

Fourier Transform: Sawtooth

f 0 =3; t= f 0 p i : . 0 1 : f 0 p i ; u=1; f o r t 1=f 0 p i : . 0 1 : f 0 p i i f t1>=0 && t1 <=2p i f ( u)=mod( t1 , 2 p i ) ; else f ( u )=0; end u=u+1; end %f o u r i e r t r a n s f o r m %syms t ; %syms jw ; %i n t ( t exp(jw t ) , 0 , 2 p i ) w1=f 0 p i : . 0 1 : f 0 p i ; u=1; f o r w=f 0 p i : . 0 1 : f 0 p i F( u)=1/( j w) 2 (2 p i j w + 1 ) / ( ( j w)2 exp (2 p i j w ) ) ; u=u+1; end

10

figure (1); hol d on ; z=p l o t ( t , f ) ; s e t ( z , Color , black , LineWidth , 2 ) z=p l o t (w1 , F ) ; s e t ( z , Color , green , LineWidth , 2 )

Floating point error in loops.

#i n c l u d e <s t d i o . h> #i n c l u d e <math . h> #i n c l u d e < s t d l i b . h> i n t main ( ) { do ub le a ; int i ; f r e o p e n ( 1 4 . dat , w , s t d o u t ) ; a =1.0/5.0; f o r ( i =0; i <=100; i ++){ p r i n t f (%d %f \n , i , apow ( 2 . 0 , i )pow ( 2 . 0 , i ) / 5 . 0 ) ; } f c l o s e ( stdout ) ; system ( g n u p l o t 1 4 . gnu ) ; return 0; }

11

12