Skip to main content

Posts

Showing posts from July, 2018

To solve Poison equation in 2D

PROGRAM Poisson_equation_in_2D parameter (kx= 40 ,ky= 40 ) integer ::i,j,k real ::rho(kx,ky),V(kx,ky),R(kx,ky),d open ( 1 , file ='poisson input', status ='unknown') open ( 2 , file ='poisson output', status ='unknown') read ( 1 ,*)((rho(i,j),j= 1 ,ky),i= 1 ,kx) k= 0 d= 0 . 025 do i= 1 ,kx do j= 1 ,ky V(i,j)= 0 . 0 enddo enddo 4 do i= 2 ,kx- 1 do j= 2 ,ky- 1 R(i,j)=V(i+ 1 ,j)+V(i- 1 ,j)+V(i,j+ 1 )+V(i,j- 1 )- 4 *V(i,j)+rho(i,j)*d** 2 enddo enddo do i= 2 ,kx- 1 do j= 2 ,ky- 1 if ( abs (R(i,j))> 0 . 0000001 ) goto 3 enddo enddo goto 5 3 k=k+ 1 do i= 2 ,kx- 1 do j= 2 ,ky- 1 V(i,j)=V(i,j)+ 0 . 25 *R(i,j) enddo enddo goto 4 5 write (*, 6 )k do i= 1 ,kx+ 1 do j= 1 ,ky+ 1 write ( 2 , 7 )i,j,V(i,j) enddo enddo 6 format ('no of Iteration=',i5) 7 format (i2,1x,i2,1x,f7. 5 ) end

To solve Laplace equation in 2D

PROGRAM Laplace_equation_in_2D parameter (kx= 6 ,ky= 6 ) integer ::i,j,n,k real , dimension ( 6 , 6 )::a,a1,err open ( 1 , file ='laplace_input', status ='unknown') open ( 2 , file ='laplace_output', status ='unknown') read ( 1 ,*)((a(i,j),j= 1 ,ky),i= 1 ,kx) k= 0 n= 0 3 do i= 2 ,kx- 1 do j= 2 ,ky- 1 a1(i,j)= 0 . 25 *(a(i+ 1 ,j)+a(i- 1 ,j)+a(i,j+ 1 )+a(i,j- 1 )) enddo enddo do i= 2 ,kx- 1 do j= 2 ,ky- 1 err(i,j)=a(i,j)-a1(i,j) a(i,j)=a1(i,j) if ( abs (err(i,j))<= 0 . 00001 )n=n+ 1 if (n==(kx- 1 )*(ky- 1 )) goto 11 enddo enddo k=k+ 1 goto 3 11 write ( 2 , 4 )k write ( 2 , 5 )((a(i,j),j= 1 , 6 ),i= 1 , 6 ) 4 format ('Number of Iteration=',i3) 5 format ( 6 (f6. 3 ,1x)) end

Heat conduction equation solution with Implicit Scheme

PROGRAM Heat_conduction_with_Implicit_Scheme parameter (kx= 8 ,ky= 8 ,kxx= 10 ) real ::T(kxx),T1(kxx),a1(kx,kx),b1(kx),dt,dx,k,c integer ::i,j, count open ( 1 , file ='input_gauss') open ( 2 , file ='heat_implicit.dat') T( 1 )= 100 . 0 T(kxx)= 100 . 0 dt= 10 ;dx= 0 . 1 ;k=. 004 c=k*dt/dx** 2 count = 0 do i= 2 ,kxx- 1 T(i)= 27 . 0 enddo do i= 1 ,kx do j= 1 ,kx if (i==j) then a1(i,j)= 1 . 0 + 2 *c else if (j==i+ 1 .or. i==j+ 1 ) then a1(i,j)=-c else a1(i,j)= 0 endif endif enddo enddo do i= 1 ,kx write ( 1 ,*)(a1(i,j),j= 1 ,kx) enddo close ( 1 ) 5 count = count + 1 do i= 1 ,kx if (i== 1 ) b1(i)=T(i+ 1 )+c*T(i) if (i==kx) b1(i)=T(i+ 1 )+c*T(i+ 2 ) if (i /= 1 .and. i/=kx) b1(i)=T(i+ 1 ) enddo write ( 1 ,*)(b1(i),i= 1 ,kx) call gauss(b1,kx,T1) do i= 1 ,kx if ( abs (T1(i)-T(i+ 1 )) > 0 . 001 ) then do j= 1 ,kx T(j+ 1 )=T1(j) enddo write ( 2 ,*)dt

