Wednesday, December 7, 2011

mandelbrot C code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 150
#define ZOOM 800.0
int main()
{
FILE *fp;fp=fopen("c:\\mandelbrot.txt","w");
int i,x,y;
double re;//x
double im;//y
double tx,ty; // (x + iy)
const double x0=0.5,y0=-0.000397;


fprintf(fp,"%.6f + %.6fi\n",x0,y0);
for(y=MAX;y>=-MAX;y--)
for(x=-MAX;x<=MAX;x++)
{
re=x/ZOOM+0.6;
im=y/ZOOM+0.5;

  for(i=0;i<15;i++)
  {
  tx=re;ty=im;
  re=tx*tx-ty*ty; //x²-y²
  im=2.0*tx*ty; // 2xy
  re+=x0;
  im+=y0;
  }

if(re>0.01)
  fprintf(fp," ");
else
  fprintf(fp,"O");

if(x==MAX)
  fprintf(fp,"\n");
}


}

Friday, April 15, 2011

Some interesting Mathematica plots

Copy paste all into a Mathematica notebook and evaluate.


ParametricPlot[{t*E^Sin[t], t*E^Cos[t]}, {t, -100, 100}]

ParametricPlot[{t*Sin[t], Sin[t]}, {t, -10, 10}]

ParametricPlot[{(t*Sin[t]), (t*Cos[t])}, {t, -100, 100}]

ParametricPlot[{Sin[t] + Sin[2 t], Sin[2 t] + Sin[3 t]}, {t, -10, 10}]

ParametricPlot[{Sin[4 t], Sin[5 t]}, {t, -10, 10}]

ParametricPlot[{Sin[8 t] + Sin[9 t], Sin[9 t] + Sin[10 t]}, {t, -10,
  10}]

ParametricPlot[{t*Sin[t], t*Sin[t] + Sin[20 t]}, {t, -10, 10}]

ParametricPlot[{(Sin[
     t]*(E^Cos[t] - 2 Cos[4 t] - (Sin[t/12])^5)), (Cos[
     t]*(E^Cos[t] - 2 Cos[4 t] - (Sin[t/12])^5))}, {t, -150, 150}]

ParametricPlot[{Sin[t]*Cos[t] Log[Abs[t]],
  Sqrt[Abs[t]] (Cos[t])}, {t, -100, 100}]

ParametricPlot[{Sin[t]*Cos[t] Log[Abs[t]],
  Sqrt[Abs[t]] (Cos[t])}, {t, -1, 1}]

ParametricPlot[{Sin[t]*Cos[t] Log[Abs[t]],
  Sqrt[Abs[t]] (Cos[t])^3}, {t, -100, 100}]

ParametricPlot[{Sin[t]*Cos[t] Log[Abs[t]],
  Sqrt[Abs[t]] (Cos[t])^E}, {t, -100, 100}]

ParametricPlot[{t*Abs[Sin[t]], t*Abs[(Cos[t])]}, {t, -50, 50}]

PolarPlot[Sin[5 t], {t, 0, 2 Pi}]

PolarPlot[t, {t, 0, 5 Pi}]

PolarPlot[1 + 1/10 Sin[25 t], {t, 0, 2 Pi}]

PolarPlot[Sin[100 t] + Sin[3 t], {t, 0, 5 Pi}]

PolarPlot[Sin[100 t] + Sin[25 t], {t, 0, 5 Pi}]

PolarPlot[t^2, {t, 0, 100 Pi}]


Wednesday, April 6, 2011

C language cellular automata code-(rule 30)

  • 1D cellular automata Written using the very basics of C so it is easily readable.
  • Generates rule 30(you can find all rules generator here)
  • Creates a text file in the location C:\cellular_automata_rule_specific.txt
  • Open that text file using any text viewer(notepad is good enough) and set to a lower font size and see the automata patterns
Of course the code looks too long. That's because i was using the very basics of the C language


#include <stdio.h>
#include <stdlib.h>
#define COLUMNS 300
#define ITERATIONS 100
#define SEED 150

