# Integrator program using borland C

Discussion in 'Programmer's Corner' started by aivogue, May 14, 2013.

1. ### aivogue Thread Starter New Member

Oct 8, 2012
2
0
hi.. i am currently doing my project which is using matlab simulink software and need to convert to the borland C, using C programing and will download it to DSP board. But i have got problem to write the program for integral to replace the 1/s block in simulink to the program by using borland C that can be connected to dsp board . can someone help me. thank you in advance.

2. ### ActivePower Member

Mar 15, 2012
155
23
Looks like you're trying to convert MATLAB/Simulink code to C. There's plenty of options available for a direct conversion. Matlab itself offers a conversion program (although I haven't used it yet). Have you considered them?

The 1/s block (which looks continuous) in Simulink is symbolic. Underneath the Simulink model, Matlab uses discrete data processing techniques (various numerical methods) for integration and other operations.

If your application doesn't require much accuracy you could go for simple numerical integration techniques (try summing up the numbers?). Any detail about what you're trying to do would be useful here.

Hope this helps.

3. ### aivogue Thread Starter New Member

Oct 8, 2012
2
0
I already try to summing up the numbers by using this program,

u(1)=slip,
u(2)=speed;
u(3)=time

t=u(3);
a=u(1)+u(2);
b(t)=a

if t<0
theta = b(t)
else
theta=b(t)+6(t-1)
end

sys(1)=theta

but it shows some error which are

1) Attempted to access b(0); index must be a positive integer or logical
2) Have flag=3 (output) at time (0)

Is it possible to write the integral coding like this?
can u help me to give some example of a basic program to replace the integral block?

4. ### ActivePower Member

Mar 15, 2012
155
23
What exactly are you trying to do here? Looks like you're trying to find angular position by integrating speed wrt time. If so, you need to take the discrete values of speed at different times and add them (and multiply by a scaling factor if needed, to avoid overflows).

Take a fixed time step, i.e. difference between successive elements in your 't' vector and multiply it with successive steps of the quantity you want to integrate. The links below should help.

This is because you're trying to access b(0) in your if-block. MATLAB indexes arrays beginning at 1 so you need to modify your program accordingly. Can you post a detailed snippet with some comments so that we can know what you're trying to do in your code?

Here's a few links related to numerical integration you can look at:
http://mathworld.wolfram.com/RiemannSum.html (Riemann Sums)
http://bigprof.com/c4engineering/intgr001.htm
http://www2.le.ac.uk/Members/mr6/lectures/numc.pdf (this one contains a detailed tutorial for numerical calculations in C)

PS: It'd be better if you could post your questions on the public forums, that way you'll have a better chance at having your questions answered quickly. It'll also help others with similar questions.

PS2: Use code tags (the hash/pound symbol in the editor) to separate code and text. It makes your code a lot more easier to read.

Last edited: May 19, 2013