would you please guide how to write this code?
Oct 27, 2014 at 1:32pm UTC
Hi,
I want to write a code that n times ask me a constant number (c) and solve the dx/dt=c by euler methods. would you please help me? I have written a code to solve the equation dy/dx=10*x by euler methods but I do not know how to change it for n times asking a constant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <cmath>
using namespace std;
#define dist 0.001 /* stepsize in t*/
#define MAX 1.0 /* max for t */
int N=1;
void euler(double x, double y[], double step); /* Euler function */
double f(double x, double y[], int i); /* function for derivatives */
int main()
{
double t, y[N];
int j;
y[0]=0; /* initial condition */
for (j=0; j*dist<=MAX ;j++) /* time loop */
{
t=j*dist;
cout<<"j=" <<j<<"\t" <<"t=" <<t<<"\t" <<"y[t]=" << y[0]<<"\n" ;
euler(t, y, dist);
}
}
void euler(double x, double y[], double step)
{
double s[N]; /* for euler */
int i;
{
for (i=0;i<N;i++)
{ s[i]=step*f(x, y, i);
}
}
{
for (i=0;i<N;i++)
y[i]+=s[i];
}
}
double f(double x, double y[], int i)
{
return (10*x); /* derivative of first equation */
}
Topic archived. No new replies allowed.