int main()
{
    char F='O',B=' ';
    int y[8]= {0,0,0,1,1,1,1,0}; /*rule is set to popular "rule 30"*/
    int x[COLUMNS],z[COLUMNS];
    int r,j,k;
    FILE *fp;
    fp=fopen("c:\\cellular_automata_rule_specific.txt","w");

    fprintf(fp,"Set font size to 1 on your text viewer to see the larger picture\n");
    fprintf(fp,"If you are using notepad click Format->font and change the font size\n\n\n");

    fprintf(fp,"rule  %d%d%d%d%d%d%d%d\n ",y[0],y[1],y[2],y[3],y[4],y[5],y[6],y[7]);
    for(j=0; j<COLUMNS; j++)
    {
        x[j]=0;
        z[j]=0;
    }
    x[SEED]=1;


    for(k=0; k<ITERATIONS; k++)
    {
        x[0]=x[COLUMNS-1]=0;
        for(r=1; r<(COLUMNS-1); r++)
        {
            if( (x[r-1]==0)&&(x[r]==0)&&(x[r+1]==0) )
                z[r]=y[7];
            else if( (x[r-1]==0)&&(x[r]==0)&&(x[r+1]==1) )
                z[r]=y[6];
            else if( (x[r-1]==0)&&(x[r]==1)&&(x[r+1]==0) )
                z[r]=y[5];
            else if( (x[r-1]==0)&&(x[r]==1)&&(x[r+1]==1) )
                z[r]=y[4];
            else if( (x[r-1]==1)&&(x[r]==0)&&(x[r+1]==0) )
                z[r]=y[3];
            else if( (x[r-1]==1)&&(x[r]==0)&&(x[r+1]==1) )
                z[r]=y[2];
            else if( (x[r-1]==1)&&(x[r]==1)&&(x[r+1]==0) )
                z[r]=y[1];
            else if( (x[r-1]==1)&&(x[r]==1)&&(x[r+1]==1) )
                z[r]=y[0];
            if(z[r]==1)fprintf(fp,"%c",F);
            else fprintf(fp,"%c",B);
        }
        fprintf(fp,"\n ");


        z[0]=z[COLUMNS-1]=0;
        for(r=1; r<(COLUMNS-1); r++)
        {
            if( (z[r-1]==0)&&(z[r]==0)&&(z[r+1]==0) )
                x[r]=y[7];
            else if( (z[r-1]==0)&&(z[r]==0)&&(z[r+1]==1) )
                x[r]=y[6];
            else if( (z[r-1]==0)&&(z[r]==1)&&(z[r+1]==0) )
                x[r]=y[5];
            else if( (z[r-1]==0)&&(z[r]==1)&&(z[r+1]==1) )
                x[r]=y[4];
            else if( (z[r-1]==1)&&(z[r]==0)&&(z[r+1]==0) )
                x[r]=y[3];
            else if( (z[r-1]==1)&&(z[r]==0)&&(z[r+1]==1) )
                x[r]=y[2];
            else if( (z[r-1]==1)&&(z[r]==1)&&(z[r+1]==0) )
                x[r]=y[1];
            else if( (z[r-1]==1)&&(z[r]==1)&&(z[r+1]==1) )
                x[r]=y[0];
            if(x[r]==1)fprintf(fp,"%c",F);
            else fprintf(fp,"%c",B);
        }
        fprintf(fp,"\n ");
    }



    printf("open c:\\cellular_automata_rule_specific.txt to see cellular automata pattern..!!\n\n");
    return 0;
}


Output as seen on Notepad

C language cellular automata code

  • 1D cellular automata Written using the very basics of C so it is easily readable.
  • Generates all 256 possible rules(you can find specific rule code here)
  • Creates a text file in the location C:\cellular_automata.txt of the size 15057KB 
  • Open that text file using any text viewer(notepad is good enough) and set to a lower font size and see the automata patterns
Of course the code looks too long. That's because i was using the very basics of the C language

/* (copyleft) no rights reserved*/
#include <stdio.h>
#include <stdlib.h>
#define COLUMNS 300
#define ITERATIONS 100
#define SEED 150

