GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-May-2005, 06:05
edward_fraser edward_fraser is offline
New Member
 
Join Date: May 2005
Posts: 2
edward_fraser is on a distinguished road

PLEASE HELP, Beginner Problem with different results every time!!


Hey everytime i run my c++ program to implement an Improved Eulers Method numerical loop to calculate the response of something (dont worry about the maths behind it i know the equations are right!) i get different answers each time i do it. Ive tried setting each of the variables in the equation to 0 at the beginning of each loop but this didnt help?? I wont bother posting the results because they are each 1000 entry long text files, but they change slightly everytime!? Any ideas would be greatly appreciated?? Thanks alot,

Ed Fraser

CPP / C++ / C Code:

define variables n open libraries;

int main()
{
        m = 290, 
        k = 16182,
        c = 1000,     
        y0 = 0, 
        v0 = 0,
        t0 = 0;
 
 
 
 
    again=1;      
    while (again==1)
        {
          
        y = y0, v = v0, t = t0,
        K1 = 0, K2 = 0,
        L1 = 0, L2 = 0,
        dt = 0, t = 0, speed = 0;
        
        
       
        
        
        
        display menu;
                   
        
        if (menuchoice==1)
        {
        
                FILE *ifile;
                printf("\n\nPlease enter road profile filename: ");
                scanf("%s",filename);
                ifile=fopen(filename,"r");
        
                system("cls");
                printf("\n\nPlease enter the speed of the vehicle (mph): ");
                scanf("%f",&speed);
                        
                
                /* Convert speed from m.p.h. to ms-1 */
                
        
                
                speed = (speed * 0.44704);                        
                total_time = 150 / speed;              
                num_steps = 1000;                 
                dt = total_time / num_steps;
        
      
       
                        for(loop=0; loop<=num_steps; loop++)
                        {
                        fscanf(ifile, "%f\n", &rt[loop]);
                        }
           
                fclose(ifile);
                
        
                FILE *foutput;                
                foutput = fopen("output.txt", "w");
        
                        for (count=1; count<=num_steps; count++)
                        {       
                                
                                
                                
                                K1 = (dt*(c*(rt[count]-rt[count-1])/(dt*1000)+(k*rt[count]/1000)-(k*y)-(c*v)))/m;
                                K2 = dt*((v + K1)/(dt + t));                
                                L1 = dt*v;
                                L2 = dt * ((y + L1)/(dt + t));
                                v = v + 0.5*(K1 + K2);
                                y = y + 0.5*(L1 + L2);
                                t = t + dt;
                                
                                tout[count] = t;
                                yout[count] = y;
                                             
                                fprintf(foutput, "\n%f,", t);
                                fprintf(foutput, " %f", y);
                
                                      
                        } 
                        
                fclose(foutput);
                
                system("cls");
                printf("\n\nThe results for the response of the vehicle can be found \nin the output.txt file!");                      
                         
                        
                
                            
                }       
                
        else 
        
        if (menuchoice==2)
        {
        
                float x; 
                              
                system("cls");
                printf("\n\nPlease enter the speed of the vehicle (mph): ");
                scanf("%f",&speed);
        
                /* Convert speed from m.p.h. to ms-1 */
                                
                speed = (speed * 0.44704);                        
                total_time = 3 / speed;              
                hump_steps = 100;                 
                dt = total_time / hump_steps;
                
                         
                        for (i=1; i<=num_steps; i++)
                        {
                                if (i<=hump_steps)
                                {
                                x = 3.1415 * ( t / total_time ); 
                                rt[i] = sin(x)*0.08; 
                                t = t + dt;                                              
                                }
                                
                                else 
                                {
                                rt[i]=0;
                                }
                        }   
                        
                        FILE *foutput;                
                        foutput = fopen("output.txt", "w");
        
                        for (count=1; count<=num_steps; count++)
                        {       
                                
                                
                                
                                K1 = (dt*(c*(rt[count]-rt[count-1])/(dt*1000)+(k*rt[count]/1000)-(k*y)-(c*v)))/m;
                                K2 = dt*((v + K1)/(dt + t));                
                                L1 = dt*v;
                                L2 = dt * ((y + L1)/(dt + t));
                                v = v + 0.5*(K1 + K2);
                                y = y + 0.5*(L1 + L2);
                                t = t + dt;
                                
                                tout[count] = t;
                                yout[count] = y;
                                             
                                fprintf(foutput, "\n%f,", t);
                                fprintf(foutput, " %f", y);
                
                                      
                        } 
                        
                fclose(foutput);
                
                system("cls");
                printf("\n\nThe results for the response of the vehicle can be found \nin the output.txt file!");
                
                
        }                  
                              
             
        leaving menu;               
        if (again==1)
        {
                 printf("Great!\n\n");
                 system("PAUSE");   
        }
       
        else
      
        if (again==2)
        {
                printf("\n\nThank you for using the road profile response  emulator.\n\n");   
        }
                       
                   
                                            
        }

        return 0;   
        
}
  #2  
