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