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

  • Thread starter Thread starter nard
  • Start date Start date
  • Tags Tags
    Program Writing
Click For Summary
SUMMARY

The discussion focuses on writing a C program to compute the values of yn+1 for n=100 using the given differential equations. The equations involve calculating y and its derivative z, with initial conditions y(0)=1 and y'(0)=0, and a step size h=0.1. The user provided a code snippet that implements a for loop to iterate through the calculations and store the results in a file named "cop.txt". The program successfully outputs the values of z(n+1) and y(n+1) to both the console and the specified file.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with numerical methods for solving differential equations
  • Knowledge of file handling in C
  • Basic understanding of trigonometric functions and their applications
NEXT STEPS
  • Explore user-defined functions in C programming
  • Learn about numerical integration techniques such as Euler's method
  • Investigate advanced topics in solving ordinary differential equations (ODEs)
  • Study file I/O operations in C for better data management
USEFUL FOR

This discussion is beneficial for C programmers, students learning numerical methods, and anyone interested in implementing mathematical models through programming.

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
 
Technology 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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
10
Views
2K
Replies
31
Views
4K