Social Icons

Sunday, 4 May 2014

Gauss Elimination in Matlab

function [x] = g1(A, b)
%work out the number of equations
N = length(b)
%Gaussian elimination
for column=1:(N-1)
%work on all the rows below the diagonal element
for row = (column+1):N
%work out the value of d
d = A(row,column)/A(column,column);
%do the row operation
A(row,:) = A(row,:)-d*A(column,:)
b(row) = b(row)-d*b(column)
end%loop through rows
end %loop through columns
for row=N:-1:1
x(row) = b(row);
for i=(row+1):N
x(row) = x(row)-A(row,i)*x(i);
end
x(row) = x(row)/A(row,row);
end
return

Monte Carlo Simulation

N=input('enter no of dats');
r=4;
c=0;
for i=1:N
    x=r*rand(1);
    y=r*rand(1);
    if((x^2+y^2)<=r^2)
        c=c+1;
       
    end;
end;
n=c;
pi=(4*n)/N

Script For inverse of a matrix in Matlab

x=input('Enter a matrix 3*3')
y=input('Enter another 3*3 matrix')
s=x+y
 det=s(1,1)*(s(2,2)*s(3,3)-s(2,3)*s(3,2))-s(1,2)*(s(2,1)*s(3,3)-s(3,1)*s(2,3))+s(1,3)*(s(2,1)*s(3,2)-s(3,1)*s(2,2))
adj=[[(s(2,2)*s(3,3)-s(3,2)*s(2,3));-(s(2,1)*s(3,3)-s(3,1)*s(2,3));(s(2,1)*s(3,2)-s(3,1)*s(2,2))] [-(s(1,2)*s(3,3)-s(3,2)*s(1,3));(s(1,1)*s(3,3)-s(3,1)*s(1,3));-(s(1,1)*s(3,2)-s(3,1)*s(1,2))] [(s(2,2)*s(1,3)-s(1,2)*s(2,3));-(s(1,1)*s(2,3)-s(2,1)*s(1,3));(s(1,1)*s(2,2)-s(2,1)*s(1,2))]]
inverse=(1./det).*adj

Saturday, 3 May 2014

Symbol Table Creation


To generate tokens
%%
{letter}({letter}|{digit})*       printf(“id: %s\n”, yytext);
%%
--------------------

1.
%{
#include<string.h>
%}
%%
(int|float|double|long|short|return|include|char) { printf("\n< Keyword,%s >",yytext);func(yytext,"Keyword");}
[\"] { printf("\n< Operator,%s >",yytext);func(yytext,"Operator"); }
['"'][\\a-zA-Z0-9|' ']+['"'] { printf("\n< id,%s >",yytext); func(yytext,"String");}

\+\+ { printf("\n< %s >",yytext);func(yytext,"Operator"); }
\-\- { printf("\n< %s >",yytext); func(yytext,"Operator");}
\=\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\!\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\+\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\-\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
[/|'.']+ { printf("\n< %s >",yytext); func(yytext,"Operator");}
(printf|scanf) { printf("\n< function,%s >",yytext);func(yytext,"Function");}

[0-99]+ { printf("\n<Number,%s >",yytext);func(yytext,"Number"); }
[a-zA-Z|_][0-99|a-zA-Z|_]* { printf("\n< variable,%s>",yytext); func(yytext,"variable");}
['+'|\-|'='|':'|'%'|'&'|'('|')'|'{'|'}'|','|'#'|'!'|'@'|'$'|'*'|'<'|'>'|'?'] { printf("\n< %s >",yytext);func(yytext,"Operator");}

[';']+ { printf("\n< %s >",yytext);func(yytext,"Operator");}
\n func2();
%%
struct entry
{
char name[10];
char token[10];
}st[10];
int i=0;
func(char n[10],char t[10])
{
int j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(n,st[j].name)==0)
flag=1;
}
if(flag==0)
{
strcpy(st[i].name,n);
strcpy(st[i].token,t);
i++;
}
}
func2()
{
int k;
printf("\n===========Symbol Table==================\n");
for(k=0;k<=i;k++)
printf("%s   %s\n",st[k].name,st[k].token);
}



