Latest Updates

Showing posts with label plot. Show all posts
Showing posts with label plot. Show all posts

Plot 2d graph in c++

November 26, 2016

Source Code:


#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
int itr = 0,fn,fc;
float i,j,jp;
float xl,xu,x,xo=0,e;
float f(float );
void cdraw(float );
void pdraw(float );
void gplot(void );
int braketing(void);
void xr(void);
float err(void);


float f(float x)
{
    switch(fn)
    {
        case 1:return (pow(x,10)-1);
        break;
        case 2:return (2*log10(x)-1);
        break;
        case 3:return (exp(-x)-x);
        break;
        case 4:return (log(x)-1);
        break;
        case 5:return (sin(2*x));
        break;
    }
}
void pdraw(float a)
{
    i=300+a*100;
    jp=f(a);
    j=300-jp*100;
    setcolor(2);
    circle(i, j, 3);
    putpixel(i,j,4);
}
void cdraw(float a)
{
    i=300+a*100;
    jp=f(a);
    j=300-jp*100;
    putpixel(i,j,4);
}
void gplot(void )
{
    float d;

   for(d=-5;d<7;d=d+.001)
    cdraw(d);
}

int main()
{
    float x,d;
   // while(fn!=6)
    {
        printf("1.Power: X^10-1\n2.Log: 2logX-1\n3.Exp: e^-x-X\n4.ln: ln(X)-1\n5.Sin: Sin(X)-1\n6.Exit\nEnter your choice:\n");
        scanf("%d",&fn);
       //fn=3;
        int gdriver =DETECT, gmode;
        initgraph(&gdriver,&gmode,"c:\\TC\\BGI");
        line(20,300,800,300)  ;
        line(300,0,300,600)  ;
        gplot();
    }


       getch();
    //  return 0;
}


Output:






 
Copyright © Ebots.