To Calculate the half life of a radio active substance

/*To Calculate the half life of a radio active substance*/ #include <math.h> #include <stdio.h> int main() { float t, lambda, N, N_0, T; printf( "The value of present time, t: " ); scanf( "%f" , &t); printf( "The # of radioactive atoms in that time: " ); scanf( "%f" , &N); printf( "The initial Number of radioactive atoms: " ); scanf( "%f" , &N_0); lambda=(1/t)*log(N_0/N); T=0.693/lambda; printf( "\nThe half-life of the radioactive atom, T: %f\n" ,T); return (0); }

Assignment 9

program ass9 integer ::i real ::s,a s= 0 do i= 1 , 7 call fact(a,i) s=s+( 1 /a) enddo print *,'The sum of the series is:',s end program subroutine fact(prod,n) integer ::n real ::prod prod= 1 do i= 1 ,n prod=prod*i enddo return end

Assignment 8

program assignment_8 real ::s,t,x integer ::i,n open ( 1 , file ='1st.txt', status ='unknown') open ( 2 , file ='2nd.txt', status ='unknown') open ( 3 , file ='3rd.txt', status ='unknown') open ( 4 , file ='4th.txt', status ='unknown') do n= 1 , 4   if (n== 1 ) then     x=- 0 . 1     1 x=x+ 0 . 1     s=x     write (n,*)x,s     if (x< 3 . 4 ) goto 1   elseif (n== 2 ) then     x=- 0 . 1     2 x=x+ 0 . 1     t=x     s=x     do i= 1 ,(n- 1 )     t=-t*x*x/( 2 *i*( 2 *i+ 1 ))     s=s+t     enddo     write (n,*)x,s     if (x< 3 . 4 ) goto 2    elseif (n== 3 ) then      x=- 0 . 1      3 x=x+ 0 . 1      t=x      s=x      do i= 1 ,(n- 1 )      t=-t*x*x/( 2 *i*( 2 *i+ 1 ))      s=s+t      enddo      write (n,*)x,s      if (x< 3 . 4 ) goto 3    else      x=- 0 . 1      4 x=x+ 0 . 1      s=sin(x)      write (n,*)x,s      if (x< 3 . 4 ) goto 4    en

Problem sheet problem 6

/*Problem 6*/ #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { float sum=0,a[100]; int i,j,N; for (j=1;j<=3;j++) { printf( "Enter the value of N:" ); scanf( "%d" ,&N); for (i=1;i<=N;i++) { /*Generating floating random numbers [0,1]*/ float x=( float )rand()/( float )(RAND_MAX/1); a[i]=x; sum=sum+a[i]; } printf( "The sum of the array is=%.2f\n" ,sum); } return (0); }

Problem sheet problem 5

/*Problem 5*/ #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { float x,y,a[100],b[100],temp; int N,i,j; for (j=1;j<=4;j++) { printf( "Enter the value of N:" ); scanf( "%d" ,&N); for (i=1;i<=N;i++) { /*Generating floating random numbers [0,4]*/ float x=( float )rand()/( float )(RAND_MAX/4); x=x-2; y=x*x*x-3*x+1; printf( "%f %f\n" ,x,y); a[i]=y; b[i]=y; } /*Sorting for Maximum*/ for (i=1;i<=N;i++) { if (a[i]>a[i+1]) { temp=a[i]; a[i+1]=temp; } else { temp=a[i+1]; } } printf( "\nMaximum value of y=%f\n" ,temp); /*Sorting for Minimum*/ for (i=1;i<=N;i++) { if (b[i]>b[i+1]) { temp=b[i+1]; } else { temp=b[i]; b[i+1]=temp; } } printf( "\nMinimum value of y=%f\n" ,temp); } return (0); }

