Trying to combine three codes in 1 code. Error came out

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • albertaw1988
    New Member
    • Mar 2010
    • 7

    Trying to combine three codes in 1 code. Error came out

    I'm trying to solve a numerical problem. Ordinary Differential Equation and this problem have 3 methods to solve it. I've type out 3 individual method and try to combine them into 1 code. I'm using Visio studio 2005.

    #include "stdafx.h"
    #include "math.h"


    float f1(float t,float x, float v);
    float f2(float t,float x, float v);


    int _tmain(int argc, _TCHAR* argv[])//Euler method
    {
    FILE *wPtr;
    wPtr=fopen("res ults.xls","w");

    float t0,x0,v0,tn,n,h ;
    float k1,k2,t,x,v;

    printf("Please Inset initial time, t0 = ");
    scanf ("%f",&t0);
    printf("Please Insert initial displacement, x0 = ");
    scanf ("%f",&x0);
    printf("Please insert initial velocity, v0= ");
    scanf ("%f",&v0);
    printf("Please insert final time,tn= ");
    scanf ("%f",&tn);
    printf("Please insert -1 to end the program.");
    printf("\n");
    printf("\nPleas e insert n value= ");
    scanf ("%f",&n);

    while (n!=-1)
    {
    printf("\nt\t\t x\t\tv\n");
    h=(tn-t0)/n;
    t=t0;
    x=x0;
    v=v0;

    while(t<tn)
    {
    k1=h*f1(t,x,v);
    k2=h*f2(t,x,v);

    x=x+k1;
    v=v+k2;
    t=t+h;

    printf("%f %f %f\n",t,x,v);
    fprintf(wPtr,"% f %f %f\n",t,x,v);

    }

    printf("\nPleas e insert n value= ");
    scanf ("%f",&n);
    }

    fclose(wPtr);
    printf ("Thankyou for using.\n\n");

    return 0;
    }


    float f1(float t,float x, float v)
    {
    return v;
    }

    float f2(float t, float x, float v)
    {
    return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
    }

    =============== =============== =============== =============== =============== =============== =============== =============== =============== =============== =====

    #include "stdafx.h"
    #include "math.h"


    float f1(float t,float x, float v);
    float f2(float t,float x, float v);


    int _tmain(int argc, _TCHAR* argv[])//Huen's method
    {
    FILE *wPtr;
    wPtr=fopen("res ults.xls","w");

    float t0,x0,v0,tn,n,h ;
    float dydx11,dydx12,d ydx21,dydx22,sl ope1,slope2,t,x ,xe,ve,v;

    printf("Please Inset initial time, t0 = ");
    scanf ("%f",&t0);
    printf("Please Insert initial displacement, x0 = ");
    scanf ("%f",&x0);
    printf("Please insert initial velocity, v0= ");
    scanf ("%f",&v0);
    printf("Please insert final time,tn= ");
    scanf ("%f",&tn);
    printf("Please insert -1 to end the program.");
    printf("\n");
    printf("\nPleas e insert n value= ");
    scanf ("%f",&n);

    while(n!=-1)
    {

    printf("\nt\t\t x\t\tv\n");
    h=(tn-t0)/n;
    t=t0;
    x=x0;
    v=v0;

    while(t<tn)
    {
    dydx11=f1(t,x,v );
    dydx12=f2(t,x,v );

    xe=x+h*dydx11;
    ve=v+h*dydx12;

    dydx21=f1(t+h,x e,ve);
    dydx22=f2(t+h,x e,ve);

    slope1=(dydx11+ dydx21)/2;
    slope2=(dydx12+ dydx22)/2;

    x=x+h*slope1;
    v=v+h*slope2;

    t=t+h;

    printf("%f %f %f\n",t,x,v);
    fprintf(wPtr,"% f %f %f\n",t,x,v);

    }
    printf("\nPleas e insert n value= ");
    scanf ("%f",&n);
    }

    fclose(wPtr);
    printf ("Thankyou for using.\n\n");
    return 0;
    }


    float f1(float t,float x, float v)
    {
    return v;
    }

    float f2(float t, float x, float v)
    {
    return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
    }


    =============== =============== =============== =============== =============== =============== =============== =============== =============== =============== =====

    #include "stdafx.h"
    #include "math.h"


    float f1(float t,float x, float v);
    float f2(float t,float x, float v);


    int _tmain(int argc, _TCHAR* argv[])//Range-Kutta method
    {
    FILE *wPtr;
    wPtr=fopen("res ults.xls","w");

    float t0,x0,v0,tn,n,h ;
    float k11,k12,k21,k22 ,k31,k32,k41,k4 2,slope1,slope2 ,t,x,v;

    printf("Please Inset initial time, t0 = ");
    scanf ("%f",&t0);
    printf("Please Insert initial displacement, x0 = ");
    scanf ("%f",&x0);
    printf("Please insert initial velocity, v0= ");
    scanf ("%f",&v0);
    printf("Please insert final time,tn= ");
    scanf ("%f",&tn);
    printf("Please insert EOF to end the program.");
    printf("\n");
    printf("\nPleas e insert n value= ");
    scanf ("%f",&n);

    while(n!=-1)
    {

    printf("\nt\t\t x\t\tv\n");
    h=(tn-t0)/n;
    t=t0;
    x=x0;
    v=v0;

    while(t<tn)
    {
    k11=f1(t,x,v);
    k12=f2(t,x,v);

    k21=f1(t+h/2,x+k11*h/2, v+k12*h/2);
    k22=f2(t+h/2,x+k11*h/2, v+k12*h/2);

    k31=f1(t+h/2,x+k21*h/2, v+k22*h/2);
    k32=f2(t+h/2,x+k21*h/2, v+k22*h/2);

    k41=f1(t+h, x+k31*h, v+k32*h);
    k42=f2(t+h, x+k31*h, v+k32*h);

    slope1=(k11+2*k 21+2*k31+k41)/6;
    slope2=(k12+2*k 22+2*k32+k42)/6;

    x=x+h*slope1;
    v=v+h*slope2;

    t=t+h;

    printf("%f %f %f\n",t,x,v);
    fprintf(wPtr,"% f %f %f\n",t,x,v);

    }
    printf("\nPleas e insert n value= ");
    scanf ("%f",&n);
    }

    fclose(wPtr);
    printf ("Thankyou for using.\n\n");
    return 0;
    }


    float f1(float t,float x, float v)
    {
    return v;
    }

    float f2(float t, float x, float v)
    {
    return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
    }

    =============== =============== =============== ===========
    and here's my combined code:

    #include "stdafx.h"
    #include "math.h"

    float dxdt(float t, float x, float v);
    float dvdt(float t, float x, float v);

    FILE *wEuler;
    FILE *wHuen;
    FILE *wRK;

    float Euler(float t0, float x0, float v0, float tn,int n )
    {

    float t,x,v,h;
    float k1,k2;

    printf("\nt\t\t x\t\tv\n");
    fprintf(wEuler, "\nt\tx\tv\ n");

    h=(tn-t0)/n;
    t=t0;
    x=x0;
    v=v0;

    while(t<tn)
    {
    k1=h*dxdt(t,x,v );
    k2=h*dvdt(t,x,v );

    x=x+k1;
    v=v+k2;
    t=t+h;

    printf("%f %f %f\n",t,x,v);
    fprintf(wEuler, "%f %f %f\n",t,x,v);

    }

    return 0;
    }

    float Huen(float t0, float x0, float v0, float tn,int n )
    {

    float t,x,v,h;
    float dydx11,dydx12,d ydx21,dydx22,sl ope1,slope2,xe, ve;

    printf("\nt\t\t x\t\tv\n");
    fprintf(wHuen," \n\nt\tx\tv\n") ;

    h=(tn-t0)/n;
    t=t0;
    x=x0;
    v=v0;

    while(t<tn)
    {
    dydx11=dxdt(t,x ,v);
    dydx12=dvdt(t,x ,v);

    xe=x+h*dydx11;
    ve=v+h*dydx12;

    dydx21=dxdt(t+h ,xe,ve);
    dydx22=dvdt(t+h ,xe,ve);

    slope1=(dydx11+ dydx21)/2;
    slope2=(dydx12+ dydx22)/2;

    x=x+h*slope1;
    v=v+h*slope2;

    t=t+h;

    printf("%f %f %f\n",t,x,v);
    fprintf(wHuen," %f %f %f\n",t,x,v);

    }

    return 0;
    }

    float RK(float t0, float x0, float v0, float tn,int n )
    {

    float t,x,v,h;
    float k11,k12,k21,k22 ,k31,k32,k41,k4 2,s1,s2;

    printf("\nt\t\t x\t\tv\n");
    fprintf(wRK,"\n t\tx\tv\n");

    h=(tn-t0)/n;
    t=t0;
    x=x0;
    v=v0;

    while(t<tn)
    {
    k11=dxdt(t,x,v) ;
    k12=dvdt(t,x,v) ;

    k21=dxdt(t+h/2,x+k11*h/2, v+k12*h/2);
    k22=dvdt(t+h/2,x+k11*h/2, v+k12*h/2);

    k31=dxdt(t+h/2,x+k21*h/2, v+k22*h/2);
    k32=dvdt(t+h/2,x+k21*h/2, v+k22*h/2);

    k41=dxdt(t+h, x+k31*h, v+k32*h);
    k42=dvdt(t+h, x+k31*h, v+k32*h);

    s1=(k11+2*k21+2 *k31+k41)/6;
    s2=(k12+2*k22+2 *k32+k42)/6;

    x=x+h*s1;
    v=v+h*s2;

    t=t+h;

    printf("%f %f %f\n",t,x,v);
    fprintf(wRK,"%f %f %f\n",t,x,v);


    }

    return 0;
    }

    float dxdt(float t,float x, float v)
    {
    return v;
    }

    float dvdt(float t, float x, float v)
    {
    return (0.5*cos(0.5*t)-1*x-0.5*pow(x,3)-0.4*v)/1;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    wEuler=fopen("E uler.xls","w");
    wHuen=fopen("He un.xls","w");
    wRK=fopen("Rang e-Kutta.xls","w") ;

    // char selection;
    float t0,x0,v0,tn,h,t ,x,v;
    int n;
    // float k1,k2;
    // float dydx11,dydx12,d ydx21,dydx22,sl ope1,slope2,xe, ve;
    // float k11,k12,k21,k22 ,k31,k32,k41,k4 2,s1,s2;

    //main:

    printf("\nPleas e select the option:\n\n");
    printf("======= ===========Main Menu=========== ====\n");
    printf("** Option 1: Different time steps **\n");
    printf("** Option 2: Same time steps **\n");
    printf("** Enter EOF to end the program **\n");
    printf("======= =============== =============== =====\n\n");


    printf("Please Inset initial time, t0 = ");
    scanf_s ("%f",&t0);
    printf("Please Insert initial displacement, x0 = ");
    scanf_s ("%f",&x0);
    printf("Please insert initial velocity, v0= ");
    scanf_s ("%f",&v0);
    printf("Please insert final time,tn= ");
    scanf_s ("%f",&tn);
    printf("Please insert -1 to end the program.");
    printf("\n");
    again: printf("\nPleas e insert n value= ");
    scanf_s ("%f",&n);

    if(n!=-1)
    {
    Euler(t0,x0,v0, tn,n);
    Huen(t0,x0,v0,t n,n );
    RK(t0,x0,v0,tn, n);

    goto again;
    }


    else
    {
    printf ("Thankyou for using.\n\n");
    }

    fclose(wEuler);
    fclose(wHuen);
    fclose(wRK);
    return 0;
    }

    The program give me wrong answers and looping. Any advise?
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Did you determine in which loop exactly it loops infinitely, or you'd rather want us do it for you?

    Comment

    • albertaw1988
      New Member
      • Mar 2010
      • 7

      #3
      erm. it should not loop the program. it oni loop from t0 till tn (let say 0 to 100 with time steps of h) and ask the user to input another n value or -1 to end the program

      i don't know what's wrong with my combined code because it seems to be correct when i try to do with 1 method.

      Comment

      • albertaw1988
        New Member
        • Mar 2010
        • 7

        #4
        erm. it should not loop the program. it oni loop from t0 till tn (let say 0 to 100 with time steps of h) and ask the user to input another n value or -1 to end the program

        i don't know what's wrong with my combined code because it seems to be correct when i try to do with 1 method.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Ok, it should not loop but it loops, so one of the loops performes endlessly. Did you find which one and why it never exits?

          Comment

          • albertaw1988
            New Member
            • Mar 2010
            • 7

            #6
            there's only looping in t<tn, where t=t+h..but when the program run, it does not run the go through the h equation. the program like keep adding 0.000001 and the other values for x n v gives wrong results. I still can't figure it out.

            Comment

            • albertaw1988
              New Member
              • Mar 2010
              • 7

              #7
              i think my n input gt some problem..the program should end when i input -1..but it doesn't. still can't figure out why.

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                Try to check the value of n when you calculate h, it's incorrect. And it's already incorrect when you pass it to the function.

                Comment

                • albertaw1988
                  New Member
                  • Mar 2010
                  • 7

                  #9
                  how would it be wrong since the individual code works when i insert n=500, it give h of 0.2, t will loop with increment of 0.2. does it mean that i have to put h function into if{}?

                  Comment

                  • newb16
                    Contributor
                    • Jul 2008
                    • 687

                    #10
                    Because in scanf you have format specifier %f when reading n which is int and requires %d

                    Comment

                    • albertaw1988
                      New Member
                      • Mar 2010
                      • 7

                      #11
                      oh yeah..thanks..n ow the program runs correctly. but 1 more thing, if i enter values less than 500, some of the output gives me #INFOO and #INDOO. May I know why?

                      Comment

                      Working...