3

I would like to plot area of the integral in Simpson 1/3 rule corresponding to a given curve using c program?

Here is my codes:

/*
 Simpson's 1/3 Rule
 Equation: x/(1+x) dx
 lower limit=0 upper limit=1 number of interval=6
 */
//#include<conio.h>
#include<stdio.h>
int main()
{
    float a,b,h,x,y,so=0;
    float x0,y0,yn,xn,r,se=0;;
    int i,n;
    float f(float);
   // clrscr();
    printf("\nEnter lower limit value:: "); scanf("%f",&a);
    printf("\nEnter upper limit value:: "); scanf("%f",&b);
    printf("\nEnter the number of intervals:: "); scanf("%d",&n);
    h=(b-a)/n;
    x0=a;
    y0=f(x0);
    yn=f(b);
    x=x0+h;
    for(i=1;i<=n-1;i=i+2)
    {


        y=f(x);
        so=so+y;
        x=x+2*h;
    }
    x=x0+2*h; for(i=2;i<=n-2;i=i+2) {
        y=f(x);
        se=se+y;
        x=x+2*h;
    }
    r=(h/3)*(y0+yn+4*so+2*se);
    printf("\nResult is:: %f",r);
    //getch();
}
float f(float x)
{
    return x/(1+x);
}

How can I plot the area using C programming? I can do this using Python, but would like to do it with c.

Christoph
  • 47,569
  • 8
  • 87
  • 187
Primo Raj
  • 109
  • 1
  • 7
  • 2
    The C standard does not cover graphics, so it is completely system-dependent. – Lundin Feb 15 '18 at 15:38
  • Then, learn a graphics API? DirectX, OpenGL, SDL... – machine_1 Feb 15 '18 at 15:45
  • Well formatted code and sample inputs make for a better post. – chux - Reinstate Monica Feb 15 '18 at 21:34
  • Apply [the Unix Philosophy](https://en.wikipedia.org/wiki/Unix_philosophy), and just print the sample points (x and y) to a file, one point (pair of numbers) per line. Then use e.g. gnuplot (`plot "a file" u 1:2 notitle w lines`) to plot the graph itself (either interactively to your display, or to an image file). – Nominal Animal Feb 15 '18 at 21:41
  • Give this a try: http://ndevilla.free.fr/gnuplot/ –  Feb 15 '18 at 21:42

0 Answers0