Problem sheet problem 4

/* To find Trace and subtraction of two matrices */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m; float a[20][20], b[20][20], c[20][20], d[20][20],sum=0,sum1=0,sum2=0,sum3=0; printf( "Enter order of the matrices:\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the elements of matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%f" , &a[i][j]); printf( "\n" ); } printf( "Entered Matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10f" ,a[i][j]); printf( "\n" ); } printf( "\nEnter the elements of matrix B:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%f" , &b[i][j]); printf( "\n" ); } printf( "Entered Matrix B:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10f" ,b[i][j]); printf( "\n" ); } for (i=

Problem sheet problem 3

/*Solution of a quadratic equation*/ #include <math.h> #include <stdio.h> int main() { float a, b, c, d, x1, x2, rp, imp; int i; for (i=1;i<=3;i++) { printf( "Enter the values of a:" ); scanf( "%f" , &a); printf( "Enter the values of b:" ); scanf( "%f" , &b); printf( "Enter the values of c:" ); scanf( "%f" , &c); d=b*b-4*a*c; x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); if (x1==x2) { printf( "\n The roots are equal" ); printf( "\n The root is: %.3f\n\n" , x1); } else { printf( "\n The roots are not equal" ); printf( "\n The 1st root is: %.3f" , x1); printf( "\n The 2nd root is: %.3f\n\n" , x2); } } return (0); }

Problem sheet problem 2

/*Problem 2*/ #include <stdio.h> #include <math.h> int main() { float base,side,perimeter,median,s,area,a[100],b[100],temp=0; int i; for (i=1;i<=3;i++) { printf( "Enter the value of base and any other side:" ); scanf( "%f%f" ,&base,&side); perimeter=base+2*side; median=sqrt(side*side-(base*base/4)); area=0.5*base*median; a[i]=area; b[i]=perimeter; printf( "Perimeter of the triangle=%f" ,perimeter); printf( "\nMedian of the triangle=%f" ,median); printf( "\nArea of the triangle=%f\n" ,area); } /*Sorting for Maximum*/ for (i=1;i<=2;i++) { if (a[i]>a[i+1]) { temp=a[i]; a[i+1]=temp; } else { temp=a[i+1]; } } printf( "\nMaximum Area of the triangles=%f\n" ,temp); /*Sorting for Minimum*/ for (i=1;i<=2;i++) { if (b[i]>b[i+1]) { temp=b[i+1]; } else { temp=b[i]; b[i+1]=temp; } } printf( "\nMinimum Perimeter of the triangles=%f\n" ,temp);

Problem sheet problem 1

/*Problem 1*/ #include <stdio.h> #include <math.h> int main() { float sum=0,term,sum1,rel_err,a[100]; int N,r,i; for (i=1;i<=5;i++) { printf( "Enter the value of an integer N:" ); scanf( "%d" ,&N); for (r=1;r<=N;r++) { term=r/((r*r*r)-(r*r)+1.0); sum=sum+term; } a[i]=sum; sum=0; printf( "The sum is=%.3f\n" ,a[i]); } for (i=1;i<=4;i++) { rel_err=(a[i+1]-a[1])*100.0/a[i]; printf( "The Relative Error is=%.3f\n" ,rel_err); } return (0); }

To find transpose of a given matrix

program transpose integer ::i,j,a( 10 , 10 ),m,n,b( 10 , 10 ) print *, "Enter the order of the matrix:" read (*,*)m,n print *,'Enter the matrix:' do i= 1 ,m read *,(a(i,j),j= 1 ,n) enddo print *, "The entered matrix is:" do i= 1 ,m print *,(a(i,j),j= 1 ,n) enddo !to determine transpose of entered matrix do i= 1 ,m do j= 1 ,n b(j,i)=a(i,j) enddo enddo print *, "The transpose of the entered matrix is:" do i= 1 ,n print *,(b(i,j),j= 1 ,m) enddo end program

To find the values of Sinx for a given degree using subroutine

program sinx_using_subroutine real * 16 :: sum ,x,a integer ::i,n print *,'Enter the angle:-' read *,x print *,'Enter the no of countable steps:-' read *,n !converting the angle from degree to radian x=x* 3 . 14159 / 180 sum = 0 j= 0 do i= 1 ,n+ 1 , 2 j=j+ 1 call f(i,a) sum = sum +((- 1 )**(j+ 1 ))*x**i/a enddo print *,'Sin',x* 180 / 3 . 14159 ,'=', sum end program !subroutine of factorial subroutine f(n,prod) integer ::n real * 16 ::prod prod= 1 do i= 1 ,n prod=prod*i enddo return end

To find pressure distribution with height from the sea level using Euler method

program ode1 real ::rho,g,p !defining rho=density, p=pressure, g=gravity f(p,z)=-rho*g print *, "Enter the stepsize:" read (*,*)h open ( 1 , file ='a.dat', status ='unknown') !assuming the pre values z= 0 ;p= 10000 ;g= 9 . 8 !iteration starts 1 write ( 1 ,*)z,p rho= 1 . 276 *exp(z/ 8000 ) p=p+h*f(p,z) z=z+h if (z<= 16000 ) goto 1   end

To find temperature distribution from Heat conduction equation with FTCS Scheme

PROGRAM Heat_conduction_with_FTCS_Scheme !defining kx=x position, ky=time, k=conductivity !dx,dt=step size parameter (kx= 10 ,ky= 100 ) real ::T(kx,ky),dt,dx,k,c integer ::n,i open ( 1 , file ='heat_FTCS.out') !assuming the temperature of the two outer faces are 100K do i= 1 ,ky T( 1 ,i)= 100 . 0 T(kx,i)= 100 . 0 enddo !defining parameters dt= 1 ;dx= 0 . 01 ;k=. 004 c=k*dt/dx !to satisfy CFL criteria if (c> 0 . 5 ) stop !assuming the temperature of the inner points are 27K     do j= 1 ,ky do i= 2 ,kx- 1 T(i,j)= 27 . 0 enddo enddo do j= 1 ,ky- 1 do i= 2 ,kx- 1 T(i,j+ 1 )=T(i,j)+c*(T(i+ 1 ,j)- 2 *T(i,j)+T(i- 1 ,j)) enddo write ( 1 , 2 )(T(i,j+ 1 ),i= 1 ,kx) enddo 2 format ( 10 (f5. 1 ,2x)) END PROGRAM

To find roots of an equation using Bisection Method

program bisection real ::a,b,c print *,'Enter two initial guess values:-' read (*,*)a,b if ( abs (f(a))< 0 . 00001 ) then print *,'The root is =',a elseif ( abs (f(b))< 0 . 00001 ) then print *,'The root is =',b elseif (f(a)*f(b)< 0 . 00001 ) then 1 c=(a+b)/ 2 if (f(a)*f(c)< 0 . 00001 ) then b=c else a=c endif if ( abs (f(c))> 0 . 00001 ) then go to 1 else print *,'The root is =',c endif else print *,'Change the initial guess.' endif end !define your function below function f(x) real ::x f=x*x*x+ 2 *x*x- 5 *x- 6 return end function

To multiply two matrices

program matrix_multiplication integer ::i,j,k,m,n,p real ::a( 10 , 10 ),b( 10 , 10 ),c( 10 , 10 ) print *,'Enter the order of matrix A:-' read *,m,n print *,'Enter the matrix A:-' do i= 1 ,m read (*,*)(a(i,j),j= 1 ,n) enddo print *,'Enter the order of matrix B:-' read *,n,p print *,'Enter the matrix B:-' do i= 1 ,n read (*,*)(b(i,j),j= 1 ,p) enddo !performing matrix multiplication do i= 1 ,m do j= 1 ,p c(i,j)= 0 do k= 1 ,n c(i,j)=c(i,j)+a(i,k)*b(k,j) enddo enddo enddo print *, "The multiplied matrix is:" do i= 1 ,m write (*,*)(c(i,j),j= 1 ,p) enddo end

To determine variation of current with time in series DC LR circuit

program LRcktSeries real ::t,v,i,l,r,h !defining v=voltage,i=current,r=resistance,l=inductance,t=time !defining di/dt=f(i,t) f(i,t)=(v-i*r)/l print *, "Enter the smallest incriment" read *,h open ( 1 , file ='lcr.dat', status ='unknown') !assuming initial values as r= 2 ;l= 0 . 4 ;v= 10 ;t= 0 ;i= 0 !iteration starts and stops at t=100 1 write ( 1 ,*)t,i i=i+h*f(i,t) t=t+h if (t<= 100 ) goto 1 end

Gauss Jordan method to find the inverse of a matrix

PROGRAM Gauss_Jordan !Gauss Jordan method to find the inverse of a matrix !The order of the matrix is n. !The coefficient matrix=a(i,j); i,j=1,2,3,...,n integer ::n,i,j,k,m real :: a( 10 , 10 ),r,t open ( 4 , file ='output.dat') print *, "Enter the order of the matrix:" read (*,*)n print *, "Enter the matrix:" do i= 1 ,n read (*,*)(a(i,j),j= 1 ,n) enddo write ( 4 ,*) "A=" do i= 1 ,n write ( 4 ,*)(a(i,j),j= 1 ,n) enddo write ( 4 ,*) " " do k= 1 ,n do m= 1 ,n if (m==k) then a(m,n+ 1 )= 1 . 0 else a(m,n+ 1 )= 0 . 0 endif enddo write ( 4 ,*) "aug=" ,k do i= 1 ,n write ( 4 ,*) " " ,(a(i,j),j= 1 ,n+ 1 ) enddo write ( 4 ,*) " " do i= 1 ,n if (i/=k) then r=a(i, 1 )/a(k, 1 ) do j= 1 ,n a(i,j)=a(i,j+ 1 )-a(k,j+ 1 )*r enddo endif enddo t=a(k, 1 ) do m= 1 ,n a(k,m)=a(k,m+ 1 )/t enddo enddo write ( 4 ,*) " " write ( 4 ,*) "inverse

Addition of two matrices

program matrix_addition integer ::a( 10 , 10 ),b( 10 , 10 ),c( 10 , 10 ),m,n print *, "Enter the order of two matrices:" read (*,*)m,n print *, "Enter the matrix A:" do i= 1 ,m read (*,*)(a(i,j),j= 1 ,n) enddo print *, "Enter the matrix B:" do i= 1 ,m read (*,*)(b(i,j),j= 1 ,n) enddo !performing matrix addition do i= 1 ,m do j= 1 ,n c(i,j)=a(i,j)+b(i,j) enddo enddo print *, "The sum of two entered matrices is:" do i= 1 ,m write (*,*)(c(i,j),j= 1 ,n) enddo end

To evaluate the value of y=sinx+cos3x

/*To evaluate the value of y=sinx+cos3x*/ #include <math.h> #include <stdio.h> int main () { float x, y; printf( "Enter the value of x in degree: " ); scanf( "%f" , &x); /*multiplying x by 0.0174533 to convert it from degree to radian*/ y=sin(x*.0174533)+cos(3*x*.0174533); printf( "\n The required value of y is: %f\n" ,y); return (0); }

To determine a) (Z_1+Z_2)/(Z_1-Z_2) , b) Z_1/Z_2 for two given sets of complex numbers Z_1 & Z_2