Friday, 2 May 2014

Bouncing Sphere in Opengl

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
float x=2,y=0,d=0;
void display()
{      
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
   
   
     
      glPushMatrix();
      glTranslatef(0.0,-2.0+y,0.0);
      glRotatef(x,1.0,0.0,0.0);
      glutSolidSphere(0.5,30,40);
     
      glPopMatrix();
      glFlush();

 glutPostRedisplay();
 }
void reshape(GLint w, GLint h) {

  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
 
    glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
  } else {
 
    glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
  }

}



void timer(int v)
{
x=x+0.03;
     

 if(d==0)
  {glColor3f(1.0,0.0,0.0);
   y=y+0.03;
   if(y>4)
    d=1;
  }
   if(d==1)
  {
  glColor3f(1.0,1.0,0.0);
   y=y-0.03;
   if(y<-0.8)
    d=0;
  }


 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Cyan Shapes in Yellow Light");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);

  glutMainLoop();
}

Solar System Using Opengl IN Graphics


#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

//float x=0.0;
float a[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//angle of Rotation
float speed[10]={0.0,0.06,0.05,0.04,0.03,0.02,0.01,0.008,0.007,0.006};//speed of Rotation
float radius[10]=   {5.0,1.5,1.2,1.0,2.0,1.3,1.0,1.0,1.0,1.0};//planet Size
float distancex[10]={0.0,7.0,10.0,13.0,17.0,22.0,26.0,29.0,32.0,35.0};//DIstance Between Planets

  GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
  GLfloat skyblue[] = { 0.2, 0.2, 1.0, 1.0 };
  GLfloat mitty[] = { 0.8, 0.8, 1.0, 1.0 };

void display()
{
       int i;
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glMatrixMode(GL_MODELVIEW);

        for(i=0;i<10;i++)
        {
            glPushMatrix();
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,white);
             glutWireTorus(0.001,distancex[i],50,50);
            if(i==4)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,red);
            else if(i==3)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,blue);
            else if(i==0)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,yellow);
            else
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mitty);
            glRotatef(a[i],0.0,0.0,1.0);
            glTranslatef(distancex[i],0.0,0.0);
             if(i==6)
            {
                glRotatef(-45,1.0,1.0,0.0);
                glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,green);
                glutWireTorus(0.2,1.5,20,20);
            }

            glPushMatrix();
            glRotatef(a[i],0.0,1.0,0.0);
            glutSolidSphere(radius[i],30,10);
            glPopMatrix();

              glPopMatrix();
          }

      glutSwapBuffers();
}

void timer(int vaulue)
{
    int i;
    for(i=1;i<10;i++)
       {
           a[i] = a[i]+speed[i];
       }
       glutPostRedisplay();
       glutTimerFunc(1000/500,timer,1);
}

void reshape(GLint w, GLint h) {
   glViewport(0, 0, w, h);
 // glClearColor(1.0,1.0,1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
gluLookAt(5.0,-5.0,10.0,.0,0.0,0.0,0.0,0.0,1.0);
  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
    // width is smaller, so stretch out the height
    glOrtho(-40.0, 40.0, -40.0/aspect, 40.0/aspect, -40.0, 40.0);
  } else {
    // height is smaller, so stretch out the width
    glOrtho(-40.0*aspect, 40.0*aspect, -40.0, 40.0, -40.0, 40.0);
  }
}

void init() {
 GLfloat direction[] = { 5.0, 0.0, 0.0, 0.0 };
 GLfloat direction1[] = { -5.0, 0.0, 0.0, 0.0 };
 glMaterialf(GL_FRONT, GL_SHININESS, 50);
  glLightfv(GL_LIGHT0, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glLightfv(GL_LIGHT1, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT1, GL_POSITION, direction1);

  glEnable(GL_LIGHTING);                // so the renderer considers light
  glEnable(GL_LIGHT0);                  // turn LIGHT0 on
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);              // so the renderer considers depth
}

// The usual application statup code.
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(600, 600);
  glutCreateWindow("Solar System Simulation");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
   glutTimerFunc(1000/10,timer,1);
  init();
  glutMainLoop();
}

