473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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("results.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("\nPlease insert n value= ");
scanf ("%f",&n);

while (n!=-1)
{
printf("\nt\t\tx\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("\nPlease 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("results.xls","w");

float t0,x0,v0,tn,n,h;
float dydx11,dydx12,dydx21,dydx22,slope1,slope2,t,x,xe,v e,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("\nPlease insert n value= ");
scanf ("%f",&n);

while(n!=-1)
{

printf("\nt\t\tx\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,xe,ve);
dydx22=f2(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(wPtr,"%f %f %f\n",t,x,v);

}
printf("\nPlease 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("results.xls","w");

float t0,x0,v0,tn,n,h;
float k11,k12,k21,k22,k31,k32,k41,k42,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("\nPlease insert n value= ");
scanf ("%f",&n);

while(n!=-1)
{

printf("\nt\t\tx\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*k21+2*k31+k41)/6;
slope2=(k12+2*k22+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("\nPlease 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\tx\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,dydx21,dydx22,slope1,slope2,xe,ve;

printf("\nt\t\tx\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,k42,s1,s2;

printf("\nt\t\tx\t\tv\n");
fprintf(wRK,"\nt\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("Euler.xls","w");
wHuen=fopen("Heun.xls","w");
wRK=fopen("Range-Kutta.xls","w");

// char selection;
float t0,x0,v0,tn,h,t,x,v;
int n;
// float k1,k2;
// float dydx11,dydx12,dydx21,dydx22,slope1,slope2,xe,ve;
// float k11,k12,k21,k22,k31,k32,k41,k42,s1,s2;

//main:

printf("\nPlease 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("\nPlease insert n value= ");
scanf_s ("%f",&n);

if(n!=-1)
{
Euler(t0,x0,v0,tn,n);
Huen(t0,x0,v0,tn,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?
Mar 28 '10 #1
10 4302
newb16
687 512MB
Did you determine in which loop exactly it loops infinitely, or you'd rather want us do it for you?
Mar 28 '10 #2
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.
Mar 28 '10 #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.
Mar 28 '10 #4
newb16
687 512MB
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?
Mar 28 '10 #5
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.
Mar 28 '10 #6
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.
Mar 28 '10 #7
newb16
687 512MB
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.
Mar 28 '10 #8
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{}?
Mar 28 '10 #9
newb16
687 512MB
Because in scanf you have format specifier %f when reading n which is int and requires %d
Mar 28 '10 #10
oh yeah..thanks..now 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?
Mar 29 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mmccaws | last post by:
Thanks ahead for your help I'm trying to learn what I can do with echo and print statements. I figured out the echo statement and below is the simple version using print. I've tried two dozen...
8
by: Shane Groff | last post by:
I know this is a recurring discussion (I've spent the last 3 days reading through threads on the topic), but I feel compelled to start it up again. After reading through the existing threads, I...
8
by: mark | last post by:
Access2000 How do I write a query that combines the CTC field from each record below into one record? I need to concatenate the CTC field with a separator, like below: ...
2
by: Henro V | last post by:
Can I integrate 3 existing forms (all based on the same query) integrate in one form by using Tab's? Thanx for thinking, Henro
24
by: trint | last post by:
add them into one PrintDocument: PrintDocument pd1 = new PrintDocument(); PrintDocument pd2 = new PrintDocument(); PrintDocument pdCombined = new PrintDocument(); pdCombined = pd1 + pd2;...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
0
by: thatsastounding | last post by:
Hi, I have 3 web projects built using asp.net 2.0. Basically, I want to combine these sites at run time into one main site. I use Web Deployment Projects to deploy my sites so that each site has...
46
by: dude88888 | last post by:
Hello I have one query where i get the total number of units for an entire fund say fund 89 has 1000 total units Then i need to find out how much of the total fund each user has say user...
13
by: mike3 | last post by:
Hi. (crossposted because the program is in C++ and some C++-related elements are discussed, hence comp.lang.c++, plus general program design questions are asked, hence comp.programming.) I'm...
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.