# why does this simple program not work

1. ## why does this simple program not work

Hi
I have a simple code to learn about PI controller. The set point is 1.0 and output is not converging. What is the problem?

```Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{  /* Variables*/
double input,output,kp,error,ki,kpo,kio,T;
input=1.00;
kp=1;
ki=0.1;
output=0;
T=1/100;

while(1)
{
error=input-output;
kpo=error*kp;
kio=kio+(error*ki*0.01);
output=kpo+kio;
printf("output is %f\t%f\t%f\t%f\n",error,kpo,kio,output);
}
}```

•

2. ## Re: why does this simple program not work

I don't know why it isn't working
I note that kio is not initialized and you never use T
I tried to simulate it with excel, since it is only a few lines.
output oscillates between (about) 2+x and (about) -x, where x is 0.01, 0.02, 0.03... and increases like that every iteration

I suggest you check your equations and initial values.

I changed input to 5, kp to 0.1, ki to 2, and the output converged to 5 (< 1% error) at about 265 steps
I then changed input to 3 at step 265 and the putput converged to (< 1%) at step 435

looks like changing some parameters is sufficient

•

3. ## Re: why does this simple program not work

Hi
I hooked these values kp=1 and ki=0.1 in simulink and works ok. Converges in 100s. But no random fluctuations. I also changed the values as you suggested and indeed there was convergence. Strange but why does my earlier program oscillate so much?

•

4. ## Re: why does this simple program not work

control theory rests on the roots of quadratic equations.
depending on the roots, you can get decay (e^-x), decay with oscillation (e^-x sin x), growth with oscillation (e^x sin x) and maybe even just growth (e^x)

the parameters you chose led to growth (albeit small) with oscillation (that's what it looked like to me)
choosing different parameters led to different result.

5. ## Re: why does this simple program not work

You wrote "output=kpo+kio;" instead of "output=output+kpo+kio;"

•

6. ## Re: why does this simple program not work

You wrote "output=kpo+kio;" instead of "output=output+kpo+kio;"
That's o.k. Problem is unsuitable combination of kp and ki, as already stated. kp=1 seems generally problematic. A controller with practical use has set point and process value inputs, by the way. I understand that the simplified example is modelling a plant with unity transfer function.

--[[ ]]--