## euler approximation method

Submitted by:David Villa

Category:C++

C/C++ source code for euler approximation method

Code Snippet:

``#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>void menu(double **coefficient,int n,double ct,double xo,double yo,double h,double a,double b); void compute_approximation_euler1(double **coefficient,int n,double ct,double xo,double yo,double h, double a,double b); double f(double **coefficient,int n,double ct,double x,double y); int main(){    double *coefficient[2];    double ct,xo,yo,a,b,h;    int n,i;         printf("what's the highest degree of f(x,y)?\t");    fflush(stdin);    scanf("%d",&n);    n=n+1;    for(i=0;i<2;i++){    coefficient[i]=(double *)calloc(n,sizeof (double));}    for(i=1;i<n;i++){    printf("what's the coefficient of x to the %d power\t",i);    fflush(stdin);    scanf("%lf",&coefficient[0][i]);}    for(i=1;i<n;i++){    printf("what's the coefficient of y to the %d power\t",i);    fflush(stdin);    scanf("%lf",&coefficient[0][i]);}     printf("what's the constant term?\t");    fflush(stdin);    scanf("%lf",&ct);         printf("what's x initial\t");    fflush(stdin);    scanf("%lf",&xo);          printf("what's y initial\t");    fflush(stdin);    scanf("%lf",&yo);         printf("what's the stepsize\t");    fflush(stdin);    scanf("%lf",&h);         printf("over what interval do you want to approximate f(x)->( a b)\t");    fflush(stdin);    scanf("%lf%lf",&a,&b);    menu(coefficient,n,ct,xo,yo,h,a,b);    system("pause");    free(coefficient);     }   void menu(double **coefficient,int n,double ct,double xo,double yo,double h,double a,double b){       int k;       system("cls");       printf("choose the method that you want to use for the approximation\n");       printf("1.first order euler method\n");       fflush(stdin);       scanf("%d",&k);               switch(k){                         case 1:           compute_approximation_euler1(coefficient,n,ct,xo,yo,h,a,b);            break;       default:               printf("you did not enter a valid number\n");               }               } void compute_approximation_euler1(double **coefficient,int n,double ct,double xo,double yo,double h, double a,double b){               FILE *file;       int k,i;       double *x;       double *y;               system("cls");       file=fopen("c:\\approximation_euler1.dat","w");               if (file==NULL){       printf("the file cannot be created");       system("pause");       }               else               k=(int)((b-a)/h)+1;       x=(double *)calloc(k,sizeof (double));       y=(double *)calloc(k,sizeof (double));       x[0]=xo;       y[0]=yo;               for(i=0;i<k;i++){               x[i+1]=x[i]+h;       y[i+1]=y[i]+h*f(coefficient,n,ct,x[i],y[i]);       fprintf(file,"4.2%lf\t4.2%lf\n",x[i],y[i]);       }       free(x);       free(y);       fclose(file);       printf("to view the results open the file approximation_euler1");       system("pause");       exit(1);       }        double f(double **coefficient,int n,double ct,double x,double y){       int k;       double sum,xsum,ysum;                                for(k=1;k<n;k++){               xsum=xsum+x*pow(coefficient[0][k],k);       }               for(k=1;k<n;k++){               ysum=ysum+y*pow(coefficient[1][k],k);       }           sum=xsum+ysum+ct;               return(sum); }``