int main()
{
char F='O',B=' ';
int y[8]={0,0,0,0,0,0,0,0},x[COLUMNS],z[COLUMNS];
int r,j,k,t,i;
FILE *fp;fp=fopen("c:\\cellular_automata.txt","w");

fprintf(fp,"Set font size to 1 on your text viewer to see the larger picture\n");
fprintf(fp,"Hint: If you are using notepad click Format->font and change the font size\n\n\n");
for(i=1;i<=256;i++)
    {
if(i%1==0)  {if(y[7]==1)y[7]=0;else y[7]=1;}
if(i%2==0)  {if(y[6]==1)y[6]=0;else y[6]=1;}
if(i%4==0)  {if(y[5]==1)y[5]=0;else y[5]=1;}
if(i%8==0)  {if(y[4]==1)y[4]=0;else y[4]=1;}
if(i%16==0) {if(y[3]==1)y[3]=0;else y[3]=1;}
if(i%32==0) {if(y[2]==1)y[2]=0;else y[2]=1;}
if(i%64==0) {if(y[1]==1)y[1]=0;else y[1]=1;}
if(i%128==0){if(y[0]==1)y[0]=0;else y[0]=1;}

    fprintf(fp,"rule %d: %d%d%d%d%d%d%d%d\n ",i,y[0],y[1],y[2],y[3],y[4],y[5],y[6],y[7]);
    for(j=0;j<COLUMNS;j++)
    {
    x[j]=0;
    z[j]=0;
    }
    x[SEED]=1;


    for(k=0;k<ITERATIONS;k++)
            {
        x[0]=x[COLUMNS-1]=0;
        for(r=1;r<(COLUMNS-1);r++)
                {
             if( (x[r-1]==0)&&(x[r]==0)&&(x[r+1]==0) )
        z[r]=y[7];
        else if( (x[r-1]==0)&&(x[r]==0)&&(x[r+1]==1) )
        z[r]=y[6];
        else if( (x[r-1]==0)&&(x[r]==1)&&(x[r+1]==0) )
        z[r]=y[5];
        else if( (x[r-1]==0)&&(x[r]==1)&&(x[r+1]==1) )
        z[r]=y[4];
        else if( (x[r-1]==1)&&(x[r]==0)&&(x[r+1]==0) )
        z[r]=y[3];
        else if( (x[r-1]==1)&&(x[r]==0)&&(x[r+1]==1) )
        z[r]=y[2];
        else if( (x[r-1]==1)&&(x[r]==1)&&(x[r+1]==0) )
        z[r]=y[1];
        else if( (x[r-1]==1)&&(x[r]==1)&&(x[r+1]==1) )
        z[r]=y[0];
        if(z[r]==1)fprintf(fp,"%c",F);
        else fprintf(fp,"%c",B);
                }
        fprintf(fp,"\n ");


        z[0]=z[COLUMNS-1]=0;
        for(r=1;r<(COLUMNS-1);r++)
                {
             if( (z[r-1]==0)&&(z[r]==0)&&(z[r+1]==0) )
        x[r]=y[7];
        else if( (z[r-1]==0)&&(z[r]==0)&&(z[r+1]==1) )
        x[r]=y[6];
        else if( (z[r-1]==0)&&(z[r]==1)&&(z[r+1]==0) )
        x[r]=y[5];
        else if( (z[r-1]==0)&&(z[r]==1)&&(z[r+1]==1) )
        x[r]=y[4];
        else if( (z[r-1]==1)&&(z[r]==0)&&(z[r+1]==0) )
        x[r]=y[3];
        else if( (z[r-1]==1)&&(z[r]==0)&&(z[r+1]==1) )
        x[r]=y[2];
        else if( (z[r-1]==1)&&(z[r]==1)&&(z[r+1]==0) )
        x[r]=y[1];
        else if( (z[r-1]==1)&&(z[r]==1)&&(z[r+1]==1) )
        x[r]=y[0];
        if(x[r]==1)fprintf(fp,"%c",F);
        else fprintf(fp,"%c",B);
                }
        fprintf(fp,"\n ");

            }

    fprintf(fp,"\n\n\n");

    }

printf("open c:\\cellular_automata.txt to see intrisique patterns of cellular automata..!!\n\n");
return 0;
}



Here are some examples 

rule 13

rule 18
rule 30
rule 129
rule 151
rule 169
rule 195


PS:
If you are not seeing patterns in Notepad set the font settings in notepad to following

Font: Lucida Console
Font style: Regular
Font size: 1