/* To determine a) (Z_1+Z_2)/(Z_1-Z_2) , b) Z_1/Z_2 for two given sets of complex numbers */ #include <stdio.h> #include <math.h> int main() { float a1, b1, a2, b2, Z_1plusZ_2real, Z_1plusZ_2imp, Real, Imp; float Z_1byZ_2real, Z_1byZ_2imp, Z_1minusZ_2real, Z_1minusZ_2imp; printf( "Enter the value of Z_1:\n" ); scanf( "%f%f" , &a1, &b1); printf( "Enter the value of Z_2:\n" ); scanf( "%f%f" , &a2, &b2); Z_1plusZ_2real=a1+a2; Z_1plusZ_2imp=b1+b2; Z_1minusZ_2real=a1-a2; Z_1minusZ_2imp=b1-b2; printf( "\nZ_1=%f+i(%f)" , a1, b1); printf( "\n\nZ_2=%f+i(%f)" , a2, b2); printf( "\n\nZ_1+Z_2=%f+i(%f)" , Z_1plusZ_2real, Z_1plusZ_2imp); printf( "\n\nZ_1-Z_2=%f+i(%f)" , Z_1minusZ_2real, Z_1minusZ_2imp); Z_1byZ_2real=(a1*a2+b1*b2)/(a2*a2+b2*b2); Z_1byZ_2imp=(a2*b1-a1*b2)/(a2*a2+b2*b2); printf( "\n\nZ_1/Z_2real=%f" ,Z_1byZ_2real); printf

