How to make use of cosine Function to make this shape

program Make_2D_Model
	implicit none
	integer : x,y
        integer:: i,j,nx,nz 
       integer :: idepth
  character*1,allocatable,dimension(:,:) :: icode

nx=1000  
nz=500
idepth=100
 x= !
 y=   !
	allocate(icode(1:nx,1:nz))	
	do i=1,nx
		do j=1,nz
			if(j<=idepth)then
				icode(i,j)='1'
			elseif(j>idepth)then
				icode(i,j)='3'		
            endif
		enddo
    enddo

    do i=1,nx
        do j=1,nz
            if  x= np.arange(0,3*np.pi,0.01)then
                icode(i,j)='2'
            endif
        enddo
    enddo


open(2012,file='model.txt',status='unknown')
		do j=1,nz
			write(2012,1000)(icode(i,j),i=1,nx)
		enddo
	close(2012)
	1000 format(<nx>a1)

    end program Make_2D_Model



Hm, the title of your post is formed as a question, but I do not understand what you want exactly. The first picture might illustrate the result you want but the others are more difficult to understand. Please elaborate on this post.

1 Like

I need to make an irregular surface by using a cosine function

What you could do is use two cosines with periods that have an irrational ratio, like:

z = cos(x) + 0.5 * cos( sqrt(2.0) * x)

That way you get an irregular shape, even though in the long run the two waves can get almost into the same phase.

1 Like
program Circle
	implicit none
	integer :: radius, cx, cy
     integer:: i,j,nx,nz 
     integer :: idepth
 character*1,allocatable,dimension(:,:) :: icode

nx=1000  
nz=500
idepth=100
radius=20
 cx=500 !Center-X
 cy=40  !Center-y
	allocate(icode(1:nx,1:nz))	
	do i=1,nx
		do j=1,nz
			if(j<=idepth)then
				icode(i,j)='1'
			elseif(j>idepth)then
				icode(i,j)='3'		
            endif
		enddo
    enddo

    do i=1,nx
        do j=1,nz
            if(((i-cx)**2+(j-cy)**2)<=radius**2)then
                icode(i,j)='2'
            endif
        enddo
    enddo


open(2012,file='model.txt',status='unknown')
		do j=1,nz
			write(2012,1000)(icode(i,j),i=1,nx)
		enddo
	close(2012)
	1000 format(<nx>a1)

    end program Circle

To obtain a better formatting of your code in the Discourse, you can put your Fortran code between those two lines:

```fortran

and

```

program Make_2D_Model
	implicit none
	integer :: radius, cx, cy
     integer:: i,j,nx,nz 
     integer :: idepth
 character*1,allocatable,dimension(:,:) :: icode

nx=1000  
nz=500
idepth=100
radius=20
 cx=500 !Center-X
 cy=40  !Center-y
	allocate(icode(1:nx,1:nz))	
	do i=1,nx
		do j=1,nz
			if(j<=idepth)then
				icode(i,j)='1'
			elseif(j>idepth)then
				icode(i,j)='3'		
            endif
		enddo
    enddo

    do i=1,nx
        do j=1,nz
            if(((i-cx)**2+(j-cy)**2)<=radius**2)then
                icode(i,j)='2'
            endif
        enddo
    enddo


open(2012,file='model.txt',status='unknown')
		do j=1,nz
			write(2012,1000)(icode(i,j),i=1,nx)
		enddo
	close(2012)
	1000 format(<nx>a1)

    end program Make_2D_Model
    



1 Like