Difference between c function and s function

9 views (last 30 days)
AKHILA
AKHILA on 6 Sep 2024
Edited: AKHILA on 29 Oct 2024 at 6:40
Is there any difference between c function and s function block?In the mathlab documentation I saw as
"The C Function block supports initializing persistent data and calling external functions from the block dialog. The C Function block supports only initializing and terminating persistent data; the block does not support updating the data during simulation. To model a dynamic system with continuous states, use an S-Function block. "
Can anyone explain me this difference with some example? Or whether this difference is no longer there in new version(2024a) of Matlab?

Accepted Answer

Jaimin
Jaimin on 6 Sep 2024
Hello @AKHILA
The distinction between the “C Function block” and the “S-Function block” in Simulink can be clarified by examining their capabilities and intended use cases:
C Function Block
  • Purpose: The C Function block is intended for embedding custom C code into a Simulink model. It is generally used for straightforward computations and invoking external C functions.
  • Initialization: It allows for the initialization of persistent data, enabling data to maintain its value across function calls.
  • Limitations: The block does not allow for updating persistent data during simulation, meaning you cannot have persistent states that evolve over time within the block.
  • Use Case: Best suited for static computations or functions that do not need state updates.
S-Function Block
  • Purpose: The S-Function block is highly versatile and robust, enabling the creation of custom blocks that can manage complex behaviors, including dynamic systems with states.
  • Initialization and Termination: It allows for both the initialization and termination of persistent data.
  • State Updates: In contrast to the C Function block, the S-Function block can handle both continuous and discrete states, making it ideal for modelling dynamic systems.
  • Use Case: Perfect for modelling intricate dynamic systems that necessitate state updates throughout the simulation.
Example Scenario
  • C Function Block Example: Imagine you have a C-written mathematical function that computes the square root of a number. The C Function block can be used to call this function within a Simulink model. The computation is static, with no need for state updates.
  • S-Function Block Example: Consider simulating a simple harmonic oscillator, a system with continuous states (position and velocity) that evolve over time. An S-Function block would be used to implement the differential equations that describe the oscillator's motion, as it supports state updates during the simulation.
To check “C-Function Block” and “S-Function Block” working in MATLAB R2024a please refer this MathWorks Documentation
I hope this will be helpful.
  1 Comment
AKHILA
AKHILA on 6 Sep 2024
Edited: AKHILA on 29 Oct 2024 at 6:40
Thanks @Jaimin, thank you for your answer. I was experimneting the block with an example of integrater
double integrate1(double input)
{
static double state = 0; // Persistent state variable
state = state+ input; // Update state
return state; // Output state
}
Here in this code, we are updating the value of state, and it's working in c function. That's why I got confused. Can you help me in clarifying my doubt in this case

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!