HELP

I am a beginner at programming and I am having some trouble with the results in the following program:

****I need C to keep 0 after it gets 0 and in my program after it gets 0 it starts growing again.

Can anyone help me?


#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iomanip.h>
#include <fstream.h>
#include <iostream.h>


ofstream fout ("Modelo_1");

double t, C, M, O;
const float ti=0., tf=100., h=0.1, m=0.05, r=0.05;
float a=0.2;

double f1(double t, double C, double M, double O);
double f2(double t, double C, double M, double O);
double f3(double t, double C, double M, double O);
double n;
double k1, k2, k3, k4; //valores da function

main()
{
n=(tf-ti)/h;

t=ti;
C=10.;
M=0.1;
O=0.0;
cout<<"SOM inicial vale "<<C<<endl;
cout<<"MM inicial vale "<<M<<endl;
cout<<"rM inicial vale "<<O<<endl;
getch();


fout<<"t "<<" "<<"C "<<" "<<"M "<<" "<<"O "<<" "<<endl;
for(int i=1; i<n+1; i++)
{

k1=f1(t, C, M, O);
k2=f1(t+h/2., C+h*k1/2., M, O);
k3=f1(t+h/2., C+h*k2/2., M, O);
k4=f1(t+h, C+h*k3, M, O);
C=C+(h/6)*(k1+2*k2+2*k3+k4); //runge kutta para C
if(C<=0.00001)
{
C=0.;
a=0.;
}


k1=f2(t, C, M, O);
k2=f2(t+h/2, C, M+h*k1/2, O);
k3=f2(t+h/2, C, M+h*k2/2, O);
k4=f2(t+h, C, M+h*k3, O);
M=M+(h/6)*(k1+2*k2+2*k3+k4); //runge kutta para M

k1=f3(t, C, M, O);
k2=f3(t+h/2, C, M, O+h*k1/2);
k3=f3(t+h/2, C, M, O+h*k2/2);
k4=f3(t+h, C, M, O+h*k3);
O=O+(h/6)*(k1+2*k2+2*k3+k4); //runge kutta para O
t=t+h;

cout<<"t= "<<t<<" "<<"C= "<<C<<" "<<"M= "<<M<<" "<<"O= "<<O<<" "<<endl;
fout<<t<<" "<<C<<" "<<M<<" "<<O<<" "<<endl;
getch();
}
getch();
}



double f1(double t, double C, double M, double O)
{
return (-a*M+m*M); //para mat org
}

double f2(double t, double C, double M, double O)
{
return M*(a-r)-m*M; //para os micro org
}


double f3(double t, double C, double M, double O)
{
return r*M;
}

Hi !, please format your code like this with proper indentation so that it's readable in the future.

To answer your question, it looks like there should be a breakpoint when C == 0 in your loop. This should fix it:

1
2
3
4
5
6
		if(C<=0.00001)
		{
			C=0;
			a=0;
			break;
		}


A few more thoughts to get you into good coding habits:

Add more spacing such that k3=f3(t+h/2, C, M, O+h*k2/2); looks more like k3 = f3( (t + h/2), C, M, (O + h*( k2/2) ) );

C=10.; should be C=10.0;

You should use "int main()" at the start and "return 0;" at the end.

Your functions (and variables) should have more meaningful names (I'm not sure what they could be here) to make it easier for someone to follow what's going on.

Also, consider simplifying this: k2=f3(t+h/2, C, M, O+h*k1/2); so that the function itself handles most of those +*/ operations for 2 reasons:
1- again, easier to read
2- less work for you

Also, it looks like your doing a trick that's hard to see when passing for example 't+h/2' as the first param of f3(t, ...). Since t is a global variable, passing t+h/2 into t effectively changes the value of 't' ? Honestly, I don't even know lol either you're not using param t at all or it's being assigned a new value as u'r passing it into the func. Either way it's confusing as ___ to follow. If u want to change the value of t there, pass it in by reference and do this:
1
2
3
4
5
f3(double &t, ...)
{
   t = t+h/2; 
...
}

Same goes for the other params like this...


Hope this helps :)
Last edited on
Soranz,

Thank you very much for your help. Your advice really helped me out. My program is working perfectly now. =)
Topic archived. No new replies allowed.