Hi, I'm onto a second part of a problem sheet I've been given, and I've had to modify some code(that previously worked fine) to use the Runge Kutta method for solving ODE's. I have my code, shown below, but its outputting a straight line, and I dont know why. It should be a projectile parabola. I've been looking at this for hours now and can't figure out what's wrong. Any help is appreciated.
Code:#include <stdio.h>#include <stdlib.h> #include <math.h> const double pi = 3.141592654, g = 9.81; //define constants for easy use #define F(x,y) ((Uy/Ux)-(g*x)/(pow(Ux, 2))) //defining projectile function int main() { FILE *myfile; double U, theta, Ux, Uy, h, x, y1, y2, k1, k2, k3, k4; printf("Enter a launch speed between 0 and 22 in m/s\n");//0 and 22 explained in report scanf("%lf", &U); printf("Enter, in degrees, the angle of elevation you wish to shoot at\n"); scanf("%lf", &theta); Ux = U*cos(theta*pi/180);// x component of velocity Uy = U*sin(theta*pi/180);//y component of velocity x=0; h=0.1; k1 =h*F(x,y1); k2 =h*F(x+(h/2), y1+(k1/2)); k3=h*F(x+(h/2), y1+(k2/2)); k4=h*F(x+h, y1+k3); myfile=fopen("myoutput.txt", "w");//open file for results to be sent do { y2 = y1 + k1/6 + k2/3 + k3/3 + k4/6; x=x+h; printf("\n\n x= %lf y= %lf", x, y2);//print results in compiler fprintf(myfile, "\n\n %lf %lf", x,y2);//print results in file y1=y2;//repeat } while (y2>0); fclose(myfile); return 0; }



LinkBack URL
About LinkBacks


