What wrong with my code

Hi,
I want to write a code that calculate the dy/dx=u (which u is a constant number) by euler method. But there is error in line 51
When I write for example 10 instead of u in line 51 (and delete line 18) and enter 40 for MAX it works and the final answer is near 400 but when I want to change the code in that way that it ask me 10 in line 18 and put to u and calculate it in does not work. why? how can I correct it?

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
50
51
52
53
  #include <iostream>
#include <cmath>
using namespace std;
const double dist=0.001;               /* stepsize in 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;
double MAX;
double u;
cin>>MAX;
cin>>u;
               /* max for t */

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(u);                 /* derivative of first equation */
}
Last edited on
1. Your indentation is not informative. It is hard to see the scopes.

2. You say "there is error". I bet you mean: "compiler returns an error", but you don't tell what the compiler actually says. It is very useful to learn to understand what the compiler tells you.
can you show another similar simple example without calculus? i have forgotten my college differentiation (a long time ago!)
The compiler says :

14:15:44 **** Incremental Build of configuration Debug for project tesy10 ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main10.o "..\\main10.cpp"
..\main10.cpp: In function 'double f(double, double*, int)':
..\main10.cpp:58:14: error: 'u' was not declared in this scope
return(u); /* derivative of first equation */
^
..\main10.cpp:59:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^

It says u is not declared in this scope at line 51
closed account (SECMoG1T)
You dint declare u as a parameter nor is u a global variable so that is why you get the error
How can I fix it?
Last edited on
closed account (SECMoG1T)
By passing u as a parameter

1
2
3
4
5
6
7

 double f(double x, double y[], int i , double u) /// u is a parameter
   { 
        return(u); /* derivative of first equation */
    }

/// then again I noted you are not using parameters x, y, and i what is their need. 
closed account (SECMoG1T)
Hold on a second , what is u ssupposed to be maybe a result of some calculation within the function
If so then you can make a local variable u

1
2
3
4
5
6
  double f(double x, double y[], int i ) 
     { 
         double u;
         u= // some calculations here.
         return(u); /* derivative of first equation */
     } 
Last edited on
Topic archived. No new replies allowed.