Old 03-May-2005, 06:45
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Ed, welcome to GIDForums. Glad to see your code in tags. Really.

But, you have not declared a single variable in your code (c not c++). Some just need a little push:
Quote:
Originally Posted by edward_fraser
CPP / C++ / C Code:
int main()
{
        m = 290,
        k = 16182,
        c = 1000,
        y0 = 0,
        v0 = 0,
        t0 = 0;
becomes:
CPP / C++ / C Code:
int main()
{
   int  m = 290,
        k = 16182,
        c = 1000,
        y0 = 0,
        v0 = 0,
        t0 = 0;

and so on. You will also need some includes to get things like:
Quote:
Originally Posted by edward_fraser
CPP / C++ / C Code:
    FILE *ifile;
    printf("\n\nPlease enter road profile filename: ");
    scanf("%s",filename);
    ifile=fopen(filename,"r");
to work. Work on that, post the errors you are getting. Right now, that dog won't fly.

Once you get to that point let us know what happens,
Quote:
Originally Posted by example
When I use 0 as my input my computer starts on fire. I am compiling with (insert compiler here) on (insert os here). This only happens with this input. This is what I am seeing.
Code:
>gcc -Wall myprog.c -o myexe >myexe.exe Welcome to my program Enter the number : 0 BOOM!

Well, you get the idea...
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
Last edited by LuciWiz : 04-May-2005 at 00:15. Reason: [Merging threads]
  #3  
Old 03-May-2005, 10:45
edward_fraser edward_fraser is offline
New Member
 
Join Date: May 2005
Posts: 2
edward_fraser is on a distinguished road

Improved but still doesn't get repeated results!?!?!


Hey, i've improved the program and added all the inlcude's etc... the program does the calculation fine and outputs it to a .txt file, although if i dont close the program and start it again, will give me different values everytime i do it. This makes me think i need to reset the values somehow but im not sure where or how?? Any ideas would be really great cause im ripping my hair out over it!!

Cheers,

E
D

CPP / C++ / C Code:

/* Assignment 2 - H3BCNT */
/* Program By Edward Fraser */
/* Written 20/04/05 */  

#include <stdio.h>   
#include <stdlib.h>
#include <math.h>

float rt[1000], yout[1000], tout[1000];
float t, y, v, dt, k, m, c, 
      L1, K1, K2, speed, 
      y0, v0, t0, L2,
      total_time;
char filename[20];
int count, num_steps,
    again, menuchoice,
    i, hump_steps, loop;

int main()
{
        m = 290, 
        k = 16182,
        c = 1000,     
        y0 = 0, 
        v0 = 0,
        t0 = 0;
 
 
 
 
    again=1;      
    while (again==1)
        {
          
        y = y0, v = v0, t = t0,
        K1 = 0, K2 = 0,
        L1 = 0, L2 = 0,
        dt = 0, t = 0, speed = 0;
        
        
       
        
        
        
        system("cls");
        printf("Welcome to the Road Emulator v1.0");
        printf("\n\nThis program will simulate a road surface to");
        printf("\nanalyse the response of a car driving along it");
        printf("\n\nWould you like to:");
        printf("\n1. View response to a specified road profile");
        printf("\n2. View response to a speed bump\n");
        scanf("%d",&menuchoice);
        
        while ((menuchoice!=1)&&(menuchoice!=2))
        {
          system("cls");
          printf("\n\nSorry you must choose either 1 for road profile response");
          printf("\nor 2 for the road hump response!! \n\nPlease re-enter: ");
          scanf("%d",&menuchoice);
        }       
                 
        
        
        if (menuchoice==1)
        {
        
                system("cls");
                FILE *ifile;
                printf("\n\nPlease enter road profile filename: ");
                scanf("%s",filename);
                ifile=fopen(filename,"r");
        
                        while(ifile==NULL)
                        { 
                         system("cls");
                         printf("\n\n  Sorry improper filename, please re-enter: ");
                         scanf("%s",filename);
                         ifile=fopen(filename,"r");
                         }   
        
                system("cls");
                printf("\n\nPlease enter the speed of the vehicle (mph): ");
                scanf("%f",&speed);
        
                        while (speed>50)
                        {
                        system("cls");
                        printf("\n\nI'm sorry the speed must be between 0 and 50 mph!");
                        printf("\n\n          Please Re-enter Speed (mph): ");
                        scanf("%f", &speed);
                        }
                
                
                /* Convert speed from m.p.h. to ms-1 */
                
        
                
                speed = (speed * 0.44704);                        
                total_time = 150 / speed;              
                num_steps = 1000;                 
                dt = total_time / num_steps;
        
      
       
                        for(loop=0; loop<=num_steps; loop++)
                        {
                        fscanf(ifile, "%f\n", &rt[loop]);
                        }
           
                fclose(ifile);
                
        
                FILE *foutput;                
                foutput = fopen("output.txt", "w");
        
                        for (count=1; count<=num_steps; count++)
                        {       
                                
                                
                                
                                K1 = (dt*(c*(rt[count]-rt[count-1])/(dt*1000)+(k*rt[count]/1000)-(k*y)-(c*v)))/m;
                                K2 = dt*((v + K1)/(dt + t));                
                                L1 = dt*v;
                                L2 = dt * ((y + L1)/(dt + t));
                                v = v + 0.5*(K1 + K2);
                                y = y + 0.5*(L1 + L2);
                                t = t + dt;
                                
                                tout[count] = t;
                                yout[count] = y;
                                             
                                fprintf(foutput, "\n%f,", t);
                                fprintf(foutput, " %f", y);
                
                                      
                        } 
                        
                fclose(foutput);
                
                system("cls");
                printf("\n\nThe results for the response of the vehicle can be found \nin the output.txt file!");                      
                         
                        
                
                            
                }       
                
        else 
        
        if (menuchoice==2)
        {
        
                float x; 
                              
                system("cls");
                printf("\n\nPlease enter the speed of the vehicle (mph): ");
                scanf("%f",&speed);
        
                /* Convert speed from m.p.h. to ms-1 */
                                
                speed = (speed * 0.44704);                        
                total_time = 3 / speed;              
                hump_steps = 100;                 
                dt = total_time / hump_steps;
                
                         
                        for (i=1; i<=num_steps; i++)
                        {
                                if (i<=hump_steps)
                                {
                                x = 3.1415 * ( t / total_time ); 
                                rt[i] = sin(x)*0.08; 
                                t = t + dt;                                              
                                }
                                
                                else 
                                {
                                rt[i]=0;
                                }
                        }   
                        
                        FILE *foutput;                
                        foutput = fopen("output.txt", "w");
        
                        for (count=1; count<=num_steps; count++)
                        {       
                                
                                
                                
                                K1 = (dt*(c*(rt[count]-rt[count-1])/(dt*1000)+(k*rt[count]/1000)-(k*y)-(c*v)))/m;
                                K2 = dt*((v + K1)/(dt + t));                
                                L1 = dt*v;
                                L2 = dt * ((y + L1)/(dt + t));
                                v = v + 0.5*(K1 + K2);
                                y = y + 0.5*(L1 + L2);
                                t = t + dt;
                                
                                tout[count] = t;
                                yout[count] = y;
                                             
                                fprintf(foutput, "\n%f,", t);
                                fprintf(foutput, " %f", y);
                
                                      
                        } 
                        
                fclose(foutput);
                
                system("cls");
                printf("\n\nThe results for the response of the vehicle can be found \nin the output.txt file!");
                
                
        }                  
                              
             
        printf("\n\nWhat would you like to do now?\n1. Calculate Another Profile");
        printf("\n2. Exit Program\n\n");
        scanf("\n\n%d",&again);
        system("cls");
               
        if (again==1)
        {
                 printf("Great!\n\n");
                 system("PAUSE");   
        }
       
        else
      
        if (again==2)
        {
                printf("\n\nThank you for using the road profile response emulator.\n\n");   
        }
                       
                   
                                            
        }

        return 0;   
        
}

  #4  
Old 03-May-2005, 23:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Read the Sticky. Take careful note of the guideline Do not start new threads on the same topic

And keep your hair on your side of the keyboard. I just found some in my soup

Generally, reset your variables as you start your pass into the calculations portion of the code. Usually at the beginning of a loop.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simulation Problem wu_weidong C++ Forum 7 12-Mar-2005 23:56
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 11:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.