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

    Active Member

    Mar 15, 2012
    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
    I already try to summing up the numbers by using this program,



    if t<0
    theta = b(t)


    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?
    thank you in advance.
  4. ActivePower

    Active Member

    Mar 15, 2012
    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: (Riemann Sums) (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