To verify the identity trace AB = trace BA for two matrices

/* To verify trace AB = trace BA */ #include <stdio.h> #include <math.h> int main() { int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],sum=0,sum1=0,g[20][20]; printf( "Enter the order of the matrix A:\n" ); scanf( "%d%d" , &m ,&n); printf( "Enter the order of the matrix B:\n" ); scanf( "%d%d" , &n,&p); if (m==p) { printf( "Enter the elements of the matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%d" , &a[i][j]); printf( "\n" ); } printf( "Enter the elements of the matrix B:\n" ); for (i=0;i<n;i++) { for (j=0;j<p;j++) scanf( "%d" , &b[i][j]); printf( "\n" ); } /*matrix multiplication AB*/ for (i=0;i<m;i++) { for (j=0;j<p;j++) { e[i][j]=0; for (k=0;k<n;k++) e[i][j]=e[i][j]+a[i][k]*b[k][j]; } } printf( "Product matrix AB is:

To check the Orthogonality test for a matrix

/* Orthogonality test */ #include <stdio.h> #include <math.h> int main() { int matrix[10][10], transpose[10][10], e[20] [20]; int m, n, c, r, k,flag=1; printf( "Enter the number of rows and columns of matrix:\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the %d elements of matrix:\n" , m*n); for (r=0;r<m;r++) { for (c=0;c<n;c++) { printf( "\nEnter value in %dth row and %dth column:" , r+1, c+1); scanf( "%d" , &matrix[r][c]); } } printf( "your matrix:\n" ); for (r=0;r<m;r++) { for (c=0;c<n;c++) { printf( "%10d" , matrix[r][c]); } printf( "\n\n" ); } /*To determine transpose of entered matrix*/ for (r=0;r<m;r++) { for (c=0;c<n;c++) { transpose[c][r]=matrix[r][c]; } } printf( "Transpose of entered matrix :\n" ); for (r=0;r<n;r++) { for (c=0;c<m;c++) {

To prove the identity Transpose(AB)=Transpose B x Transpose A

/* To prove Transpose(AB)=Transpose B x Transpose A */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m, p, k, a[20] [20], b[20] [20], e[20] [20], flag=0; int transpose[20][20], at[20] [20], bt[20] [20], atbt[20] [20]; printf( "Enter order of the matrix A:\n" ); scanf( "%d%d" , &m, &n); printf( "Enter order of the matrix B:\n" ); scanf( "%d%d" , &n, &p); printf( "Enter the elements of matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%d" , &a[i] [j]); printf( "\n" ); }     printf( "Entered Matrix A:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,a[i] [j]); printf( "\n" ); } /*To determine transpose of matrix A*/ for (i=0;i<m;i++) { for (j=0;j<n;j++) { at[j][i]=a[i][j]; } } printf( "Transpose of matrix A:\n&qu