Writing a Program to Calculate yn+1 for n=100

  • Thread starter Thread starter nard
  • Start date Start date
  • Tags Tags
    Program Writing
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
nard
Messages
16
Reaction score
0
can somebody help me on writting a program that displays yo
to yn+1 when n=100 and store them in a file.
y"=cos(t)*y+y
y(0)=1
y'(0)=0
h=0.1
y'=z
z'=z'*cos(t)+y
yn+1=zn+h*zn*(cos(tn))+yn)
tn=t+nh
 
Physics news on Phys.org
i have the above using for loop without declaring any fuction,
if someone knows how to solve it including user defined function then he might be able to help me.
thank you!
the one i wrote is showed below

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
FILE *fp;
fp=fopen("D:\\cop.txt","w");
float y[100],dy[100],z[100],h,t[100];
int i;
y[0]=1;
z[0]=0;
h=0.1;
t[0]=0;
for(i=0;i<100;i++)
{
t=t[0]*i*h;
y[i+1]=y+h*z;
z[i+1]=z+h*((z*cos (t))+y);
}
fprintf(fp,"i\tz(n+1)\t\ty(n+1)\n\n");
printf("i\tz(n+1)\t\ty(n+1)\n\n");
for(i=0;i<100;i++)
{

fprintf(fp,"%d\t%f\t%f\n",i,z[i+1],y[i+1]);
printf("%d\t%f\t%f\n",i,z[i+1],y[i+1]);
}
fclose(fp);
system("PAUSE");

}
 
Last edited: