Problem with function inside forloop
Im having problems with using a function in a forloop. i keep getting an error saying its not declared in the scope.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <vector>
#include <fstream>
#include<cmath>
#include<math.h>
#define PI 3.14
#define R .1
#define C 1.0
#define h .05
#define tend 1.0
#define a tend/h
using namespace std;
double inputvoltage(double x);
int main()
{
ofstream myfile;
myfile.open ("example2.txt");
vector<double> volt (a);
vector<double> k1 (a);
vector<double> k2 (a);
vector<double> k3 (a);
vector<double> k4 (a);
cout<<"enter an initial condition "<<endl;
cin>>volt[0];
cout<<"time,t" <<"\t"<< "voltage,v "<<endl;
for(int i = 0; i<=20 ; i ++)
{
k1[i] = (inputvoltage(i) - volt[i])/(R*C);
k2[i] = (inputvoltge(i) - (volt[i] + (.5*h*k1[i])))/(R*C);
k3[i] = (inputvoltage(i) - (volt[i] + (.5*h*k2[i])))/(R*C);
k4[i] = (inputvoltage(i) - (volt[i] + h))/(R*C);
volt[i+1] = volt[i] + ((h/6)*(k1[i] + 2*k2[i] + 2*k3[i] + k4[i]));
cout<<i*h<<"\t"<<volt[i]<<endl;
myfile<<i*h<<"\t"<<volt[i]<<endl;
}
myfile.close();
return 1;
}
double inputvoltage( double x )
{
double v = 0 ;
v = (10* exp(-1*x*h/.05))*sin(2*PI*x*h/.03);
return v;
}
|
you have a typo on line 41
k2[i] = (inputvoltge(i) - (volt[i] + (.5*h*k1[i])))/(R*C);
you forgot the A in voltage
damn sorry its late. ha
Topic archived. No new replies allowed.