So my code works, it's just that for some reason, my code always runs both if statements(both y equations, no matter which number I enter for the first fprintf question). Also, the t,y columns are always much longer than the t,y2 columns (EDIT i.e. if I enter funt=1,t0=0,t1=10,inity=1,dt=0.1, my t,y columns have 100 rows and my t,yexact columns have 10 rows). Furthermore, the t columns of the t,y part have all zeros for the first if statement (y equation) and all of one number for the second if statement(y equation). Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double rk4(double(*f)(double,double),double,double,double);
double user_fun_1(double,double);
double user_fun_2(double,double);
int main (void) {
int funct;
double *y,t,y2;
double t0,t1,dt,inity,C;
int i,n;
fprintf(stdout, "Which function do you wish to integrate?\n(1)f(t,y)=t\n(2)f(t,y)=-y\n");
fscanf(stdin,"%d",&funct);
fprintf(stdout,"What is the initial time?\n");
fscanf(stdin,"%lf",&t0);
fprintf(stdout,"What is the final time?\n");
fscanf(stdin,"%lf",&t1);
fprintf(stdout,"What is the initial value of y?\n");
fscanf(stdin,"%lf",&inity);
fprintf(stdout,"what is the step size?\n");
fscanf(stdin,"%lf",&dt);
if (dt>(t1-t0)){
printf("The stepsize has to be less than or equal to the total time\n");
return EXIT_FAILURE;}
n = 1 + (t1-t0)/dt;
y = (double *)malloc(n*sizeof(double));
if (funct=1){
C=inity-(t0*t0*0.5);
printf("%s%25s\n","t","y");
for (y[0]=inity,i=1;i<n;i++){
y[i]=rk4(user_fun_1,dt,t0 + dt * (i-1),y[i-1]);
printf("%lf%25lf\n",t,y[i]);}
printf("%s%25s\n","t","y exact");
for(i=0;i<n;i+=t1){
t=t0+dt*i;
y2=(t*t*0.5) + C;
printf("%lf%25lf\n",t,y2);}}
if (funct=2){
C=inity/(exp(-t0));
printf("%s%25s\n","t","y");
for(y[0]=inity,i=1;i<n;i++){
y[i]=rk4(user_fun_2,dt,t0+dt*(i-1),y[i-1]);
printf("%lf%25lf\n",t,y[i]);}
printf("%s%25s\n","t","y exact");
for(i=t0;i<n;i+=t1){
t=t0+dt*i;
y2=C*exp(-t);
printf("%lf%25lf\n",t,y2);}}
free(y);
return EXIT_SUCCESS;}
double user_fun_1(double t,double y){
return t;}
double user_fun_2(double t, double y){
return -y;}
double rk4(double(*f)(double, double), double dt,double t,double y){
double k1,k2,k3,k4;
k1=dt*f(t,y);
k2=dt*f(t + dt/2,y + k1/2);
k3=dt*f(t + dt/2,y + k2/2);
k4=dt*f(t + dt,y + k3);
return y + (k1 + 2*k2 + 2*k3 +k4)/6;}
Your
if
statement is wrong, it is an assignment not a comparison. It should beNOT
The same goes to the first
if
statement.A a safe c habit is to swap places of the constant and variable in an if statement. if(2==funtc) works and if(2=funct) will give a compile time error. Try to turn on compiler warning next time, it can help you find this type of error. If you are using
gcc
, add-Wall -Wextra
flags.The reason your
t, y
is longer thant, y exact
is yourn =101
, andt1 = 10
. So in firstfor
loop, you wrotei++
, so it loops for 100 times, but in the secondfor
loop, you usei+=t1
, so it only loops for 11 times.The reason
t
is always 0, is because you never assign any initial value to it nor assign a value in the firstfor
loop.