Bouncing Ball In Opengl

#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1,u=0.0,d=0.0;
static GLfloat currentAngleOfRotation = 45.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-50.0, 50.0, -50.0/aspect, 50.0/aspect, -1.0, 1.0);
//     f1=1;
  } else {
    glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
 //    f2=1;
  }
}

void display() {
   float angle,x,y;
   int i;
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 0.0, 1.0);
  glScalef(10.0,10.0,10.0);
//  printf("f1=%d f2=%d",f1,f2);
 
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x+u,y+u);
   }

  glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v)
{

 if(d==0)
  {
  u=u+0.03;
  if(u>2.9)
  d=1;
  }
   if(d==1)
  {
  u=u-0.03;
  if(u<-2.9)
  d=0;
  }
 

 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
 
  glutMainLoop();
}

Moving Torus in opengl

#include<GL/glut.h>

int x=0,y=0,z=0;
float radius=7.0;
float u=0.0;
float p=2.0;
void display()
{   p=p+0.03;

glClear(GL_COLOR_BUFFER_BIT);
// glColor3f(1.0,0.0,1.0);
    glPushMatrix();
     
      glRotatef(p,1.0,0.0,0.0);
      glutWireTorus(3,5,30,40);
     
      glPopMatrix();
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);glVertex3f(0,0,0);glVertex3f(20,0,0);
glColor3f(1.0,1.0,0.0);glVertex3f(0,0,0);glVertex3f(0,20,0);
glColor3f(1.0,0.0,1.0);glVertex3f(0,0,0);glVertex3f(0,0,20);

glEnd();

   glFlush();
   glutPostRedisplay();
   //glutSwapBuffers();
}
/*

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glutSolidSphere(1.0, 25, 25);
  glMatrixMode(GL_MODELVIEW);
//  glPushMatrix();
  glLoadIdentity();

   x = radius * cos(u);
    y = 0;
    z = radius * sin(u);
  gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

*/
void init(void)
{

 glClearColor(0.0,0.0,0.0,1.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(60.0,4/3,1,40);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(7,7,20,0,0,0,0,1,0);



 /*glLightfv(GL_LIGHT0,GL_DIFFUSE,yellow);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);*/

}



/*
void timer(int v)
{
 u=u+0.1;
 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,((GLfloat)w/(GLfloat)(h)),1,10.0);
   
}


*/

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutCreateWindow("Going for a SMILEY :-)");
 
   glutDisplayFunc(display);
  // glutReshapeFunc(reshape);
  // glutTimerFunc(100,timer,0);
    init();
    glutMainLoop();
}

Moon in Opengl


#include<GL/glut.h>

int x=0,y=0,z=0;
float radius=7.0;
float u=0.0;

void display()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// glColor3f(1.0,0.0,1.0);

glutSolidSphere(1.0,30,30);
GLfloat direction[]={1.0,1.0,1.0,0.0};
glLightfv(GL_LIGHT0,GL_POSITION,direction);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
x=radius* cos(u);
y=0;
z=radius* sin(u);

gluLookAt(x,y,z,0,0,0,0,1,0);




   glFlush();
   glutSwapBuffers();
}
/*

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat direction[] = {-1.0, -1.0, -1.0, 0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glutSolidSphere(1.0, 25, 25);
  glMatrixMode(GL_MODELVIEW);
//  glPushMatrix();
  glLoadIdentity();

   x = radius * cos(u);
    y = 0;
    z = radius * sin(u);
  gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  //glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

*/
void init(void)
{

 GLfloat yellow[]={1.0,1.0,0.0};  
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 //gluOrtho2D(0.0,640.0,0.0,480.0);
 glLightfv(GL_LIGHT0,GL_DIFFUSE,yellow);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);

}




void timer(int v)
{
 u=u+0.1;
 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,((GLfloat)w/(GLfloat)(h)),1,10.0);
   
}




int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Going for a SMILEY :-)");
 
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);
   glutTimerFunc(100,timer,0);
    init();
    glutMainLoop();
}
 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management